A Blog for quality professional

AJAX Testing with Selenium

The obvious approach approach in salenium  is to open the page, click the button, then check the value of the text field. However when we did that in Selenium, the test case failed.because its working on form submission

The reason for the test failure is perhaps not so obvious. What happens is that the asynchronous nature of the Ajax call means that the result doesn’t come back from the server immediately.

Selenium doesn’t know to wait for the result,so how can we get the effective Ajax effect tested

One way to solve the problem is to use the click and wait 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.

Another possibility is to insert a pause between the click and aseertvalue So lets pause for 5 seconds in between, to give the server enough time to respond. This approach works most of the time, but it may fail if the server takes more than 5 seconds.

This might happen for any number of reasons, slow network, overloaded test machine, and so on. You can keep increasing the pause before checking the result, but this will make your tests run slower and slower. So this clearly isn’t the best solution because it’s not guaranteed to work and makes your tests run much slower than necessary.

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

So in order to make the failing test pass we need to replace the failing assertvalue command.

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.

Idea Taken from popular site and my own experience

Shishir Gopal Jee Srivastava

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: