Sunday, June 24, 2012

Migrating From Selenium 1.0 to 2 - an Alternative to waitForPageToLoad()

We have been developing a selenium 1.0 based automation suite and an important decision we had to take recently was to support modern browsers. Modern in the sense, Firefox 3.6 is not any more modern, and so is Firefox 10.0.
This forced us to upgrade our suite to Selenium 2.0. Because of the way the suite is designed, upgrading the entire suite is just a matter of rewriting a single class, and ideally it should work.  All went fine, until we came up with the waitForPageToLoad() function of Selenium 1.0, which does not have an equivalent counterpart in Selenium 2.0.
Two recommended solutions to this issue in the official selenium documentation were

  1. Wait for an element instead of a page load - This doesn't work because this requires a major rework. We need to find all wait statements, and decide what to wait for. There are instances where clicking on a button may lead to different pages based on the other choices made in the page. This makes this solution more complex.
  2. Wait for certain amount of time if an element is not found - This is simple, and we tried it out. It creates a lot of unnecessary waiting for every elements, even when the test case expects the element to be not present. Eg: assertFalse($selenium->isElementPresent("ElementID"));  takes 30 seconds (default timeout) to pass. The suite which originally ran at 1.5 hours, took more than 5 hours to complete.

The current solution we are trying out, may be the final one that we would settle for, is none of the above two. We implemented waitForPageToLoad() as waiting for document.readyState status to become "complete".

$state = $session->execute(array( 'script' => 'return window.document.readyState', 'args' => array(), ));
if ($state == "complete" || $timedOut ){
return;
}

So far the results are positive. I hope this tip would help anyone migrating from selenium 1.0 to 2.

No comments: