Article / November 10, 2008

jQuery ajax Loading and Selenium Respec Testing

By ceberz/2777 Views/2 Comments
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 >.>

Comments

Posted by Ragnar on about 1 year agoEdbe4de13a6284cc37d81ee51243f960?s=30

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");

Posted by WaaX on 11 months ago46f61a8a15c5e234ad416b590a2676c9?s=30

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-getc... it fixed the problem.

Add a Comment