Please post your Web Driver questions in official Web Driver forum

Sunday, July 23, 2017

Do you check for absence of element on page

When writing automated tests we often check presence or absence of element for assertion conditions. This is usually written as - 

assertTrue("Element is missing. Bug!!!", pageObject.isSomeElementDisplayed());

And when element is not displayed then above mentioned assert statement fails.

In the similar manner we can write following assert statement to check for absence of element - 

assertFalse("Element is present. Bug!!!", pageObject.isSomeElementDisplayed());

Above mentioned assert statement would fail if element is displayed on page.

Everything looks ok, is not it. Not really, there is catch. Let's have a closer look at assertFalse statement we used above - 


assertFalse("Element is present. Bug!!!", pageObject.isSomeElementDisplayed());

This assert statement would also succeed even if webpage results in 404/500 or any other error page, since error page would also be missing the element on which we are performing assert.

So what is the solution? My recommendation is to not use check for absence of element but presence of element for assertion. What do you think of this approach?
Fork me on GitHub