Selenium IE9 WaitForPageToLoad Fix

Posted on Friday, August 26, 2011

There appears to be an issue in Selenium that causes WaitForPageToLoad to act improperly in IE9.

To resolve the issue copy the following code into a file called "user-extensions.js":

Reference that file when you start the Selenium server using the "-userExtensions" option:

java -jar selenium-server-standalone-2.4.0.jar -userExtensions user-extensions.js

Now for the explanation...

I noticed that when I experienced the WaitForPageToLoad timeout, the browser logs showed an unusual error message coming from IEBrowserBot.windowclosed:

debug(1314123427103): _isSamePage: sameLoc: true
debug(1314123427103): _isSamePage: sameHref: true
debug(1314123427103): _isSamePage: markedLoc: true
debug(1314123427824): replaced getReadyState used
debug(1314123427824): pageUnloading = true!!!!
debug(1314123427824): getReadyState returning loading
debug(1314123427824): pollForLoad continue (selenium1314123422376): undefined
debug(1314123427824): runScheduledPollers DONE
debug(1314123427843): runScheduledPollers
debug(1314123427843): IEBrowserBot.pollForLoad: selenium1314123422376
debug(1314123427843): pollForLoad original (selenium1314123422376): http://localhost:8000/weblo/Gfe.aspx
debug(1314123427899): IEBrowserBot.windowClosed: couldn't read win.document, assume closed:  (this.pageUnloading=true)
debug(1314123427899): pollForLoad WINDOW CLOSED (selenium1314123422376)
debug(1314123427899): runScheduledPollers DONE
debug(1314123427900): runScheduledPollers
debug(1314123427900): runScheduledPollers DONE
debug(1314123427919): runScheduledPollers
debug(1314123427919): runScheduledPollers DONE

Looking at browser-bot.js, I noticed that the condition with the logged error "couldn't read win.document, assume closed" is the only one that returns true:

I changed that to return false and included the entire function as a user extension, thus overwriting the original:

Since then, I have run over a thousand tests (on IE9) and not one has faltered because of WaitForPageToLoad.

Labels: , , , , , ,


Comments:
Thanks!

You just saved me a boatload of debugging, and our selenium test suite execution time dropped to about half on IE9.
 
No problem! Glad I could help.
 
If it is acceptable to customize the client driver, here is the Python implementation for your refernece:

def open(self):
timeout = self.get_eval('this.defaultTimeout')
self.set_timeout(0)
self.do_command("open", [url,ignoreResponseCode])
self.set_timeout(timeout)
self.wait_for_page_to_load(timeout)

def wait_for_page_to_load(self,timeout):
# self.do_command("waitForPageToLoad", [timeout,])
import time
end = time.time() + int(float(timeout) / 1000)
while time.time() < end:
if self.get_eval('window.document.readyState') == 'complete': return
time.sleep(2)
raise Exception('Time out after %sms' % timeout)

I just use DOM attribute `document.readyState` to determine if the page is fully loaded.

http://imsardine.simplbug.com/note/selenium/rc/ie-onload-timeout_EN.html (IE 9+ intermittently throws a timeout error even the page is fully loaded), for more details.
 
Nice fix! Thank you.
 
This is straight up awesome and saved me tons of time. Thanks so much!
 
Thank Jeremy alot! The solution (customize the client driver) works fine :)
 

Post a Comment

Subscribe to Post Comments [Atom]





<< Home