Nightwatchjs:如何在不创建错误/失败/异常的情况下检查元素是否存在

Nightwatchjs:如何在不创建错误/失败/异常的情况下检查元素是否存在

问题描述:

在我正在测试的页面中,可能会显示两个按钮:基本"或高级".

In the page I'm testing, two buttons may be displayed: BASIC or ADVANCED.

我想知道ADVANCED按钮是否正在显示-如果是,请单击它.

I want to be able to tell if the ADVANCED button is showing -- and if so, click it.

如果显示"BASIC"按钮,我什么也不做,继续测试.

If the BASIC button is showing, I want to do nothing and continue with my test.

我尝试过的所有Nightwatchjs选项都会生成失败消息.例如,如果我"waitforpresent"或"waitforvisible"并且按钮不存在,则会生成错误/失败.我只想知道存在哪个按钮,以便可以在代码中做出决定.

All of the Nightwatchjs options I've experimented with generate a failure message. For example if I "waitforpresent" or "waitforvisible" and the button is not there, it generates an error/failure. I just want to know which button is present so I can make a decision in my code.

这是我尝试过的:

try { 
    browser.isVisible('#advanced-search', function(result) {console.log(result.state); }) 
} catch (myError) 
{ 
    console.log(myError); 
}

有想法吗?

您可以使用 Selenium协议元素" 和一个回调函数,用于检查结果状态以确定是否找到了该元素.例如:

You can achieve this by using the Selenium protocol "element" and a callback function to check the result status to determine if the element was found. For example:

browser.element('css selector', '#advanced-search', function(result){
    if(result.status != -1){
        //Element exists, do something
    } else{
        //Element does not exist, do something else
    }
});