2017-05-30 27 views
1

我想獲得一個arraylist &的計數,然後試圖斷言在數組的值的關鍵字的存在。以下是我的代碼有問題;無法聲明在arraylist中的關鍵字

describe('My Test', function() { 

    it('Test starts', function() { 

    browser.ignoreSynchronization = true; 
    browser.get('https://www.w3schools.com/angular/'); 

    browser.sleep(5000).then(function(){}); 
    var results = element.all(by.css(".sidesection>p>a")); 

    var results_count=results.count().then(function(counting){ 
     console.log("There are total "+counting+" lines"); 
     return counting; 
    }) 
    results_count.then (function(count){ 
     console.log("There are totalx "+count+" lines"); 

     for (var iterate=1;iterate<count;iterate++){ 

     results.get(iterate).getText().then(function(text){ 
      console.log("The text in Relationship Type node line "+iterate+" is ---"+text); 
      expect(text.indexOf('Navigation')!=-1).toBeTruthy(); 
     })  
     } 
    }) 
    }) 

}) 

輸出:

There are total 19 lines 
There are totalx 19 lines 
The text in Relationship Type node line 19 is ---Dropdowns 
The text in Relationship Type node line 19 is ---Accordions 
The text in Relationship Type node line 19 is ---Convert Weights 
The text in Relationship Type node line 19 is ---Animated Buttons 
The text in Relationship Type node line 19 is ---Side Navigation 
The text in Relationship Type node line 19 is ---Top Navigation 
The text in Relationship Type node line 19 is ---JS Animations 
The text in Relationship Type node line 19 is ---Modal Boxes 
The text in Relationship Type node line 19 is ---Progress Bars 
The text in Relationship Type node line 19 is ---Parallax 
The text in Relationship Type node line 19 is ---Login Form 
The text in Relationship Type node line 19 is ---HTML Includes 
The text in Relationship Type node line 19 is ---Google Maps 
The text in Relationship Type node line 19 is ---Loaders 
The text in Relationship Type node line 19 is ---Tooltips 
The text in Relationship Type node line 19 is ---Slideshow 
The text in Relationship Type node line 19 is ---Filter List 
The text in Relationship Type node line 19 is ---Sort List 
[31mF[0m 

Failures: 
1) My Test Test starts 
    Message: 
[31m Expected false to be truthy. 
我這裏有2個查詢上我堅持

1)爲什麼我會得到數19值的所有列表硬編碼,我想輸出計數是迭代像1,2,3,4 ...等

2.)爲什麼我的期望語句失敗,雖然關鍵字是存在於某些數組值。

有人能幫我理解&如何解決上述2個問題?

回答

1

在(1)我還不能肯定,但我可以肯定回答(2)有利於提高你的代碼有點

1)這似乎是經典的for循環範圍的問題,其中環已經完成直到它被稱爲...參見this question。對於量角器和控制流程執行如何發揮作用並不積極。

2)你的期望是失敗的,因爲它檢查每一行,你說每行文本與'導航'相比的條件將評估爲真。這其中很多會失敗(即幻燈片,工具提示,裝載機等)。您需要更好的斷言,例如,您可以按1:1執行鏈接:expect(results.get(i).getText()).toEqual('Help'),或者您可以創建一個導航項目數組,並期望它們匹配等......但您肯定需要更好的斷言。這個測試究竟在做什麼?

無論哪種方式,這裏的一些幫助,你的代碼一般:

  1. 你並不真的需要量角器for循環,除非你正在做的非常具體的東西。您可以使用each來遍歷ElementArrayFinder。
  2. 這是更多的語義,但是您可以使用從promise中返回的值而不是將其分配給變量,您的某些代碼有點多餘。

    results.count().then(function(counting){ 
        console.log("There are total "+counting+" lines"); // logs 19 
        return counting; 
    }).then(function (count) { 
        console.log(count); // logs 19 
        for(var i = 0; i<count; i++) { 
    
        } 
    }) 
    

但同樣,for循環是不是真的有必要在量角器:如果你實現它這樣你就可以省略有關results_count的部分。相反,您可以使用each,這會使您的代碼更加簡潔,並消除您遇到的環路關閉問題:

var results = element.all(by.css(".sidesection>p>a")); 
    results.each(function (elem, index) { 
     return elem.getText().then(function (text) { 
      console.log('Element at index ' + index + ' has text ' + text); 
      // this still fails because it's not a good assertion 
      expect(text.indexOf('Navigation')!=-1).toBeTruthy(); 
     }); 
    });