Is element visible?

For me and I believe for any decent automation framework is very important to have a reliable method which checks elements visibility before interaction and returns a boolean value instead of exception. Even though it’s an easy solution but for those who just started it will save a lot of time.

this is a neat and very useful method

 

public boolean isElementVisable(By by){
return driver.findElements(by).size() > 0;
}

 

However if you are using PageFactory this gets a bit tricky since WebElement doesn’t have .size() method and getSize returns coordinates which are not useful to make a check. I have tried to make a method avoiding exceptions but in this case it seems that you’ll have to handle it.

 

public boolean isElementVisable(WebElement element){
 try{
   return element.isDisplayed();
 }catch(NoSuchElementException e){
   return false;
 }
}

 

I know that it’s a bit messy solution since exceptions are involved, also webdriver can throw ElementNotVisibleException or something else so this makes it hard to control, unless you throw in a generic exception or catch-all possible ones.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s