ScalaTest 的 Selenium DSL 的奇怪超时

Strange timeout with ScalaTest's Selenium DSL

我正在使用 ScalaTest 的 Selenium DSL 编写 Selenium 测试,我 运行 遇到无法解释的超时。更复杂的是,它们似乎只是偶尔发生。

每当我在页面加载或一些 Javascript 渲染后访问元素时,就会出现问题。它看起来像这样:

click on "editEmployee"
eventually {
  textField(name("firstName")).value = "Steve"
}

我的 PatienceConfig 配置如下:

override implicit val patienceConfig: PatienceConfig =
    PatienceConfig(timeout = Span(5, Seconds), interval = Span(50, Millis))

测试失败并出现以下错误:

- should not display the old data after an employee was edited *** FAILED ***
  The code passed to eventually never returned normally. Attempted 1 times over 10.023253653000001 seconds.
  Last failure message: WebElement 'firstName' not found.. (EditOwnerTest.scala:24)

它不会立即成功是有道理的,因为 click 会导致一些渲染,并且文本字段可能不会立即可用。但是,尝试找到它应该不需要 10 秒,对吗?

另外,我觉得非常有趣的是,最终块只尝试了一次,而且几乎正好用了 10 秒。这闻起来像是某处发生了超时,这不是我的 PatienceConfig,因为它被设置为 5 秒后超时。

使用此解决方法,它确实有效:

click on "editEmployee"
eventually {
  find(name("firstName")).value // from ScalaTest's `OptionValues`
}
textField(name("firstName")).value = "Steve"

我在 ScalaTest 源代码中做了一些挖掘,我注意到所有调用都有这个问题(不仅仅是 textField),最终在某个时候调用 webElement。解决方法起作用的原因是因为它不调用 webElementwebElement 定义如下:

def webElement(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): WebElement = {
  try {
    driver.findElement(by)
  }
  catch {
    case e: org.openqa.selenium.NoSuchElementException =>
      // the following is avoid the suite instance to be bound/dragged into the messageFun, which can cause serialization problem.
      val queryStringValue = queryString
      throw new TestFailedException(
                 (_: StackDepthException) => Some("WebElement '" + queryStringValue + "' not found."),
                 Some(e),
                 pos
               )
  }
}

我已经将该代码复制到我的项目中并进行了试验,看起来构建 and/or 抛出异常是花费 10 秒的大部分时间。

(EDIT 澄清:我实际上看到代码实际上在 catch 块中花费了 10 秒。隐式等待设置为 0,此外,如果我删除catch 块一切都按预期工作。)

所以我的问题是,我该怎么做才能避免这种奇怪的行为?我不想一直插入对 find 的多余调用,因为它很容易被遗忘,尤其是因为正如我所说,错误只在某些时候发生。 (我无法确定该行为何时发生以及何时不发生。)

我不知道 ScalaTest 的细节,但当您将隐式和显式等待混在一起时,通常会发生这种奇怪的超时。

driver.findElement 在内部使用隐式等待。并且根据指定的显式等待超时,您可能会面临将两者相加的问题。

理想情况下,应将隐式等待设置为 0 以避免此类问题。

很明显 textField(name("firstName")).value = "Steve" 最终调用了 WebElement,正如您所发现的那样。 由于操作中的问题发生在涉及 web 元素的地方(这反过来意味着涉及 webdriver),我认为可以安全地假设该问题与 Web 驱动程序上的隐式等待有关。

implicitlyWait(Span(0, Seconds))

以上应该可以很好地解决问题。此外,使隐式等待为 0 是一种不好的做法。任何网页都可能有一些加载问题。页面加载由 Selenium 在其等待条件之外处理。但是缓慢的元素加载(可能是由于 ajax 调用)可能会导致失败。我通常保留 10 秒作为我的标准隐式等待。对于需要更多等待的场景,可以使用显式等待。

def implicitlyWait(timeout: Span)(implicit driver: WebDriver): Unit = {
driver.manage.timeouts.implicitlyWait(timeout.totalNanos, TimeUnit.NANOSECONDS)
}

执行流程:

name("firstName") 最终的值为 Query {Val by = By.className("firstName") }.

def name(elementName: String): NameQuery = new NameQuery(elementName)

case class NameQuery(queryString: String) extends Query { val by = By.name(queryString) }

Query 被馈送到 textField 方法,该方法调用 Query.webElement 如下。

def textField(query: Query)(implicit driver: WebDriver, pos: source.Position): TextField = new TextField(query.webElement)(pos)

sealed trait Query extends Product with Serializable {

    val by: By

    val queryString: String

    def webElement(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): WebElement = {
      try {
        driver.findElement(by)
      }
      catch {
        case e: org.openqa.selenium.NoSuchElementException =>
          // the following is avoid the suite instance to be bound/dragged into the messageFun, which can cause serialization problem.
          val queryStringValue = queryString
          throw new TestFailedException(
                     (_: StackDepthException) => Some("WebElement '" + queryStringValue + "' not found."),
                     Some(e),
                     pos
                   )
      }
    }
  }