How to return more than one isDisplayed results inside a single method?



  • 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();
        }
    



  • 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() {
    try{
    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)




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2