如何查看页面显示后是否存在其中一个元素

时间:2016-07-04 17:33:11

标签: protractor

量角器必须验证页面中是否存在错误消息。因此,错误消息可以在“alert-message”类或“fail-heading”类中。

如果我在下面这样做,只有在显示alert-message className时它才会成功。但有时我将className称为“fail-heading”。

var waitforele_confirmation = by.className('alert-message');
browser.wait(function(){return browser.driver.isElementPresent(waitforele_confirmation)}, 60000);
expect(browser.driver.isElementPresent(waitforele_confirmation)).toBeTruthy();

所以我想在页面加载后动态检查哪个元素存在。另外我想在等待每个元素时使用browser.wait函数。我只是为了理解而做了下面的伪代码。

while (i<120 and !found)
{
    int i=0;
    if (element(by.className('alert-message')).isPresent())
    {
    found = true;
    }
    else if (element(by.className('fail-heading')).isPresent())
    {
    found = true;
    }
    else
    {
    browser.sleep(500);
    }
    i++;
}

有人可以在量角器中转换伪代码吗?

2 个答案:

答案 0 :(得分:1)

let EC = protractor.ExpectedConditions;
let alertMessageVisibility = EC.visibilityOf($('.alert-message'));
let failHeadingVisibility = EC.visibilityOf($('.fail-heading'));

browser.wait(EC.or(alertMessageVisibility, failHeadingVisibility), 60000, "Alert message or fail heading should become visible in 60 seconds, but it wasn't")

答案 1 :(得分:0)

下面的代码可以按预期工作:

 var EC = protractor.ExpectedConditions;
 var alertMessageElement = element(by.className('alert-message'))
 var failHeadingElement = element(by.className('fail-heading'))

/*browser.wait() will be keep on checking for error messages until either   
any one of the error message found or specified timeout limit reached*/

browser.wait(function(){
EC.visibilityOf(alertMessageElement).call().then(function(isAlertMessagePres
 ent){
    if(isAlertMessagePresnt){
    return true;
  }
})

EC.visibilityOf(failHeadingElement).call().then(function(isFailedHeadingPresent)   {
 if(isFailedHeadingPresent){
    return true;
}
 })
   },10000,'No error message found within specified timeout of 10 seconds    
       ');