I am trying to create a single method to return some boolean values that check whether some of the elements are visible on a webpage.

In that case, I am getting an "unreachable statement" error for the second statement.

So I am looking for a workaround to this problem other than writing single methods for every element separately so that I can Assert whether a whole section of elements is visible by only one method.

This is the code snippet I tried to use:

   public boolean verifyDescriptionInfo() {
        return element(name).isDisplayed();
        return element(age).isDisplayed();

1 Answer 1


whether a whole section of elements is visible by only one method

Your can name change the name of your method (and create others) to describe what it does:

public boolean isDescriptionInfoDisplayed() {
    return isNameDisplayed() && isAgeDisplayed();

private boolean isNameDisplayed() {
    return element(name).isDisplayed();

private boolean isAgeDisplayed() {
    return element(age).isDisplayed();

This way, you are expressing clearly your criteria for what does it means for the Description Info component to be displayed. If someday the criteria changes to either one of Name or Age to be displayed, you simply change && to ||.

Naturally, we don't know what does element(age) returns, but this object's isDisplayed method should have defensive programming:

class Element {


  public boolean isDiplayed() {
         return element(name).isDisplayed();
     } catch(NosuchElementException e){
       return false;


(But note that this is not concern of the class that has isDescriptionInfoDisplayed, this defensive programming is a low-level detail shared between all Elements)

  • Thank you for this. I am used this and got the results I wanted Mar 14, 2022 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.