Selenium 页面加载等待方法
我一直在运行自动化测试,这些测试在我的本地环境中执行相对较快,但在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有两个不同的概念。首先,使用 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:
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.
您确定确实需要等待
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.
pageLoadTimeout 设置在抛出错误之前等待页面完全加载的时间。
程序中的 WebDriverWait 确保您的程序等待
document.readyState
至少在配置的
waitTime
内等于 “完整”万一缓慢加载的页面。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 configuredwaitTime
incase of a slowly loading page.