2016-06-13 24 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