Super newbie here.
I am testing a page that either returns a list of tweets, or if none are available, a no results page. So I have two possible acceptable outcomes. I am trying to write an assert in Selenium 2 that tests for either element, but if neither appears, return an error.
I am using the page object model and I wrote the following:
Assert.assertTrue((iu.twitterUsername().isDisplayed()) || (iu.noData().isDisplayed()), "Page is not loading")
However, I am getting an element not found on the first part of my OR statement when the no data page is displayed. I thought the point of ‘isDisplayed’ was to check if the element is there. Why am I getting an element not found error? Obviously its not there, and I want to move onto the second part of my OR statement.
Is there a better way to write this when there are two possible acceptable results?
The WebElement.isDisplayed() method doesn’t tell you whether or not an element is there; it tells you whether it is displayed on the page (as opposed to being hidden).
You can use the WebDriver.findElement() method to test whether or not an element has loaded; it will throw a NoSuchElementException if it hasn’t. Your method might look like this:
You can alter how long WebDriver waits for the element to load with something like the following, which changes it to five seconds:
Alternatively, you can create a method that will poll for the element until a timeout is reached.