Thursday, October 29, 2009

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

14 comments:

Unknown said...

Thanks for the post, it helped me a lot. I am using Perl and selenium and was having hard times finding the right way to get popup window activated. For Perl I used the following:

my $feedWinId = $selenium.get_eval("{var windowId; for(var x in selenium.browserbot.openedWindows ) {windowId=x;} }");
$selenium.select_window("name=$feedWinId");

Thanks again for the useful post

Unknown said...

This post is incredibly helpful and I was easily able to employ your suggestions.

Brian Kitchener said...

I have never had a problem getting back tot he main window. Here is some example code i'm using right now:

selenium.WaitForPopUp("",timeout);
selenium.SelectPopUp("");

//perform actions, click then either click the close button or
selenium.Close();
//and then to select your main window call it with null, this will reset you back to the inital browser window that was opened with selenium.

selenium.SelectWindow("null");

Shan said...

Hi all,
Thx for your posts...
I used below code and not working....

1) selenium.Click("css=input[id$='_LookupFooterImageButton']");
2) String feedWinId = selenium.GetEval("{var windowId; for(var x in selenium.browserbot.openedWindows ) {windowId=x;} }");
3) selenium.SelectWindow(feedWinId);
4) selenium.WindowFocus();
5) selenium.Type("//form[@id='form1']/table//*[@id='SearchTextBox']", "p");

Here while executing, selenium RemoteRunner executes line 1 which is going to open popup window.
after that no response for long time..
When close popup window manually while executing, that time lines 2,3 and 4 are executing....
line 5 is the command to give input on popup window...

Can anybody help me?

Thx in advance,
SCSVEL

Rahul Mendiratta said...

Thanks a lot... Its really helpful for getting window id.

Seetha said...

How do I find the name of a dynamic pop up window.i.e After logging in, if a popup window comes automatically, how would I know the name. How can I handle this

Thanh Le said...

It does work if I run the test in *iexploreproxy

Thanh Le said...

Hi,

Thanks for your topic, but it does not work if I run the test on *iexploreproxy

The value, that was got after execute command my $feedWinId = $selenium.get_eval("{var windowId; for(var x in selenium.browserbot.openedWindows ) {windowId=x;} }");
is "null"

Could anybody help me?

Thanh Le said...

Hi,

Thanks for your topic, but it does not work if I run the test on *iexploreproxy

The value, that was got after execute command my $feedWinId = $selenium.get_eval("{var windowId; for(var x in selenium.browserbot.openedWindows ) {windowId=x;} }");
is "null"

Could anybody help me?

Gopal said...

Hi ,

I am facing to run a test case for a special scenario.

Step1:
User login.
Select some product and add to cart.
(In this scenario the product pickup is option available it will open a popup )

To test this popup i wrote code like below

Example code:
if(popupName != null & popupName.length() >0)
{

Utils.logMessage("DOING OPERATIONS ON POP WINDOW --> THIS IS POPUP WINDOW NAME " + popupName);
this.config.getBrowser().waitForPopUp(popupName, String.valueOf(sleepTime));
this.config.getBrowser().selectPopUp(popupName);
...
instr.execute(this.config, context, this, testPlanDtls.getClassName());
..
}

if(isPopup && popupName != null)
{
this.config.getBrowser().deselectPopUp();
this.config.getBrowser().selectWindow(null);
verify(this.config, context, testPlanDtls,testCaseDetails,verify);
}

Senarion 2:
--------------
If the same above case some products doesn't have pickup option then it will not test the popup scenario . It will ignore the test case to execute,

Note : WE don't know which product have pickup option.
The code should work both scenario.

Gopal said...

How to wrote a code for selenium IDE for dynamic popup test as below.

Senario1.
If the popup available in the flow it should execute the test case for popup.

If the popup is doesn't have popup.

if(popupName != null & popupName.length() >0)
{

Utils.logMessage("DOING OPERATIONS ON POP WINDOW --> THIS IS POPUP WINDOW NAME " + popupName);
this.config.getBrowser().waitForPopUp(popupName, String.valueOf(sleepTime));
this.config.getBrowser().selectPopUp(popupName);
...
instr.execute(this.config, context, this, testPlanDtls.getClassName());
..
}

if(isPopup && popupName != null)
{
this.config.getBrowser().deselectPopUp();
this.config.getBrowser().selectWindow(null);
}

Thanks,
Gopal

Arindam Das said...

Thanks for the post it got me started. I was(am) using WebDriver, so the equivalent code is this.

driver = new FirefoxDriver();
baseUrl = "http://10.10.80.62:8080/";
for(String locationPopUpHandle : driver.getWindowHandles()){
driver.switchTo().window(locationPopUpHandle);
try{
new Select(driver.findElement(By.id("locSelect"))).selectByVisibleText("2 Location");
driver.findElement(By.id("btnChange")).click();
} catch(NoSuchElementException ex){
// ignore as it is trying various popup windows
}
}

Francisco said...

Hello,

In Selenium IDE when I try to open a new window, I can run the commands:
waitForPopUp ("dynamic parameter", 5000)
selectWindow ("dynamic parameter", )
but the vble "dynamic parameter" is dynamic so when I export the script to other platform it doesn't run.

Someone know how to get a dynamic parameter (id or name) of a new window with Selenium IDE? I searched in different forums but I don't find any solution.

Thanks! :)

Francisco said...

Hello,

In Selenium IDE when I try to open a new window, I can run the commands:
waitForPopUp ("dynamic parameter", 5000)
selectWindow ("dynamic parameter", )
but the vble "dynamic parameter" is dynamic so when I export the script to other platform it doesn't run.

Someone know how to get a dynamic parameter (id or name) of a new window with Selenium IDE? I searched in different forums but I don't find any solution.

Thanks! :)