Monday, May 4, 2009

Testing Ajax Applications with Selenium

Testing Ajax Applications with Selenium


AJAX, stands for Asynchronous JavaScript and XML, is a web development technique for developing intractive web application that makes

web applicaton fast and more responsive by exchanging data with the server.

For loading a page time is not fixed.(i.e 200 ms or 20 secs.) Ajax avoid reloading the page. Browser sends the request to server and refreshs the small part of

current page.This reduces the time between sending the request and seeing the result on the page because it saves time takes by reloading the entire page.

Gmail is the prefect example of Ajax.


When we click on arrow it does not reload entire page instead it refresh the web clicp header only.

Suppose we have an web application cotaining one text field and button. Once we click on the button text field reflectes with the result comes from the server.


In some case pause will work but its not a permanent workaround because we dont know how much time server will take to respond. It might repsond withing millseconds or might respond in more than 20 secs.

When selenium does not how long to wait for the result then how can we test these kind of application.

Wait For It...

So how do we get Selenium to wait for the result? One way to solve the problem is to use the clickAndWait command instead of click; the "AndWait" suffix indicates that Selenium should wait for a page-reload. This might seem like it would work, but there's a catch: because the page is not reloaded, clickAndWait will make Selenium keep waiting forever. This clearly isn't going to work.

Fortunately Selenium has provided support for doing exactly what we want. When a field value changes in the current page, you can use the waitForValue command to make Selenium wait until the expected value appears.



When executing this command, Selenium will suspend the execution of current test case and wait for the expected value. When the string "newValue" appears in the text field, the suspended test will go on. One thing to note: if you misspelled the expected value, Selenium will wait for 30 seconds before timing out.

As you may have already guessed, there are lots more commands that can be used to test Ajax effects. For example, if you want to check some plain text updated with Ajax, there's waitForText; if you want to check the title of current page, there's waitForTitle; if you want to verify that an HTML element is removed, there's waitForElementNotPresent. Actually, for every Selenium Accessor, there is a waitForXxxx and a waitForNotXxxx commands corresponding. Whenever you use verifyXxxx or assertXxxx to check something, there will always be a waitForXxxx for testing asynchronous effects.

What happens if the predefined waitForXxxx and waitForNotXxxx commands don't meet your needs? Well for this there is the waitForCondition command which allows you to specify a Boolean expression in Javascript which causes Selenium to wait until the expression evaluates to true

2 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Hi NIRAJ, Can you please leave some sample codes. They will be of great help.
Thanks
Ankit