0

I declare a button as field in following fashion:

@AndroidFindBy(name = "Schedule")
private WebElement calendarButton;

... and later I make sure it's NOT displayed because the app is in some special mode.

Assert.assertFalse(this.calendarButton.isDisplayed());

It gives me org.openqa.selenium.NoSuchElementException, but the test is failed. Any ideas how can I make such assertion?

The thing I don't want to define By condition a few times in the code, so using property is handy.

kiedysktos
  • 3,910
  • 7
  • 31
  • 40

1 Answers1

1

After some thinking I came up with following solution:

public static boolean elementIsPresent(AndroidElement element) {
    try {
        element.isDisplayed();
    } catch (org.openqa.selenium.NoSuchElementException e) {
        return false;
    }

    return true;
}

I use this method in following way:

Assert.assertFalse(elementIsPresent(this.calendarButton));

I was inspired by one of the answers in this thread.

Community
  • 1
  • 1
kiedysktos
  • 3,910
  • 7
  • 31
  • 40
  • Please don't forget to come back and accept this answer so that the question gets marked as answered. Thanks! – JeffC Nov 09 '15 at 02:52
  • What if element supposed to appear after a few seconds. I mean don't you want to use fluent wait. If the element doesn't appear in few expected seconds then return false. – paul Aug 26 '19 at 11:42