Posts Tagged ‘selenium’

Using Selenium to test Ajax

December 1st, 2010 2 comments

If you’ve used Selenium before you know it’s power, and the pain of Ajax and Selenium…  I spent about a week on it at work and created several helper methods, but was never quite happy with my Ajax solution (waiting for text to be present, blech!!!).  Then, I found this article about getting selenium and ajax to play nicely…

It turns out the major JavaScript libraries (jQuery, YUI (v3+), Prototype, and Dojo) provide a way to see if requests are still active.  They provided instructions for writing a function, but I wanted a fool proof method, that JUST WORKS regardless of what library you use.

So, I created this function (Currently supports jQuery, Dojo, and Prototype, not Yui 3 yet):

function getActiveRequestCount(){
    var currentActiveRequestCount = 0;
    //Get prototype active requests...
    if(window.Ajax && window.Ajax.activeRequestCount){
        currentActiveRequestCount += window.Ajax.activeRequestCount;

    //jQuery active requests...
        currentActiveRequestCount +=;

    if(window.dojo &&{
        currentActiveRequestCount +=;
    return currentActiveRequestCount;

Then, use the following selenium command to wait for the request count to be low…

selenium.waitForCondition("selenium.browserbot.getCurrentWindow().getActiveRequestCount() == 0", "30000");

And viola!!!  No more pesky pauses!!!  Or wait for text to be present!!!

Categories: ajax, dojo, html, jquery, selenium, yui Tags: