Ajax! Ajax! Ajax!!!!
You cannot go to a website without running into some form of Ajax. It’s great and all, but if you are trying to test a website with Selenium, this becomes an issue. Selenium will wait for the page to load, but is rather clueless when it comes to Ajax.
After spending several painful hours looking for a way to make a “wait_for_ajax” function, I found several solutions that do not work.
1 – Put sleep in your test.
Well obviously this is the simplest solution, but also the worst. We don’t know how long a request will take to load, what if the server is slow?
2- Use “waitForElement” command built into selenium
This is a better solution; we wait for a certain element, which is part of the Ajax response to appear before moving on. The problem with this solution is that we need to always know which element needs to load. This becomes a headache for us normal people with extra large web applications to support.
SOLUTION!
Here is the solution I stopped on. I won’t take credit for making it, but I’ll take some credit for compiling it all in one place. To be honest I have not seen it done yet, the only thing I’ve seen was for bits and pieces but not the whole problem.
To solve this problem, what we have to do is ask the browser to tell us how many active connections to the server are present. Then we sit and wait for that number to become 0. At this point we know that all Ajax requests went through and done, and we can move on in the test. (If you have a webapp that contains non-stop Ajax request to constantly update everything, you have my sympathy.)
Step 1: Figure out which Javascript library your application uses
This is important one, which had me stomped for a long time. Different libraries (Prototype, Dojo, jQuery) use different methods to retrieve the connection info.
Step2: Create the wait_for_ajax method
def wait_for_ajax(timeout=5000)
js_condition = “”
$selenium.wait_for_condition(js_condition, timeout)
end
Let’s break this one down.
Step 3: Getting the right JS query for your library.
This part was the one, which took me the longest to figure out. But I’ll give you all the answers here. Different libraries use different functions to check incoming connection, so pick the right one for you.
jQuery: “jQuery.active”
Prototype: “Ajax.activeRequestCount”
Dojo: “dojo.io.XMLHTTPTransport.inFlight.length”
Step 4: Put together the JS search string
“selenium.browserbot.getCurrentWindow().” + library specific string + “ == 0”
Final string for jQuery will look like this
js_condition = “selenium.browserbot.getCurrentWindow().jQuery.active == 0”
Step 5: Put the method all together and enjoy
def wait_for_ajax(timeout=5000)
js_condition = “selenium.browserbot.getCurrentWindow().jQuery.active == 0”
$selenium.wait_for_condition(js_condition, timeout)
end
If I’m missing something, let me know. But spread the word, I know many of selenium users need this.
15 Responses
redsquare
27|Jan|2010 1Just found this. Great work, truly awesome as my 5yr old would say.
Protocol Testing Coaching Bangalore
04|Feb|2010 2I really enjoyed exploring your site. good resource … thanks for sharing the info, keep up the good work going….
Nazar
05|Feb|2010 3Hello. Nice idea. But do you have any idea how to do it for Yahoo ui?
Niall
18|Feb|2010 4Great post, very useful indeed – exactly what I was looking for.
Thank you.
Nick
19|Feb|2010 5Dima,
We’re attempting to use, as suggested, dojo.io.XMLHTTPTransport.inFlight.length but dojo.io is apparently unrecognised by our app.
“dojo.io is undefined”
Indeed, there doesn’t appear to be an XMLHTTPTransport in the base 1.3.2 distributable at all! Can you point us in the right direction to get this working?
Thank you!
randy collins
19|Apr|2010 6Hello ,
Your site looks great. Your site was personally hand picked by our client.
I’m emailing because we do work for a company that offers software similar to Windows Active Directory to help manage employees.
They’ve asked me to contact relevant websites like yours so that they may advertise on your website – in particular they are interested in placing a text link ad in a short sentence on agilesoftwaretesting.com
If you’re interested in letting them advertise on your site please let me know, and we can work through what they’d be willing to pay.
Sincerely,
Randy Collins
Advertising Coordinator
Fabio Brandão – Blog » Blog Archive » Selenium + Wait for Ajax
20|Apr|2010 7[...] este link: http://agilesoftwaretesting.com/?p=111, que fala o [...]
Don
27|May|2010 8Hi,
Does anybody know how to write js_condition for MooTools, like one for jQuery as mentioned above:
js_condition = “selenium.browserbot.getCurrentWindow().jQuery.active == 0”
Thank you.
Don
Don
31|May|2010 9oops, nobody knows the solution?
Dima
03|Jun|2010 10Sorry, i don’t think i have the answer for you
Selenium e Ajax – Amor e Odio | Java, Selenium e Coca Cola
21|Jun|2010 11[...] poder verificar se já carregou o include javascript. Para acessar esse pequeno tutorial, clique aqui (quando eu tiver tempo eu traduzo e explico melhor a [...]
jQuery.active function | The Largest Forum Archive
30|Jun|2010 12[...] http://agilesoftwaretesting.com/?p=111 [...]
manoj
12|Jul|2010 13Hi,
I am a novice programmer using Selenium, wanted to know
which js file should I include this function? Also if you could provide
me some examples of calling this function would be great.
Thanks
Manoj
David Kahn
19|Aug|2010 14Going a little further, when you create your Selenium object, you can do this so you have @selenium.wait_for_browser:
@selenium = Selenium::Client::Driver.new(
:host => “localhost”,
:port => 4444,
:browser => “*firefox”,
:url => “http://localhost:3000″,
:timeout_in_second => 90)
# add wait_for_ajax method to browser
class << @browser
def wait_for_ajax(timeout=5000)
js_condition = "selenium.browserbot.getCurrentWindow().jQuery.active == 0"
self.wait_for_condition(js_condition, timeout)
end
end
David Kahn
19|Aug|2010 15PS, you may know this but Selenium::Client::Driver has a wait_for_ajax method, although it seems (without too much time put in to researching this) that it only works with Prototype. Anyhow your method rocks!
Leave a reply
Search
Recent Posts
Categories
Archives
Meta
Pages
Recent Comments
A design creation of Design Disease
Copyright © 2007 - Agile Software Testing - is proudly powered by WordPress
InSense 1.0 Theme by Design Disease brought to you by HostGator Web Hosting.