How to capture popup windows using selenium.

I am going to discuss on selenium popup handling specifically with selenium RC. I was searching though internet for a while to find a solution for processing of popups though selenium.But I was unable to find best fit answer for my test scenario. I wanted to capture pop-up window do some processing then close the popup and get back to parent window again.Anyway by following the reference guides and other available materials I was able to implement the the above mentioned test scenario. 


As per my understanding selenium IDE supports capturing popup windows but get back to parent window is not supported. I tried several times and failed to set focus to parent window again. Selenium API doc says, in order to select parent window again, use selectWindow() methods with null parameter. But that didn't work for me. selenium RC was fail to select the parent window again. There are several API methods available in selenium to detect required references for popup windows. Those are

selenium.selectWindow();
selenium.getAllWindowIds();
selenium.getAllWindowNames();
selenium.windowFocus();
selenium.waitForPopUp();
selenium.close();


In my case,I have followed implementation give below to write the test scenarios in junit.

click the popup window link

selenium.click("link=Feed");

Get the pop up window ID.

String feedWinId = selenium.getEval("{var windowId; for(var x in selenium.browserbot.openedWindows ) {windowId=x;} }");

Select the popup window.In order to select it, you must somehow identify it. Various identification methods are available with selectWindow() function see http://release.seleniumhq.org/selenium-remote-control/0.9.0/doc/java/ for more details

selenium.selectWindow(feedWinId);

Set focus to popup window.

selenium.windowFocus(); 

//Then do some proccssing

close the popup

selenium.close(); 

select parent window again. You need to know the window title or name

String[] winFocus;
String winTitle;
winFocus = selenium.getAllWindowTitles();


If there is more than one open windows you have to iterate though winFocus array and find the correct window to set the focus.

winTitle = winFocus[0];

if (winTitle.equalsIgnoreCase("WSO2 Management Console")) {
selenium.selectWindow(winTitle);
}


If you need to wait for the popup then use

selenium.waitForPopUp(winID, time-ms);

I hope above instructions will help you in dealing with pop ups. Please feel free to comment and ask questions

Source code of the test : https://wso2.org/repos/wso2/branches/commons/qa/web-test-framework/2.0.2/registry/src/test/java/org/wso2/carbon/web/test/registry/FeedsTest.java