【问题标题】:Resolving multiple promises using Protractor, Cucumber, Chai as Promised使用 Protractor、Cucumber、Chai 作为 Promised 解决多个 Promise
【发布时间】:2015-09-18 15:57:38
【问题描述】:

我需要断言一系列div .container 的孩子.row 的数量大于0。

this.Then(/^I should see at least one element in each container$/, function (done) {  
    page.element.all(by.css('.container'))                                                    
        .each(function (element, index) {                                     
            element.all(by.css('.row'))                               
                .then(function(elements) {                                        
                    expect(elements.length).to.be.greaterThan(0);                   
                    done();                                                       
                });                                                               
    });
});                                                                   

标记看起来像:

<div class="container">
    <div class="row"></div>
    <div class="row"></div>
    <div class="row"></div>
</div>
<div class="container">
    <div class="row"></div>
</div>

并且每个容器中必须至少有一个.row

问题是它总是通过,只要第一个检查为真。

我尝试了使用 protractor.promise 对象的不同解决方案,还尝试在 each 迭代中保存承诺,然后尝试在最后实现预期但没有任何效果。

我已经开始考虑我应该从一个完全不同的角度来采用这种方法。

有接受者吗?

【问题讨论】:

  • 这些行在 div 下吗?我的意思是一个 div 有很多行,像这样?
  • @GirishSortur 添加了标记定义。

标签: javascript angularjs protractor chai cucumberjs


【解决方案1】:

您应该获取每个 div 下的行元素,然后断言每个 div 中是否有多个行。试试这个 -

this.Then(/^I should see at least one element in each container$/, function () {  
    page.element.all(by.css('.container'))                                                    
        .each(function (ele, index) {                                     
            ele.$$('.row')     // get all the row elements in each div                           
                .then(function(elements) {                                        
                    expect(elements.length).to.be.greaterThan(0);                                                                         
                });                                                               
    });
});  

希望对你有帮助

【讨论】:

  • 我认为ele.$$('.row') 实际上与ele.all(by.css('.row')) 相同:-/ 不幸的是,只要第一个expect 返回true,它仍然会退出。
  • 好的,看到您的最后一次编辑删除了done() 调用。我现在得到AssertionError: expected 1 to be above 1
  • 我猜那是因为您在第二个 div 中只有 1 行。你能告诉我这个错误是从哪里来的吗?我的意思是第一个 div 有 3 行还是第二个 div 有 1 行。并且您的预期错误看起来像是您期望的东西大于(1)。
  • 我在每个容器中使用 11 行和 12 行来测试它。这就是为什么这对我来说根本没有意义。
  • 哎哟,发现我正在查看的错误来自拼写错误。现在,没有done() 我得到:BUG: launcher exited with 1 tasks remaining
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多