Selenium Open function is waiting for now?
When I call the Open ("[some URL]") method in Selenium, it waits until all resources (images, CSS, JS) are loaded. But how does it know that the browser is made to request resources?
Here is my driver code snippet (in C #):
DefaultSelenium selenium = new DefaultSelenium(
"localhost", 4444, "*iexplore", "http://www.stackoverflow.com");
// open my profile page
selenium.Open("http://stackoverflow.com/users/111934/jeff-meatball-yang");
The URL above is for example only. In the case where I have an AJAX call wrapped in a very long delay in setTimeout()
, called in the body event handler onload
, Selenium won't wait for that AJAX call before continuing ... I need to manually tell Selenium to wait with WaitForPageToLoad(timeout)
.
So how does Selenium detect when a normal page has loaded (no AJAX)?
source to share
I believe it expects location.href to be available, which means that if AJAX is triggered after that, you'll need to use waitForCondition as well.
selenium.open("http://www.example.com/");
selenium.waitForCondition("var value = selenium.getText('id=pageLoadStatus'); value == 'Loaded'", "60000");
Below is the code of the current page load from the Selenium core:
this.recordPageLoad = function(elementOrWindow) {
LOG.debug("Page load detected");
try {
if (elementOrWindow.location && elementOrWindow.location.href) {
LOG.debug("Page load location=" + elementOrWindow.location.href);
} else if (elementOrWindow.contentWindow && elementOrWindow.contentWindow.location && elementOrWindow.contentWindow.location.href) {
LOG.debug("Page load location=" + elementOrWindow.contentWindow.location.href);
} else {
LOG.debug("Page load location unknown, current window location=" + this.getCurrentWindow(true).location);
}
} catch (e) {
LOG.error("Caught an exception attempting to log location; this should get noticed soon!");
LOG.exception(e);
self.pageLoadError = e;
return;
}
self.newPageLoaded = true;
};
source to share
@Dave Hunt, you are correct what recordPageLoad
is being called , but it just logs the page location ( location.href
), if available, and sets self.newPageLoaded = true
, the value returned isNewPageLoaded
and thus to doOpen
.
isNewPageLoaded
called in <= <= . And it is called after the Selenium IDE runs the command .selenium-api.js
_isNewPageLoaded
makePageLoadCondition
doOpen
doOpen
open
Relevant trace:
doOpen
calls (jump to selenium-browserbot.js
) openLocation
=> getCurrentWindow
=> _modifyWindow
=> modifySeparateTestWindowToDetectPageLoads
(which, according to the comment above, waits "by polling continuously until the document is completely modified and fully loaded") => pollForLoad
=> getReadyState
.
getReadyState
returns document.readyState
( rs
), but pollForLoad
waits until rs == 'complete'
, that is, until the page is fully loaded, images and that's it.
Ace Ventura explains this better than I can.
PS @Dave Hunt, thanks for the Selenium IDE Flow Control script on GitHub . It came in handy! :)
source to share