【问题标题】:Appium/Selenium - assert that element declared as field is NOT displayedAppium/Selenium - 断言声明为字段的元素不显示
【发布时间】:2015-10-28 09:23:45
【问题描述】:

我以下列方式将按钮声明为字段:

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

...后来我确保它没有显示,因为应用程序处于某种特殊模式。

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

它给了我 org.openqa.selenium.NoSuchElementException,但测试失败。有什么想法可以做出这样的断言吗?

我不想在代码中多次通过条件定义的东西,所以使用属性很方便。

【问题讨论】:

    标签: java selenium junit appium


    【解决方案1】:

    经过一番思考,我想出了以下解决方案:

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

    我使用这种方法的方式如下:

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

    this thread 中的一个答案启发了我。

    【讨论】:

    • 请不要忘记返回并接受此答案,以便将问题标记为已回答。谢谢!
    • 如果元素应该在几秒钟后出现怎么办。我的意思是你不想使用流利的等待。如果元素在预期的几秒钟内没有出现,则返回 false。
    猜你喜欢
    • 2016-01-13
    • 2018-05-11
    • 2017-06-27
    • 1970-01-01
    • 2014-04-09
    • 2021-12-08
    • 1970-01-01
    • 2017-04-21
    • 2012-10-08
    相关资源
    最近更新 更多