gpt4 book ai didi

scala - ScalaTest 的 Selenium DSL 的奇怪超时

转载 作者:行者123 更新时间:2023-12-04 04:37:00 25 4
gpt4 key购买 nike

我正在使用 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 秒,对吧?

另外,我发现最终 block 只尝试了一次非常有趣,并且几乎精确地花费了 10 秒。这闻起来像是某个地方发生了超时,这不是我的 PatienceConfig,因为它被设置为 5 秒后超时。

使用此解决方法,它确实有效:
click on "editEmployee"
eventually {
find(name("firstName")).value // from ScalaTest's `OptionValues`
}
textField(name("firstName")).value = "Steve"

我在 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
)
}
}

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

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

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

最佳答案

很明显,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
)
}
}
}

关于scala - ScalaTest 的 Selenium DSL 的奇怪超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46189873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com