Saturday, January 15, 2011

Working with Color Boxes and AJAX Loading in Selenium

Nowadays the fashion for developers is to use the light boxes or color boxes in the web applications.. to impress the end users .... Inorder to test such scenarios using selenium follow the below steps... It is to notice that the color boxes will appear in UI after some user interaction in the web page. So in case of color boxes the development code is as follows ..

Generally the HTML related to colorbox is already embedded in the webpage when you navigate.
Here one need to concentrate what is going on in the UI when a user performs some set of operations... As per my knowledge there will be some divs in webpage added with a dynamic content and some gray layer is added to the main page ...
So the selenium code to identify such color boxes should be some thing like this ....


  • Perform an event
  • Wait for the response
  • Check the UI is showing that div or not ....
  • Later on perform the necessary actions there....
Sample code would be as follows...


selenium.click("click the item which causes colorbox to open");

Run an infinite for loop .....

for(int i=0;;i++){
try {
if(selenium.isVisible("//div[@id='ajaxResp']")){
System.out.println("Found");
System.out.println(selenium.getText("//div[@id='ajaxResp']"));
break;
}else{
Thread.sleep(1000);
}
}catch(Exception e){

}
}

In the above program assume "ajaxResp" is the ID of the division which gets visible whenever the colorbox content is loaded in that DIV. If that div is visible then we can write the necessary tests we need to perform there...

For explanation .... the for loop first checks whether the div with id "ajaxResp" is visible or not...
if it is not visible it will stop the current thread execution for one second and the loop iterates again .... like wise it will keep on iterate until that div gets visible... there by we can perform the necessary actions there and ... the loop breaks there.....
If you want to cross verify whether the div is there or not .... do a getAttribute or isElementPresent on that div...

The getAttribute("XPATH OF COLOR BOX DIV@style")
gives a string we need to check whether display:none or visibility:hidden is set in that div or not.... If any one of this string is find then we can ensure that the div is available in the page but it is in hidden mode....

Same code is helpful in case of ajax applications.....

2 comments:

  1. An infinite If loop???

    This is insane, if it never becomes visible due to a bug in the code you will sit there looping forever and never throwing an error or getting any further along in your test.

    What possible use is a test that never fails? What useful information can this test give you?

    Finally, an infinite If loop?? what were you thinking?

    ReplyDelete
  2. HI Dude, Good question.... The answer for your question is to put a loop break to a finite time so that if that particular element is not found in the finite time throw an error that test case fails...

    ReplyDelete