Javascript 如何在量角器中为元素是否存在创建条件
我用的是量角器JS。这个网站是用JS编写的 所以我有一个拨动开关。我注意到切换开关中的值从真变假 当您将其关闭或打开时,false变为true 当量角器访问我的页面时,我试图创建一个条件,当它看到切换开关“关闭”时,它将“打开”。如果拨动开关已经“打开”,它将首先“关闭”,然后再次“打开” 我想出了这个代码,但由于某种原因它不起作用:Javascript 如何在量角器中为元素是否存在创建条件,javascript,jquery,angularjs,selenium-webdriver,protractor,Javascript,Jquery,Angularjs,Selenium Webdriver,Protractor,我用的是量角器JS。这个网站是用JS编写的 所以我有一个拨动开关。我注意到切换开关中的值从真变假 当您将其关闭或打开时,false变为true 当量角器访问我的页面时,我试图创建一个条件,当它看到切换开关“关闭”时,它将“打开”。如果拨动开关已经“打开”,它将首先“关闭”,然后再次“打开” 我想出了这个代码,但由于某种原因它不起作用: if( expect(element(By.id('toggle-switch')).element(By.css('[value="false"]')).is
if( expect(element(By.id('toggle-switch')).element(By.css('[value="false"]')).isDisplayed()) ) {
element(By.id('toggle-switch')).click();
console.log('in the if')
}
else{
element(By.id('toggle-switch')).click();
browser.sleep(3000);
element(By.id('toggle-switch')).click();
console.log('in the else')
}
此代码似乎仅适用于if语句。由于某种原因,它永远不会去其他地方。以下是我收到的错误:
NoSuchElementError:使用locator:By.cssSelector(“[value=\'false\']”)找不到元素
于是我试了一下
.isPresent()
而不是。isDisplayed()
我不再收到上述错误,但由于某种原因,在使用.isPresent()时,它总是转到if语句,只运行该语句,而从不运行else语句。没有显示错误
如果有更好的办法,请告诉我。这似乎非常有限,无法在此框架中创建适当的条件。请记住,
isDisplayed()
返回一个承诺,您可以尝试:
element(anyFinder).isDisplayed().then(function(result) {
if ( result ) {
//Whatever if it is true (displayed)
} else {
//Whatever if it is false (not displayed)
}
});
isDisplayed()
对我不起作用。API可能已更改isPresent()
是我的解决方案:
var logoutButton = element(by.css('[ng-click="log_out()"]'));
logoutButton.isPresent().then(function(result) {
if ( result ) {
logoutButton.click();
} else {
//do nothing
}
});
或者尝试从我的头顶实现这个解决方案,安排一个命令来测试页面上是否存在元素。如果在评估等待时发生任何错误,将允许它们传播
function alwaysSwitchOn(element) {
browser.driver.isElementPresent(element).then(function(isPresent) {
if (isPresent) {
isPresent = true;
}
else {
browser.driver.wait(function () {
return browser.driver.isElementPresent(element);
}, 5000);
}
// to fail the test, then uncomment this line
//expect(isPresent).toBeTruthy();
}).then(function () {
if (element.getAttribute('value') === 'OFF') {
element.click();
}
else {
// turn it OFF
element.click();
// turn it back ON
element.click();
}
});
}
fn用法是反复尝试5秒钟,直到
为true
。如果在5秒内找不到该元素,则会产生错误代码;没有找到这样的元素。请注意,如果在等待之前满足条件(5s
),它将快速移动到,然后(…)
问题在于,与WebDriverJS/量角器中的许多方法一样,返回一个定义为“truthy”的,这使得调试此类问题变得困难
让我们通过一个例子来更好地理解
想象一下,您有以下代码,乍一看可能还不错:
var elm = $("#myid");
if (elm.isDisplayed()) {
// do smth
} else {
// do smth else
}
现在,它有一个严重的问题<因为elm.isDisplayed()
不是一个布尔值,而是一个承诺,所以永远不会到达code>do smth else
部分。即使未显示该元素,您仍将执行//do smth
部分
相反,如果需要检查要在条件表达式中使用的isDisplayed()
的值,则必须使用then()
显式解析承诺:
还有一种方法可以捕获此类错误,而无需使用和静态运行代码。如果直接在
if
条件中使用某些量角器方法,则会有一个监视程序
下面是上面代码的输出:
$ eslint test.js
test.js
2:1 warning Unexpected "isDisplayed()" inside if condition protractor/no-promise-in-if
如果你在2021年或接下来的几年
忘记。然后()。改为这样做:
it('test case', async () => {
if (await element(anyFinder).isDisplayed()) {
// Whatever if it is true (displayed)
} else {
// Whatever if it is false (not displayed)
}
});
这里可能不需要expect()
。我喜欢你的答案,谢谢。但是,在得到“if”语句之前,我不是会以一个期望错误结束,从而使测试失败吗?因为我需要它继续运行,即使它返回falseah我之前没有看到你的最后评论,没有刷新我的屏幕。我会取出expect()。是的,它成功了。谢谢你的帮助。我刚试过。删除了我不必要的expect()。嗨,当isDisplayed为false时,如果我们正在测试的站点中有一个不可见的元素,我们可以将其更改为可见吗?
it('test case', async () => {
if (await element(anyFinder).isDisplayed()) {
// Whatever if it is true (displayed)
} else {
// Whatever if it is false (not displayed)
}
});