【问题标题】:how to improve error message for WebDriver ExpectedCondition?如何改进 WebDriver ExpectedCondition 的错误消息?
【发布时间】:2012-06-04 14:25:24
【问题描述】:

我有一个这样的实用方法:

public void verifyTitle(int secsToWait) {
    new WebDriverWait(driver, secsToWait)
            .until(ExpectedConditions.titleIs(title));
}

失败时,消息为:

org.openqa.selenium.TimeoutException

等待标题 2 秒后超时:这里有一些标题 构建信息:版本:'2.2.1',修订:'16551',时间:'2012-04-11 21:42:35' 系统信息:os.name:'Linux',os.arch:'i386',os.version: '3.0.0-16-generic', java.version: '1.7.0_04' 驱动信息: driver.version:未知

我找不到提供更好错误消息的 API。我想要的是:

等待标题 2 秒后超时:这里有一些标题 (但标题是:其他标题)

我可以使用如下所示的 try/catch 来做到这一点,但也许还有其他选择?

public void verifyTitle(int secsToWait) {
    try {
        new WebDriverWait(driver, secsToWait)
                .until(ExpectedConditions.titleIs(title));
    }
    catch (TimeoutException e) {
        throw new AssertionError(String.format("Expected page title [%s] but was [%s]", title, driver.getTitle()));
    }
}

【问题讨论】:

  • 我看不出捕获异常有什么问题。您正在捕获它并使用更好的信息重新抛出它,这就是您通常对重新抛出异常所做的事情。
  • 没有问题 - 我在问是否有一些没有 try/catch 的更干净的方法(好的,我也可以使用 catch-exception 库)
  • 就个人而言,不,我没有看到更好的解决方案,抱歉!

标签: java testing selenium webdriver


【解决方案1】:

使用withMessage() 方法:

WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.withMessage("Any message you want");
wait.until(ExpectedConditions.titleIs(title));

【讨论】:

    【解决方案2】:

    您还可以与 JUnit 的 assertEquals 进行比较,这将提供更好的消息

    如:

    org.junit.ComparisonFailure: expected:<[something]> but was:<[differentthing]>
    

    【讨论】:

      【解决方案3】:

      当你想传递一个场景时调用这个。

      Public CustomWebDriverException(String msg, WebDriver custWebDriver) {

          super(msg);
      
          this.driver = custWebDriver;
      

      }

      有catch时调用这个构造函数

      public CustomWebDriverException(Throwable e, WebDriver selenium) {

          super(e.getMessage());
          this.driver = selenium;
      }
      

      让我更多地了解您的设置和其他东西......比如框架,因为您可以使用 testNg 或 maven 管理的东西很少

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        • 2022-10-16
        相关资源
        最近更新 更多