Appium/Selenium - assert that element declared as

2019-09-01 09:02发布

问题:

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.

回答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.