问题
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
}
This is what I have tried.
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");
}
}
);
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.
Update: please note that this is a non-angular screen and I can only use browser.driver.______ apis.
回答1:
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.
来源:https://stackoverflow.com/questions/50806848/check-if-element-is-present-in-protractor