Currently Browsing

Posts Tagged ‘ ajax ’

Wedbriver wait for ajax to finish and JQuery animation

JQuery’s ajax calls can make an automated tester’s life miserable if you don’t know how to handle them.

The quickest way to handle all of the Jquery ajax calls is to wait for the jQuery.active property to be null.
We can easily construct a sample code trough the JavasscriptExecutor class.It sends out JavaScript code which can be used for testing the property.

public void waitForAjaxToFinish() {
int timeout = 0;

while(timeout < 80) {
	boolean ajaxFinished = (boolean) ((JavascriptExecutor) driver)
			.executeScript("return !!jQuery && jQuery.active == 0");
	if(ajaxFinished) 
		return;
	timeout++;
	waitFor(500);
	}

throw new AssertionError("Ajax haven't finished its job in 40 sec");
}

The above showed code is best for a single Ajax requests, but when we have multiple Ajax requests there can be problems.
Multiple Ajax requests have different completion time and there is no guarantee that there is no fancy Ajax refresher gadget used, which can make previous wait function timeout.