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

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

问题描述

我有一个用Selenium测试的Web应用程序。在页面加载时运行了很多JavaScript。

这个JavaScript代码编写得不是很好但我无法改变任何东西。
所以等待一个元素出现在DOM中,而 findElement()方法不是一个选项。

我想创建一个泛型在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:


  • 运行一个JavaScript脚本表格WebDriver并存储结果 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所说,如果您对脚本有所了解,您可以观察其行为并跟踪<$ c $上的一些变量c> window 或 document 等。但是,此解决方案并非适用于所有人,可以使用o您只能在有限的页面上使用。

  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 直接获取原始和未编辑的HTML,但是:

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页面都能做到这一点。甚至Stack Overflow。你可以覆盖最常用的方法,并考虑使用它们完成的脚本,但是......你无法确定。

  • 如果脚本执行的操作不是更改了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.

有一些工具可以帮助你。即进度监听器以及 nsIWebProgressListener 和其他一些。然而,浏览器对此的支持非常糟糕。 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天全站免登陆