【问题标题】:How to verify if toast message is displayed?如何验证是否显示 toast 消息?
【发布时间】:2021-11-06 17:09:35
【问题描述】:

这里的场景是我正在保存数据,成功保存后会显示一条 toast 消息,但如果我说将 toast 消息保存为 WebElement 并验证 .isDisplayed() 将不起作用,因为我认为它会是显示为元素无论如何都会出现在 DOM 中,所以还有其他方法可以使用 toast 消息断言数据保存在网格中吗?

【问题讨论】:

    标签: java selenium selenium-webdriver automation ui-automation


    【解决方案1】:

    这是一个验证我在项目中使用的 toast 显示的示例:

    Assert.assertTrue(validateToastMessageAppearance("invalid Entitlement"));
    

    其中validateToastMessageAppearance方法定义为

    public boolean validateToastMessageAppearance(String message){
        waitForElementToBeVisible(String.format(toastMessage,message));
        return true;
    }
    

    方法waitForElementToBeVisible定义为:

    public boolean waitForElementToBeVisible(By.xpath(xpath)) {
        try {
        wait.until(ExpectedConditions.visibilityOfElementLocated(xpath));
        return true;
        }catch (Throwable t){
            return false;
        }
    }
    

    toastMessage 被定义为

    public String toastMessage = toast + textElement;
    

    在哪里

    public String toast =  "//div[contains(@class,'ToastContent')]";
    

    public String textElement = "//*[contains(text(),'%s')]";
    

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 2015-02-12
      • 2018-09-12
      • 2013-01-24
      • 2016-10-31
      • 2011-12-09
      • 2014-07-04
      • 1970-01-01
      相关资源
      最近更新 更多