2016-06-13 122 views
1

我有一個方案,我搜索一個文本字符串,它可能是返回結果中任何字段的一部分,它可能在標題中,或者彙總或描述返回的多個結果。我想寫一個可以匹配這三個字段的測試,如果其中任何一個是真的,那麼我的測試應該通過。如何驗證是否有任何一個期望是真實的多個期望條件

我怎樣才能把多個期望條件與OR條件。

+0

檢查也是在這裏我的答案 - http://stackoverflow.com/questions/37800341/assert-an-array-reduces-to-true/37808169#37808169 – Xotabu4

回答

2

可以與protractor.promise.all()解決它:

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); 
}); 

此測試會通過,如果3個字段的的至少一個是本


如果你問具體爲約等待的元素之一出現,你可以使用protractor.ExpectedConditions.or()

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

browser.wait(EC.or(
    EC.presenceOf(title), 
    EC.presenceOf(summary), 
    EC.presenceOf(description)), 5000); 
+0

我不想檢查元素的存在,我想檢查搜索到的文本是否與返回的結果匹配,並且搜索文本可能位於結果的這三個字段(標題,摘要,描述)中的任何一箇中。我怎樣才能做到這一點 ? – ssharma

1

在Java中,我們可以使用或像下面

 String expected="cool"; //this is my expected value 

     String actual1="cool"; //get title from driver 
     String actual2="xyz"; //get summary from driver 
     String actual3="abc"; //get required text from driver 

    Assert.assertTrue((expected.equals(actual1)) | (expected.equals(actual2)) | (expected.equals(actual3))); 

如果您正在查找標題或摘要的句子中的特定單詞,下面的方法將有所幫助。

 String actual1="its very cool"; //get title from driver 
     String actual2="xyz"; //get summary from driver 
     String actual3="abcd"; //get required text from driver 

    //here i am checking for cool  
    Assert.assertTrue((actual1.matches(".*cool.*")) | (actual2.matches(".*cool.*")) | (actual3.matches(".*cool.*"))); 
+0

Thansk爲您的回覆@murali,但我正在尋找在量角器的決議。 – ssharma

相關問題