Selenium waitForPageToLoad 中的竞争条件?

发布于 2024-09-08 02:05:28 字数 571 浏览 1 评论 0原文

我们使用 Selenium 对基于 Java Spring MVC 的网站进行冒烟测试。我们使用 JUnit 和 Java Selenium 客户端调用它。大多数时候,我们的测试工作正常,但我们往往会在 selenium.waitForPageToLoad 中明显随机的地方出现超时。 (也就是说,多次运行测试套件,超时的位置和次数会有很大差异。)

在前台的服务器上运行它,我可以观察测试的执行情况。当发生其中一种超时时,等待之前的 selenium.click 已经发生,但等待似乎没有注意到。右键单击该页面并选择“刷新”即可清除阻塞。

对我来说,这表明单击和 waitForPageToLoad 之间存在竞争条件。其他人有与 waitForPageToLoad 相关的问题,但似乎没有描述竞争条件。有些人建议前面的 selenium.click 调用存在问题,但我在这里没有看到这一点。还有其他人看到我所观察到的吗?

我见过许多不使用 waitForPageToLoad 的建议,但解决方法通常建议使用 waitForElementPresent 或手动轮询元素是否存在。对于我们的应用程序,似乎需要使用仅由测试使用的 ID 来检测 HTML。此方法是否有非侵入性的解决方法?

We're using Selenium for smoke-testing a Java Spring MVC based website. We invoke it using JUnit and the Java Selenium client. Much of the time, our tests work fine, but we tend to get timeouts in selenium.waitForPageToLoad in apparently random places. (That is, run the test suite multiple times and the location and number of timeouts will vary greatly.)

Running it with the server in the foreground, I can watch the tests executing. When one of these timeouts occurs, the selenium.click that preceded the wait has occurred but the wait doesn't appear to notice. Right-clicking on the page and selecting Refresh appears to clear the blockage.

To me, this suggests there's a race condition occurring between the click and waitForPageToLoad. Others have related problems with waitForPageToLoad but don't appear to describe a race condition. Some have suggested problems with the preceding selenium.click call, but I'm not seeing that here. Has anyone else seen what I've observed?

I've seen a number of suggestions to not use waitForPageToLoad, but the work-arounds often suggest using waitForElementPresent or manually polling for element presence. With our application that would seem to require instrumenting the HTML with IDs that would only be used by the tests. Is there a non-intrusive work-around to this method?

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

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

发布评论

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

评论(4

原来是傀儡 2024-09-15 02:05:28

Selenium 的 waitForPageToLoad() 函数非常可靠 - 我不知道为什么有人会建议你不应该使用它。它在 Firefox 中工作得特别好,Selenium RC 添加了一个插件来帮助它检测页面就绪状态,但它在 Internet Explorer 中也工作得很好。我们在测试套件中使用它数千次,而且从未失败。

您应该问的问题是“为什么我的页面等待超时?”这个问题的最佳答案是捕获测试中涉及的网络流量并在失败时分析结果。如果您有可重复的测试用例,WireShark 或 Fiddler 是不错的工具。如果没有,您可能会在异常处理程序中使用 Selenium RC 的 captureNetworkTraffic() 方法来保存失败时的流量。

Selenium's waitForPageToLoad() function is quite reliable - I don't know why anyone would suggest that you shouldn't use it. It works particularly well in Firefox, where Selenium RC adds a plug-in that helps it detect the page-ready state, but it works just fine in Internet Explorer as well. We use it several thousand times in our test suite, and it never fails.

The question you should be asking is "Why is my page-wait timing out?" That's a problem best answered by capturing the network traffic involved in a test and analyzing the results when it fails. If you have a repeatable test case, WireShark or Fiddler are good tools. If not, you may have some luck using Selenium RC's captureNetworkTraffic() method in your exception handler to save the traffic on failure.

吹泡泡o 2024-09-15 02:05:28

我以前遇到过这个问题,等待页面加载经常发生在特定页面上,但当我们检查日志和屏幕截图时,一切似乎都很正常。

@Ross,我同意你的“深入挖掘”的说法。我最初也认为这是一个竞争条件,但在我的例子中,结果是一些 JavaScript 在页面上运行得相当高,并且它正在与不是我们托管的服务器做一些事情,所以比平时花费了更长的时间完成,有时。

为了缓解这个问题,我所做的就是在仅针对此页面的 selenium 调用和使用 selenium 命令 set_speed(time_in_ms) 的特定交互之间添加额外的时间。

$self->{sel}->open_ok("/", $self->{browser});
# Inject 2000 ms in between selenium calls
$self->{sel}->set_speed("2000");

然后将其设置回我们通常运行的值,1000 毫秒。

I have encountered this issue before, wait for page to load would occur often on a specific page but when we checked the logs and screen captures everything seem normal.

@Ross, i agree with your "dig deeper" statement. I also initially thought this to be a race condition, but in my case it turned out to be some JavaScript that was running pretty up high on the page and it was doing stuff with a server not hosted by us so it took longer than usual to complete, on occasion.

What I did to alleviate this is add additional time between selenium calls only for this page and this specific interaction using the selenium command set_speed(time_in_ms).

$self->{sel}->open_ok("/", $self->{browser});
# Inject 2000 ms in between selenium calls
$self->{sel}->set_speed("2000");

Then set it back to what we usually run, 1000 ms.

不必在意 2024-09-15 02:05:28

我在 Selenium RC 2.25/Windows 7/IE 9 中遇到了同样的问题。

两个重要步骤:

  • 在管理员上下文中从命令提示符调用 Java 包(右键单击 cmd.exe -> 以管理员身份运行
  • 以兼容模式运行 Internet Explorer

这为我解决了这个问题。

I encountered the same problem with Selenium RC 2.25/Windows 7/IE 9.

Two important steps:

  • Call the Java package from the command prompt in administrator context (right click cmd.exe -> run as administrator)
  • Run the Internet Explorer in compatibility mode

That solved the issue for me.

眸中客 2024-09-15 02:05:28

我最近遇到了这个问题。就我而言,在页面加载后,window.location 几乎立即更改为锚点(例如 window.location =“#myAnchor”)。也许 URL 更改欺骗了 waitForPageToLoad ,使其认为加载的页面不是预期的页面,因此最终失败。

问题的随机部分是由触发此 window.location 更改的短时间(100ms)“setTimeout”引起的。

感谢 Selenium,丑陋的代码被发现并变成同步的。

I encountered this issue recently. In my case, the window.location was changed to an anchor (e.g. window.location = "#myAnchor") almost immediately after the page loaded. Maybe that URL change tricked the waitForPageToLoad into thinking that the page loaded was not the expected one, and therefore eventually fail.

The random part of the problem was caused by a short (100ms) "setTimeout" triggering this window.location change.

Thanks to Selenium, ugly code was found and turned synchronous.

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