使用ScalaTest的Selenium DSL发生奇怪的超时

jqn*_*qno 9 selenium scala scalatest

我正在用ScalaTest的Selenium DSL编写Selenium测试,我遇到了无法解释的超时问题.为了使问题更复杂,它们似乎只是在某些时候发生.

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

click on "editEmployee"
eventually {
  textField(name("firstName")).value = "Steve"
}
Run Code Online (Sandbox Code Playgroud)

我的PatienceConfig配置如下:

override implicit val patienceConfig: PatienceConfig =
    PatienceConfig(timeout = Span(5, Seconds), interval = Span(50, Millis))
Run Code Online (Sandbox Code Playgroud)

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

- 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)
Run Code Online (Sandbox Code Playgroud)

有意义的是它没有立即成功,因为click导致某些渲染,并且文本字段可能不会立即可用.但是,尝试找到它不应该花费10秒钟,对吗?

此外,我发现非常有趣的是,最终块只尝试了一次,而且几乎只需要10秒钟.这闻起来像某个地方发生了超时,而且不是我的PatienceConfig,因为它被设置为在5秒后超时.

通过此解决方法,它确实有效:

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

我在ScalaTest源代码中进行了一些挖掘,并且我注意到所有具有此问题的调用(不仅仅是textField),最终会webElement在某个时刻调用.解决方法之所以有效,是因为它没有调用webElement.webElement定义如下:

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
               )
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经将该代码复制到我的项目中并使用它,看起来构建和/或抛出异常是大部分10秒钟花费的时间.

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

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

Sig*_*hil 1

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

implicitlyWait(Span(0, Seconds))
Run Code Online (Sandbox Code Playgroud)

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

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

执行流程:

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) }
Run Code Online (Sandbox Code Playgroud)

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
                   )
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)