Nightwatchjs:如何在不创建错误/失败/异常的情况下检查元素是否存在 [英] Nightwatchjs: how to check if element exists without creating an error/failure/exception

查看:119
本文介绍了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
    }
});

这篇关于Nightwatchjs:如何在不创建错误/失败/异常的情况下检查元素是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆