Home > ajax, dojo, html, jquery, selenium, yui > Using Selenium to test Ajax

Using Selenium to test Ajax

December 1st, 2010 Leave a comment Go to 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 += jQuery.active;

    if(window.dojo && window.dojo.io.XMLHTTPTransport.inFlight.length){
        currentActiveRequestCount += window.dojo.io.XMLHTTPTransport.inFlight.length;
    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!!!

Be Sociable, Share!
Categories: ajax, dojo, html, jquery, selenium, yui Tags:
  1. jamin
    March 27th, 2012 at 16:10 | #1


    i was just wondering where you execute the javascript function getActiveRequestCount() from in a java class

  2. March 27th, 2012 at 17:10 | #2

    Hi Jamin,

    Good question, there is an answer on Stack Overflow here: http://stackoverflow.com/questions/2767690/how-do-you-use-selenium-to-execute-javascript-within-a-frame

    Essentialy you need to do: selenium.getEval(“var window = this.browserbot.getUserWindow();window.getActiveRequestCount()”);

  1. No trackbacks yet.