2016-06-13 25 views
7

在其中一个测试中,我们需要声明其中一个元素存在。目前我们使用的protractor.promise.all()Array.reduce()这样做:断言数组减少为真

var title = element(by.id("title")), 
    summary = element(by.id("summary")), 
    description = element(by.id("description")); 

protractor.promise.all([ 
    title.isPresent(), 
    summary.isPresent(), 
    description.isPresent() 
]).then(function (arrExists) { 
    expect(arrExists.reduce(function(a,b) { return a || b; })).toBe(true); 
}); 

有没有更好的办法茉莉花来解决这个问题没有明确解决的承诺?我们需要一个自定义匹配器还是可以用内置匹配器解决?

+1

请注意,您可以使用'arrExists.some(function(a){return a;})'而不是'reduce'。使用Jasmine,你可以在'it'回调函数中使用['done'参数](http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support),当你完成测试时调用它。我不认为有更好的办法。无论如何您需要等待承诺解决。 – trincot

+0

@trincot感谢您的观点。我应该可能已经提到['expect()'在量角器中被“修补”来隐含地解决承诺](http://www.protractortest.org/#/control-flow#protractor-adaptations)..但是因为我必须将多重承诺的结果彼此结合起来,但在这种情况下可能无济于事。 – alecxe

+1

您应该提供'false'作为初始值,以便它可以在空数组上工作。或者使用'.some(布尔)'代替。 – Bergi

回答

2

检查这一点:我正在执行的函数,ExpectedCondition返回

let EC = protractor.ExpectedConditions; 

let title = EC.visibilityOf($("#title")), 
    summary = EC.visibilityOf($("#summary")), 
    description = EC.visibilityOf($("#description")); 

expect(EC.or(title, summary, description)()).toBeTruthy('Title or summary or description should be visible on the page') 

通知 - 所以我得到的,而不是功能函数(承诺将被解析为布尔值)的结果。

,如果你需要它,而不是.visibilityOf(可以使用.presenceOf())

http://www.protractortest.org/#/api?view=ExpectedConditions.prototype.or

+0

使用预期条件的绝妙想法,从来没有想到这一点。谢谢。 – alecxe

3

你可以简单地得到所有与单一选择的元素和断言计数优于零:

var title_summary_description = element.all(by.css("#title, #summary, #description")); 
expect(title_summary_description.count()).toBeGreaterThan(0); 
+0

有趣的开箱即用思想,谢谢! – alecxe