Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: Ajax Testing

Please brief me about the Ajax testing with selenium RC. As in Ajax element not reflect on the view-source, but using firebug we can see the changes in HTML source code.

There are two methods associated with it Ajax testing..

1-The method "waitForCondition (java.lang.String script, java.lang.String timeout), to provide script we have to create a java script by own or it should be the same Ajax script/java script present on the web page.

Please correct me if i am wrong on below point..

2-The method "waitForElemantPresent(Locator)", We check the Element in the firebug and check the same in this method is self waitForElemantPresent(Locator).

Let me know if anything else I am missing testing Ajax application.

like image 632
smriti Avatar asked Jun 28 '11 09:06

smriti


2 Answers

I got help on this from one article and with help of @Hannibal

http://agilesoftwaretesting.com/?p=111

JQuery: “jQuery.active”

Prototype: “Ajax.activeRequestCount”

Dojo: “dojo.io.XMLHTTPTransport.inFlight.length”

So if there is Ajax call we can use second option.

selenium.waitForCondition(
        "selenium.browserbot.getCurrentWindow().jQuery.active == 0",
        timeout);
like image 105
smriti Avatar answered Oct 03 '22 15:10

smriti


To answer your first point, yes waitForCondition(javascript,timeout) will run the javascript till it returns a true value OR when timeout happens. You should take a look at the api documentation for this as you need to use browserbot to run the script in your application window. Link to API documentation is here

In Selenium 1, one way by which you can handle the Ajax conditions are by creating custom functions which will wait till the condition is met or till a timeout happens. While a normal Selenium.isElementPresent will fail immediately if the element is not present, your custom function will wait for some more time (the time for Ajax to load) before it fails. As an example you can refer the following custom method for isElementPresent. This is written in JAVA, you should be able to use the same logic in the programming language that you use.

public boolean AjaxIsElementPresent(String elementToLookFor, int timeOutValueInSeconds){

int timer=1;
while(timer<=timeOutValue){
 if(selenium.isElementPresent(locator))
   return true;
 else
   Thread.sleep(1000);
     }
return false;

}

This method will return a boolean false if the element is not present even after the specified timeoutValue. If it finds the element within the timeoutvalue then it returns a true.

I have seen some in built functions for AjaxCondition handling in Selenium 2. But I have not used it. You can refer to Selenium 2 code base

like image 22
A.J Avatar answered Oct 03 '22 14:10

A.J