Selenium 页面加载等待方法

发布于 2025-01-14 22:42:52 字数 577 浏览 1 评论 0原文

我一直在运行自动化测试,这些测试在我的本地环境中执行相对较快,但在 Sauce Labs 中却非常慢。

现有的Java代码似乎有两种类型的冗余页面加载等待时间

//1. set at the beginning of the test
 driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

//2. called before every UI interaction/assertion
(new WebDriverWait(wDriver, waitTime)).until((wDriver) -> {
return javascriptExecutor.execute_script("return document.readyState").equals("complete")
});

pageLoadTimeout是否是一种隐式等待,可能会导致与第二个“document.readyState”显式等待发生冲突?

这是否完全多余,或者是否存在任何边缘情况,其中其中一项检查可能不起作用,而第二种方法充当备份?

任何帮助表示赞赏 阿杰

I've been running automation tests that execute relatively quickly in my local environment but are extremely slow in Sauce Labs.

The existing Java code appears to have two types of redundant page loading wait times

//1. set at the beginning of the test
 driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

//2. called before every UI interaction/assertion
(new WebDriverWait(wDriver, waitTime)).until((wDriver) -> {
return javascriptExecutor.execute_script("return document.readyState").equals("complete")
});

Is the pageLoadTimeout a type of implicit wait that might be causing a conflict with the second "document.readyState" explicit wait?

Is this entirely redundant, or are there any edge cases where one of these checks might not work, and the secondary method acts as a backup?

Any help is appreciated
AJ

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦过后 2025-01-21 22:42:52

这里有两个不同的概念。首先,使用 w3c,您现在可以设置 页面加载会话开始时的功能中的策略。这会影响驱动程序在将控制权返回给用户之前等待的文档就绪状态。如果在页面加载超时之前未满足指定的准备状态,驱动程序应该返回错误。

请注意,这两件事仅适用于 导航事件,因此单击一个元素会导致页面加载不应触发这些。

让事情变得更复杂的是,Chromedriver 有一个未解决的错误对于错误的:

在几乎所有命令结束时等待导航完成

因此,现在 Chrome 测试实际上将等待点击时指定的文档就绪状态。

最后,请记住,对于当今的 Web,通常会通过 JavaScript 动态加载大量内容,因此即使文档准备状态为“完成”,DOM 也不太可能处于最终状态,这就是为什么鼓励您使用 < a href="https://www.selenium.dev/documentation/webdriver/waits/#explicit-wait" rel="nofollow noreferrer">显式等待您真正想要交互的事物。

There are two different concepts here. The first is that with w3c you can now set a Page Load Strategy in the capabilities at the beginning of a session. This affects what Document readiness state the driver will wait for before returning control to the user. If the specified readiness state is not satisfied before the page load timeout, the driver is supposed to return an error.

Note that both of these things only apply to navigation events, so clicking an element that results in a page load should not trigger these.

To complicate things one more level, Chromedriver has an open bug for incorrectly:

wait[ing] for navigation to complete at the end of almost all commands

So for right now Chrome tests actually will wait for the specified document readiness state on clicks.

Finally, remember that for today's web there is typically a lot of content loaded dynamically via JavaScript, so even when the document readiness state is "complete" the DOM is unlikely to be in it's final state, which is why you are encouraged to use explicit waits for the things you actually want to interact with.

墟烟 2025-01-21 22:42:52

您确定确实需要等待 document.readyState ->在每次 UI 交互/断言之前完成
如果该页面上运行了一些繁重的 JavaScript,则可能需要很长时间才能达到上述状态。
实际上,在大多数情况下,您所需要的只是当前需要访问的特定元素的准备/成熟状态。
因此,您不必等待整个页面的 readyState complete 状态,您可以等待单个特定 Web 元素的可见性或可点击性。
我相信这会减少你在这里谈论的延误。

Are you sure you really need to wait for document.readyState -> complete before every UI interaction/assertion?
In case there are some heavy JavaScripts running on that page it may take significant time to reach the above state.
Practically in most cases all what you need is readiness / maturity state of a specific element you currently need to access.
So, instead of waiting for the entire page readyState complete state you may wait for visibility or clickability of that single, specific web element.
I believe this will reduce the delays you are talking about here.

手长情犹 2025-01-21 22:42:52

pageLoadTimeout 设置在抛出错误之前等待页面完全加载的时间。

程序中的 WebDriverWait 确保您的程序等待 document.readyState至少在配置的 waitTime 内等于 “完整”万一缓慢加载的页面。

您可以在 我们是否有任何通用函数来检查 Selenium 中页面是否已完全加载< /p>

pageLoadTimeout sets the time to wait for a page to load completely before throwing an error.

The WebDriverWait in your program ensures that your program waits for the document.readyState to be equal to "complete" atleast for the configured waitTime incase of a slowly loading page.

You can find a detailed discussion in Do we have any generic function to check if page has completely loaded in Selenium

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文