Thursday, October 9, 2008

Using ajax transaction counts in selenium tests

Sometimes your "only hope" in making a selenium web test stable is to keep track of the number of ajax requests that have happened on the client side.

An example of this is the ajax request that normally returns exactly the same html as was present in the dom before the request. Although this may sound stupid, there are legitimate situations for this.

My test would look like this:

int transactionNumer = selenium.getCurrentAjaxTransactionNumber();
selenium.clickLink("SomeLink");
selenium.waitForAjaxTransactionNumberChange(transactionNumer);
-- rest of test ---


This test is free from timing issues, because it will not proceed to the rest of the test before the ajax request is completed.


public int getCurentAjaxTransactionNumber() {
String eval = selenium.getEval("window.ajaxRequestNumber");
return Integer.parseInt( eval);
}





In my ajax.js file, which wraps prototype, I will typically include two lines like this:

window.ajaxRequestNumber = 0;


Now you will have to find a way to patch into your ajax framework to update this value. For prototype, this would work:


Ajax.Responders.register({
onComplete: function() {
window.ajaxRequestNumber++;
}
});


Now you can track request numbers, and yes - they start with value 0 on page load.

No comments:

Post a Comment