gpt4 book ai didi

groovy - Geb 的一般问题(StaleElementReferenceException 和等待超时)

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

根据“Geb 之书”,我开始绘制我们门户网站的网页。我更喜欢使用静态内容闭包 block 中定义的变量,然后在页面方法中访问它们:

static content = {
buttonSend { $("input", type: "submit", nicetitle: "Senden") }
}
def sendLetter() {
waitFor { buttonSend.isDisplayed() }
buttonSend.click()
}

不幸的是,有时我会收到 Geb 等待超时异常(60 秒后),或者更糟糕的是,我会收到众所周知的“StaleElementReferenceException”。

当使用“isEnabled”而不是“isDisplayed”时,我可以避免等待超时,但对于“StaleElementReferenceException”,我只能应用以下解决方案:

def sendLetter() {
waitFor { buttonSend.isEnabled() }
try {
buttonSend.click()
} catch (StaleElementReferenceException e) {
log.info(e.getMessage())
buttonSend.click()
}
}

我想,这个解决方案不是很好,但我无法应用另一篇文章中描述的显式等待。因此,我有一些一般性问题:

  • 当页面是动态的时,我应该避免使用静态内容定义吗?
  • Geb 在什么时间或事件刷新其 DOM?如何触发 DOM 刷新?
  • 为什么我在使用 CSS 选择器时仍然收到“StaleElementReferenceException”?

我会感激每一个有助于理解或解决这个问题的提示。最好有一个简单的代码示例,因为我还是个初学者。谢谢!

最佳答案

如果您在页面类上定义了 at 检查,则页面将首先验证该条件并等待前 n 秒。这是在您的 gebConfig 文件中分配的。默认值为 30 秒。

static at = {
waitFor { buttonSend.isDisplayed() }
}

因此,一旦您通过测试调用您的页面“to”方法,或者您在页面上使用它的任何方法,都会等待然后执行您的页面操作。

to MyPage
buttonSend.click()

当页面是动态的时,我应该避免使用静态内容定义吗?

No. Actually, the static definitions are of closures. So what is actually happening is each time you make use of that Pages static components you are calling a closure which is run dynamically on the current page(collection of webElements). Understanding this is key to using Geb and discovering the problems you will run into.

Geb 在什么时间或事件刷新它的 DOM?如何触发 DOM 刷新?

When you call: to, go, at, click ,withFrame(frame, page), withWindow and browser drive methods it will refresh the current set of WebElements. Geb has a nice collection of utiliities to make switching between pages and waiting for page manipulations easy. Note: Geb is actually built on WebDriver WebElements.

为什么我在使用 CSS 选择器时仍然收到“StaleElementReferenceException”?

It is possible the page hasn't finished loading, has been manipulated with ajax calls or has been refreshed in some other way. Sometimes an 'at' PAGE method call can fix these issues. They are for me most common when using frames as Geb seems to become confused between pages and frames a little. There are workarounds.

简而言之,如果您使用页面模式,您可以使用您使用静态内容、at 和 url 闭包定义的 Page 类轻松切换预期页面,使用以下内容:

  • 到(页面)
  • 在(页面)
  • Navigator.click(页面)
  • withFrame(frame, Page) { }

关于groovy - Geb 的一般问题(StaleElementReferenceException 和等待超时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22683330/

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