Monday, December 14, 2009

How to handle popup window in Selenium?

Introduction
HTML provides window popup facility.A popup window is a web browser window that is smaller than standard window size and without some of the standard features such as tool bar or status bar. The popup window can have all basic html element like normal text, input fields, buttons ,etc.
The Web application contains the popup window and the tester has  to test the popup functionality.
In manual testing there is no problem. It is just a  click on the link and it will open popup window and verify its functionality.
In automation testing ,script should be able to detect popup window and keep it in AUT.


How to detect opened popup window ?
We can detect opened popup window based on their id, name and title


How to keep opened popup window in AUT ?
Selenium provide selectWindow method for keeping pop window in AUT.


If selenium is able to detect and select opened popup windows then what is the problem ?
Problem comes when developer use dirty code for popup window or title, name and id are auto generated.
Here is example where developer missed id, name and title


<a href="javascript:window.open('http://www.google.com','');void(0);" > popup window</a>
Here we don't have any identifier which can be used in selenium to select this popup window. If we click on the link then it will open a pop window which doesn't have any name or id or title.


If we open in selenium then it will give a auto generated identity but selenium doesn't provide any direct method to get auto generate identity. It means we cant select this window because it doesn't have any identifier.


How to work smartly:
To handle this problem we can use a custom method to get name of newly opened pop up window.
I have used Java but we can any other languages which is supported by Selenium.


Here is the custom method which detect any opened popup window and select it for further testing steps :


protected selectWindow(String windowIndenifer ) {
if (windowIndenifer==null ||windowIndenifer.length() <= 1)
windowIndenifer = selenium
.getEval("{var windowId; for(var xWindow in
selenium.browserbot.openedWindows ) {windowId=xWindow;} }");
if(windowIndenifer == null || windowIndenifer.length <=1)
System.out.prinln("There is no popupWindow in current AUT")
else{
selenium.selectWindow(windowIndenifer);
System.out.prinln("Selenium found a popup Window : " + windowIndenifer);
}
}