2

When I call the Open("[some URL]") method in Selenium, it waits until all resources (images, CSS, JS) are downloaded. But how does it know that the browser is done requesting resources?

Here's my snippet of driver code (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 a setTimeout() triggered in the body onload event handler, Selenium won't actually wait for that AJAX call before continuing... I have to manually tell Selenium to wait using WaitForPageToLoad(timeout).

So how does Selenium detect when a normal page (without AJAX) is finished loading?

Shog9
  • 156,901
  • 35
  • 231
  • 235
Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125

2 Answers2

2

I believe it waits for location.href to be available, which means that if you have AJAX running after this point you will also need to use waitForCondition.

selenium.open("http://www.example.com/");
selenium.waitForCondition("var value = selenium.getText('id=pageLoadStatus'); value == 'Loaded'", "60000");

Below is the current page load detection code from 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;
};
Dave Hunt
  • 8,191
  • 4
  • 38
  • 39
  • Thanks Commander Dave. :) Can you point me to the file or SVN address where you found this? – Jeff Meatball Yang Sep 22 '09 at 15:00
  • You're welcome! http://code.google.com/p/selenium/source/browse/selenium-core/trunk/src/main/resources/core/scripts/selenium-browserbot.js#83 – Dave Hunt Sep 22 '09 at 15:16
  • You can also use [`waitForElementPresent`](http://release.seleniumhq.org/selenium-core/1.0.1/reference.html#storeElementPresent) after calling `open`. – ma11hew28 Feb 19 '11 at 18:50
1

@Dave Hunt, you're correct that recordPageLoad is called, but it merely logs the page-load location (location.href) if available and sets self.newPageLoaded = true, the value returned by isNewPageLoaded and, thus, by doOpen.

isNewPageLoaded is called in selenium-api.js by _isNewPageLoaded <= makePageLoadCondition <= doOpen. And, doOpen is invoked after Selenium IDE runs the open command.

The relevant trace is:

doOpen calls (moving to selenium-browserbot.js) openLocation => getCurrentWindow => _modifyWindow => modifySeparateTestWindowToDetectPageLoads (which according to the comment above it, waits "by polling continuously until the document changes and is fully loaded") => pollForLoad => getReadyState.

getReadyState returns the document.readyState (rs), and pollForLoad waits until rs == 'complete', i.e., until the page has fully loaded, images and all.

Ace Ventura explains it better than I can.


P.S. @Dave Hunt, thanks for your Selenium IDE Flow Control script on GitHub. It came in handy! :)

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651