Blog

Nov
10
jQuery ajax Loading and Selenium Respec Testing
by ceberz | Article

jQuery Logo

I’m writing plain text stories for a project that heavily uses jQuery. When running s selenium test, it’s important to tell selenium to wait for a page to finish loading after some other action (say, the post of a form) before proceeding to the next action. An issue that arises when you’re dealing with ajax is that the ruby selenium driver has no built-in method for waiting for an ajax request to finish, the same way you would wait for the page to load after an HTTP request.

The alternative offered with the driver is to tell the selenium server to pause until a javascript expression evaluates to true, which there is a function for already. The question is how to do this with jQuery, which wasn’t very easily answered at first since the majority of knowledge regarding this problem seems to deal with pages using prototype. As for jQuery, the solution seems to be to use the following piece of javascript:

jQuery.active

…which will report 0 when no ajax requests are active. Calling it the context of your test might look like this:

$selenium.wait_for_condition('selenium.browserbot.getCurrentWindow().jQuery.active == 0', $max_selenium_timeout)

This ended up being a much simpler solution that I had assumed it would be >.>

2 Comments
Ragnar
January 9, 2009

Thank you!

I used this for waitForCondition with ASP.NET

Javascript:

function isInAsyncPostBack() {
instance = Sys.WebForms.PageRequestManager.getInstance();

return instance.get_isInAsyncPostBack();
}

C#

selenium.WaitForCondition(“!selenium.browserbot.getCurrentWindow().isInAsyncPostBack()”, “1000″);

WaaX
February 19, 2009

It wasn’t working for me, jQuery or any other custom properties were not found, with the help of #selenium and this post http://crschmidt.net/blog/348/selenium-ide-getcurrentwindow-problems/ it fixed the problem.