views:

1229

answers:

1

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?

+1  A: 

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
Thanks Commander Dave. :) Can you point me to the file or SVN address where you found this?
Jeff Meatball Yang
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