检查量角器中是否存在元素 [英] check if element is present in protractor
问题描述
我有一个角度应用程序,用户可以根据她/他的浏览器缓存获得不同的登录屏幕(非角度).我正在使用量角器编写端到端测试用例,我想设置一个类似这样的条件
I have an angular application where user can get a different login screen(non-angular) based on her/his browser cache. I am writing end to end test cases using protractor and I want to put a condition something like this
if(this element is present)
{
click it
}
else check if this element is pressent
{
click it
}
这是我试过的.
browser.driver.findElements(by.id('element1')).then(function(result) {
if (result) {
browser.driver.findElement(by.id('element1')).click();
}
else {
browser.driver.findElement(by.id('element2')).sendKeys("username");
}
}
);
我的问题是,如果if condition"中的元素不存在,那么我会收到错误Failed: no such element: Unable to locate element:
".我不希望我的测试用例失败,因为元素可能不存在,然后它应该转到 else 块.我在stackoverflow上看到了很多问题,但没有任何帮助.我真的很感激这里的任何帮助.
My issue is that if the element in the 'if condition' is not present then I am getting an error ' Failed: no such element: Unable to locate element:
'. I don't want my test cases to fail because it's possible that the element is not present and then it should go to else block.
I have seen so many questions on stackoverflow but nothing helped me. I would really appreciate any help here.
更新:请注意,这是一个非角度屏幕,我只能使用 browser.driver.______ apis.
Update: please note that this is a non-angular screen and I can only use browser.driver.______ apis.
推荐答案
您走在正确的轨道上.因为 findElements 返回一个数组,所以您只需要将 if (result)
更改为 if (result.length>0)
.使用 FindElements,什么也找不到不是错误.
You were very much on the right track. Because findElements returns an array, you just needed to change what you had if (result)
to if (result.length>0)
. With FindElements, finding nothing is not an error.
这篇关于检查量角器中是否存在元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!