Selenium WebDriver:等待带有 JavaScript 的复杂页面加载 [英] Selenium WebDriver: Wait for complex page with JavaScript to load

查看:32
本文介绍了Selenium WebDriver:等待带有 JavaScript 的复杂页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序要使用 Selenium 进行测试.页面加载时会运行大量 JavaScript.
这段 JavaScript 代码写得不是很好,但我无法更改任何内容.因此,使用 findElement() 方法等待元素出现在 DOM 中不是一种选择.
我想在 Java 中创建一个通用函数来等待页面加载,一个可能的解决方案是:

I have a web application to test with Selenium. There is a lot of JavaScript running on page load.
This JavaScript code is not so well written but I can't change anything. So waiting for an element to appear in the DOM with findElement() method is not an option.
I want to create a generic function in Java to wait for a page to load, a possible solution would be:

  • 从 WebDriver 运行 JavaScript 脚本并将 document.body.innerHTML 的结果存储在字符串变量 body 中.
  • body 变量与之前版本的 body 进行比较.如果它们相同,则设置递增计数器 notChangedCount 否则将 notChangedCount 设置为零.
  • 稍等片刻(例如 50 毫秒).
  • 如果页面在一段时间内没有改变(例如 500 毫秒),那么 notChangedCount >= 10 然后退出循环,否则循环到第一步.
  • run a JavaScript script form WebDriver and store the result of document.body.innerHTML in a string variable body.
  • compare the body variable to the previous version of body. if they are the same then set increment a counter notChangedCount otherwise set notChangedCount to zero.
  • wait for a litte time (50 ms for example).
  • if the page has not changed for some time (500 ms for example) so notChangedCount >= 10 then exit the loop otherwise loop to the first step.

您认为这是一个有效的解决方案吗?

Do you think it's a valid solution?

推荐答案

如果有人真的知道一个通​​用且始终适用的答案,它就会在很久以前无处不在实施,并且会让我们的生活变得如此容易多了.

If anyone actually knew a general and always-applicable answer, it would have been implemented everywhere ages ago and would make our lives SO much easier.

你可以做很多事情,但每一项都有问题:

There are many things you can do, but every single one of them has a problem:

  1. 正如 Ashwin Prabhu 所说,如果你很了解这个脚本,你可以观察它的行为并在 windowdocument 等上跟踪它的一些变量.这个但是,该解决方案并不适合所有人,并且只能由您在有限的一组页面上使用.

  1. As Ashwin Prabhu said, if you know the script well, you can observe its behaviour and track some of its variables on window or document etc. This solution, however, is not for everyone and can be used only by you and only on a limited set of pages.

通过观察 HTML 代码以及它是否有一段时间没有更改,您的解决方案还不错(此外,还有 一种方法通过WebDriver),但是:

Your solution by observing the HTML code and whether it has or hasn't been changed for some time is not bad (also, there is a method to get the original and not-edited HTML directly by WebDriver), but:

  • 实际断言页面需要很长时间,并且可能会显着延长测试时间.
  • 您永远不知道正确的时间间隔是多少.该脚本可能正在下载需要超过 500 毫秒的大型内容.我们公司的内部页面上有几个脚本在IE中需要几秒钟.您的计算机可能暂时缺乏资源 - 假设防病毒软件将使您的 CPU 充分工作,那么即使对于不复杂的脚本,500 毫秒也可能太短.
  • 有些脚本永远不会完成.它们会延迟调用自己 (setTimeout()) 并一次又一次地工作,并且每次运行时都可能更改 HTML.说真的,每个Web 2.0"页面都这样做.甚至堆栈溢出.您可以覆盖最常用的方法并将使用它们的脚本视为已完成,但是……您不能确定.
  • 如果脚本除了更改 HTML 之外还执行其他操作怎么办?它可以做成千上万的事情,而不仅仅是一些 innerHTML 乐趣.
  • It takes a long time to actually assert a page and could prolong the test significantly.
  • You never know what the right interval is. The script might be downloading something big that takes more than 500 ms. There are several scripts on our company's internal page that take several seconds in IE. Your computer may be temporarily short on resources - say that an antivirus will make your CPU work fully, then 500 ms may be too short even for a noncomplex scripts.
  • Some scripts are never done. They call themselves with some delay (setTimeout()) and work again and again and could possibly change the HTML every time they run. Seriously, every "Web 2.0" page does it. Even Stack Overflow. You could overwrite the most common methods used and consider the scripts that use them as completed, but ... you can't be sure.
  • What if the script does something other than changing the HTML? It could do thousands of things, not just some innerHTML fun.

有一些工具可以帮助您解决这个问题.即 Progress ListenersnsIWebProgressListener 和其他一些.然而,浏览器对此的支持非常糟糕.Firefox 从 FF4 开始尝试支持它(仍在发展中),IE 在 IE9 中有基本支持.

There are tools to help you on this. Namely Progress Listeners together with nsIWebProgressListener and some others. The browser support for this, however, is horrible. Firefox began to try to support it from FF4 onwards (still evolving), IE has basic support in IE9.

我想我很快就能想出另一个有缺陷的解决方案.事实是 - 何时说现在页面已完成"没有明确的答案,因为永恒的脚本在做他们的工作.选择最适合您的那个,但要注意它的缺点.

And I guess I could come up with another flawed solution soon. The fact is - there's no definite answer on when to say "now the page is complete" because of the everlasting scripts doing their work. Pick the one that serves you best, but beware of its shortcomings.

这篇关于Selenium WebDriver:等待带有 JavaScript 的复杂页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆