Alan Krueger
Alan Krueger

Reputation: 31

race condition in Selenium waitForPageToLoad?

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?

Upvotes: 3

Views: 2654

Answers (4)

user2220334
user2220334

Reputation:

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.

Upvotes: 1

Andy
Andy

Reputation: 11

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.

Upvotes: 1

Freddy Vega
Freddy Vega

Reputation: 126

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.

Upvotes: 1

Ross Patterson
Ross Patterson

Reputation: 9570

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.

Upvotes: 1

Related Questions