power_settings_new

How to correctly assert that element not visible?

by (158 points)
+1 vote
46 views
I have element,for example //div[@class='create-button'].
My test assert that for some users without admin rights this button not visible.

Assert.False(pageObject.IsElementDisplayed());

where:

public bool IsElementDisplayed() {

     try{

           element.Displayed;

           return true;

     }

     catch(NotFoundElementException){

           return false;

     }

}

Someone in my team change button class. Locator is broken. Now method IsElementDisplayed() always return false and my test always pass. This is a false positive result. How to avoid such results?

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

by (171 points)
selected by
+1 vote
 
Best answer
I suggast you to throw NotFoundElementException instead of handling it. You should handle element not found exception saparatly. Instead of simply returning false, you should log exception and throw exception back to the method where it's called. So you can fail the test.
 
As per your requirement, you should identify if element is visible or not. But with your custom method you are explictly ignoring element presence. We already have a method called isDisplayed (), you should be using that.
 
Hope that helps!
add
...