Appium/Selenium - assert that element declared as field is NOT displayed

ε祈祈猫儿з 提交于 2020-01-06 20:25:22

问题


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.



来源:https://stackoverflow.com/questions/33387189/appium-selenium-assert-that-element-declared-as-field-is-not-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!