Skip to content
Advertisement

How to count active javascript timeouts?

I am using Selenium to test a web app that uses Dojo, and it uses java script to display certain elements. I want to wait until all of the elements are desplayed before I try to manipulate the page, however I am having trouble.

I have started by waiting for the dojo inFlight variable to be 0, this says that all ajax has finished. This doesn’t always work because it seems to do some things on a timeout afterwards.

I have also tried repeatedly looking for the element, but this isn’t too nice, as perhaps there is some javascript later which will use this field in some way.

So basically I want a method (in firefox at least) to query the javascript waiting to run on a setTimeout (or setInterval) I could even cope with a way of wrapping the built in call through a function of my own just to keep track of this.

Any thoughts or suggestions appreciated!

Advertisement

Answer

Every function in JavaScript can be replaced. Consider something like this:

window.originalSetTimeout = window.setTimeout;

window.setTimeout = function(func, delay, params) {
    window.timeoutCounter++;
    window.originalSetTimeout(window.timeoutCallback, delay, [func, params]);
}

window.timeoutCallback = function(funcAndParams) {
    window.timeoutCounter--;
    func = funcAndParams[0];
    params = funcAndParams[1];
    func(params);
}

Then:

selenium.waitForCondition("window.timeoutCounter == 0");
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement