Reputation: 80
In my test in Protractor I am expecting to see element1 Or element2 at the end, I found a "solution" which works only if element1 is displayed and element2 is not on the page
expect(element1.isDisplayed() || element2.isDisplayed()).toBeTruthy();
In case if element1 is not on the page but element2 is there - i am getting an error:
Failed: No element found using locator: ...(element1 locator)
I cannot write test where only one expectation is possible, this is test with Data and in my case 2 results are possible. How can i validate if one of two elements isDisplayed on the page?
Upvotes: 1
Views: 347
Reputation: 13712
I don't think Jasmine
understand the expression element1.isDisplayed() || element2.isDisplayed()
, so ever the two elements are not display, the assertion still pass.
The ExpectedConditions API supply and/or
operation.
var EC = protractor.ExpectedConditions;
var condition = EC.or(EC.visibilityOf(element1), EC.visibilityOf(element2));
expect(condition()).toBeTruthy();
Upvotes: 3
Reputation: 362
First of all, check that isDisplayed() is the correct function you want to use.
Is your element actually rendered but just set to be hidden, or is it not even in the page? If it is just hidden, then it's OK to use isDisplayed(). If it is not in the page, then you need to find a better way to wrap the check for your elements .isDisplayed() or find a more appropriate function even.
What happens there is that your test tries to determine if element1.isDisplayed() is true or false and instead it gets a 'No element found using locator: element1'. Let's say, if your locator is id='my-element-1' it simply means that webdriver tried to locate in your rendered page an element with id 'my-element-1' and found nothing, whereas by using element1.isDisplayed() you are asking webdriver to tell you whether element1 visibility is hidden or not.
Bottomline, isDisplayed returns a promise and you gotta handle it appropriately or else you're going to be in trouble whenever it returns something other than your expectation.
Upvotes: 1