2017-05-30 50 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(); 
     }); 
    });