Waiting for a page to be loaded (synchronizing) with Selenium Webdriver

An often reoccuring automation problem is the synchronization of web pages. The automation script should only continue if the page is fully loaded. If the automation code tries to continue before the page is fully loaded, some objects might not be visible yet and the script will fail.
In some web development frameworks a loading indicator is used to show that a page is being loaded. The presence of this loading indicator object can be used to determine whether the page is still loading.

How can we implement this in Webdriver? Webdriver has a built-in method to check if an element is visible or not: ExpectedConditions.visibilityOfElementLocated(). We could use this method in combination with the WebDriverWait object in order to wait until the loading indicator is not visible, for instance:
private void waitUntilPageStopsLoading(int timeout) {
By loadingIndicatorLocator = By.xpath("//div[contains(@class,'v-loading-indicator')][contains(@style, 'display: block;')]");
WebDriver driver = getDriver();
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingIndicatorLocator));
}
However, this approach will not work all the time. Imagine that the application starts loading with a short delay. If the WebDriverWait checks for the presence of the loading indicator before the loading indicator is shown, the script will conclude that loading has finished and will continue, even before the actual loading has started.

Therefore I prefer to first check if the page starts loading, with a timeout of 1 second for instance. This way we'll wait for maximum 1 second for the loading to start. If the loading indicator is still not shown after 1 second, we can be quite sure that the page will not start loading anymore, else we wait until the loading has finished.
Here's a complete example:
public void sync() {
sync(60);
}

public void sync(int checkStopSyncTimeout) {
sync(1, checkStopSyncTimeout);
}

public void sync(int checkStartSyncTimeout, int checkStopSyncTimeout) {

if (pageStartsLoading(checkStartSyncTimeout)) {
waitUntilPageStopsLoading(checkStopSyncTimeout);
}

}

private boolean pageStartsLoading(int timeout) {

WebElement element = null;
try {
By loadingIndicatorLocator = By.xpath("//div[contains(@class,'v-loading-indicator')][contains(@style, 'display: block;')]");

WebDriverWait wait = new WebDriverWait(getDriver(), timeout);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(loadingIndicatorLocator));

} catch (TimeoutException e) {
// Do nothing, if the page doesn't start loading, it probably means that the page doesn't have to start loading and is already synchronized (or is really slow :-))
}

return element != null;
}

private void waitUntilPageStopsLoading(int timeout) {
By loadingIndicatorLocator = By.xpath("//div[contains(@class,'v-loading-indicator')][contains(@style, 'display: block;')]");

WebDriver driver = getDriver();
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingIndicatorLocator));

}

Related Posts by Categories

Comments

1 Response to "Waiting for a page to be loaded (synchronizing) with Selenium Webdriver"

Mukesh otwani said... March 5, 2015 at 3:44 PM

Nice article

Post a Comment

Recent Articles

Top Commenters

Recent Comments