Tuesday, July 7, 2009

Selenium - Handling Windows

If your application has a link or a button which opens a new window, and you want to perform some action on the newly opened window though selenium, it's not that hard to handle if you have title, name or the variable associated with that window.

However this is not the case always, sometime you don’t have name of the window or any variable associated with it, and you are not even able able to identify window using title, at the time selenium sucks to identify the window. However there are some alternative ways to resolve this (which might work, not sure). But before we see that let’s see how to select the windows that can be identified easily.

Select window using SelectWindow API

When SelectWindow API fails to Identify Window

Select window using SelectWindow API
Selenium provides the selectWindow API which takes any of the following argument to identify the window.
Title: Use whatever appears on title bar of the window you want to select as argument to selct window API.
However be careful while using title to select the window. Because it may happen that multiple windows have same titles.And in this case Selenium just says that it will choose one, but doesn't mention which one. If this is the case where both have same title I would suggest not to use the title for selecting window. And sometimes the titles of windows are dynamic, and that may create some extra noise.

Name: You can use windows name to identify the window.
For example if the code to open a new window is like as below.

window.open("http://google.co.in","Google Window");

(The second parameter here is windows name. Developer uses this to identify the window in order to use that in further part of the coding.)

You can use "Google Window" as the argument to selectWindow, to select the window as shown below if the code to open the window is as shown above.

selenium.selectWindow("Google Window"); // select new window
//Do whatever you want to do on new window
Selenium.close(); //Close the newly opened window
selenium.selectWindow(null); //To choose the original window back.

Variable name: If opened window is assigned to some variable you can use that variable to identify and select that window.

For example if the code to open a new window is like as below.

GoogleWindow = window.open("http://google.co.in");

You can use "GoogleWindow" as argument to SelectWindow API, to select the window as shown in the code.

selenium.waitForPopUp("GoogleWindow", "30000");
selenium.selectWindow("GoogleWindow");
//Execute steps on new window
selenium.close(); //close the new window
selenium.selectWindow("null"); // select original window.

Using inbuilt API to retrieve Windows Names, Titles and IDs
Selenium provides some APIs to find out the names, Titles and IDs of the all opened windows. So you can use this APIs with SelectWindow API to select the window.

getAllWindowNames() – this will return you the array of all the opened windows names.
getAllWindowTitles() - this will return you the array of all the opened windows names.
getAllWindowIDs() - this will return you the array of all the opened windows names.

So if you have opened only single window you can use something like this to select the newly opened window.

selenium.selectWindow(getAllWindowNames()[1]);
OR
selenium.selectWindow(getAllWindowTitles()[1]);
OR
selenium.selectWindow(getAllWindowIDs ()[1]);

When SelectWindow API fails to Identify Window
Sometimes it happens that you use all the things to select the window but it fails to identify the windows. At the time you can use one of the following ways

Check the selenium logs.
The document of the selectWindow API says that if selenium is not able to identify the window after using all the things mention above you should check out for the window name in selenium log. To do that just start your selenium server with –log argument, something like following:

Java – jar path of selenium-server.jar –log log.txt

Write down the test case to open the window and execute it. Now shutdown the selenium server and checkout the logs for something like this.

Debug: window.open call intercepted; window ID
If you find this you can use this window ID to identify and select the window as the argument to selectWindow API.


Selecting the Window Using openWindow API
Sometimes selenium is not able to identify the window, one of the case is when the window is opened through onload event. Something likes this

<body onload='window.open("http://google.co.in");'>

In this case you can force selenium to identify the window using openwindow command, with URL as blank and name as you like.

Selenium.openWindow("","MyWindow");
selenium.selectWindow("MyWindow");

But if your window is not opening through onload event, and though selenium is not able to select the window after doing all the above things. The only thing left is to open the window using openwindow API with URL same as it would be if would have been opend through button or link click and name as you like. Something like as below.

Selenium.openWindow("http://google.co.in","MyWindow");
Selenium.selectWindow("MyWindow");

33 comments:

lelige said...

Very useful! This is exactly what I'm searching for. Thank you!

Anonymous said...

Hi:

I have a question regarding to pop-up window handling in Selenium RC for IE 7 automation testing.

I am running a test case with JUnit in IE 7 where there will be a pop-up window. However, the pop-up window does not have any windowsID assigned; there is neither title nor name for it.

I have tried several methods but I am still getting the same error message:” Error: 'pathname' is null or not an object” or “Permission Denied” when it comes to pop-up window in RC.

The partial code is shown below:

selenium.click("xpath=//td[@id='addWidgetBtncenter']");
selenium.waitForPageToLoad("30000");
//selenium.getAllWindowIds(() [1]);
//selenium.selectWindow(() [1]);
selenium.selectPopUp("");
//system.threading.thread.sleep(3000);
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("xpath=//input[@id='widgetName']")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
//System.Threading.Thread.Sleep(3000);

I have used these commands with target of the button that triggers the pop-up window. (In this case, addWidgetBtncenter is the button to click on for add widget)

I have placed these function commands after the action of "click the add widget button" but before "waitforPopUp", please tell me if I am use these commands correctly in terms of their syntax or place order. (I have tried to place it after waitforpopup but before "selectopup", it still doesn't give me any information in the log)

Also, I have used "windowFocus" command then "selectFrame" or "selectpopup", but none of them worked in IE

I am still new to Selenium/JUnit, can anyone please help me with the correct syntax for these functions: “"storeAllWindowIds", ”storeAllWindowNames" and "storeAllWindowTitles".” As well as the “System.Threading.Thread.Sleep(3000);”

I have been stuck in this for a while and I really need help with it…

Any commands and suggestions are greatly appreciated!!!

Best,

Hades

Anonymous said...

Awesome help!

Unknown said...

Hi
I am new to selenium, could you please explain how open a new tab when we click on a link in firefox browser in java?.

Unknown said...

Hi Srinivas,

I don't think selenium allows tabbed browsing yet.

Jagan Khanal said...

Hi Gaurang,

Thank you for your Tips regarding selection original window. It helped when I used selenium.selectWindow("null") instead of selenium.selectWindow("name=null")

Thanks,
Jagan

Unknown said...

Hi Gaurang,

Thank u for thw great post.

BR,
Rade

Jagan Khanal said...

Hi Gaurang,
Selenium IDE does not recognize the input entered into FCK editor which is created using JavaScript. Any tips how to capture in this scenario would be really great.
Thanks,
Jagan

Kiran said...

Thats a great article Gaurang. My sincere appreciation

prasank said...

Hi Gaurang, I am clicking on a link from website which opens a new window. I am not able to select the new window & type in values. I am using Selenium IDE 1.0.7, following is my script...

open|Home page|
click|link=Enter user name|



waitForPopUp|Newwindow|30000
selectWindow|title=NewWindow|
type|username|abcd

Thanks.

Unknown said...

Hi Gaurang.....thanks for the tips....
I am facing a wired situation.....
let me elaborate my scenario.....
my application has few test fields and a link..
If click on the link (called using JAVA program)a new popup window will open which has got two test fields....i need to perform on the text fields and click on the save button...by doing this the popup window will close and focus should be back on main window....
Now what happens is when i click the link i am getting the IE script error,permission denied....and the whole selenium execution is halting....I tried to over come this by using IE 6,7,8 and changing the setting in the internet options-- Advanced--, nothing worked out.The same application i tried using FireFox...selenium is able to click on the link, but if once the link is clicked and the next window is populated selenium is halting the execution...I mean selenium is unable to proceed further...how to over come this issue....

thank & regards,
gajanan.bogam

ikram said...

HiGraurang

Could u please tell me in recorded code scripts where should i need to copy paste this codes. like
selenium.selectWindow("Google Window"); // select new window
//Do whatever you want to do on new window
Selenium.close(); //Close the newly opened window
selenium.selectWindow(null); //To choose the original window back.

selenium.waitForPopUp("GoogleWindow", "30000");
selenium.selectWindow("GoogleWindow");
//Execute steps on new window
selenium.close(); //close the new window
selenium.selectWindow("null"); // select original window.


selenium.selectWindow(getAllWindowNames()[1]);


OR

selenium.selectWindow(getAllWindowTitles()[1]);
selenium.selectWindow(getAllWindowIDs ()[1]);

Early response would be appreciated

Thanks and warm regards!!
Ikram
9971866303,9213679375

shveta said...

Hey Gaurav ,

Can you tell me what can i use for Linux . Username and password dialog box in Linux. I understand autoIt doesnt work on Linux.

Thanks.

Unknown said...

Hi Shveta,
I am not sure about this but you can use perl script for this.

wright-dragon said...

can u pls give a sample code for perl

Anonymous said...

command selenium.selectWindow("null")not working for me to select original window.
Anybody help me in this?

Venkatesh said...

Hello Gaurang Shah

Can I open a new window when I click on button.But New window doesnot any name and title.I open new window using url(sel.open("url");) explicitly.but it is not finding any element in the new window.i get the exception as element not found.how to resolve this

cshandler said...

Hi Gaurang Shah,

Nice blog friend, I'm not from the software testing field I'm a developer but I do work with my testing team and these days they are facing problem of Selenium & IE non-friendly natures :). Nice to see your work on selenium.. Keep it up man.

Blog: www.cshandler.com
Twitter: @vendettamit

Prasad said...

Hi Gaurang Shah,

I am a not able to click a link in the popup window,by using Selenium IDE?
The script is working fine when I run the single line click operation on the link,but when I execute the entire test case,the script failed.Please provide the solution for this scenario?

Sibiraj said...

Hi Jagan,
"Selenium IDE does not recognize the input entered into FCK editor which is created using JavaScript. Any tips how to capture in this scenario would be really great."

Try this,

Line 1: Command: selectFrame Target: provide “id of the frame” Value: Null

Line2: Command: selectFrame Target: //iframe Value: Null

Line3: Command: type Target: //body Value: "Input Content"

Line4: Command: selectFrame Target: relative=top Value: Null

Sibiraj said...

Hi Gaurang Shah,
I am facing an issue with Facebook Login window, not able to capture the window and input the user name & password for login, I tried almost all these methods, but not working. Can you please help?

Raj said...

On selenium 1.0 select_window("title") works but on 2.0 switch_to_window("title") doesn't work. Any idea?

Gem said...

Hello all,

I am trying to run the selenium commands in an already open browser. I know how to open the new browser and execute the commands but was wondering if I can run the commands on the already open browser. Please let me know if any one is aware of this.

Unknown said...

he gem,

Nope you can't run your command on already opened browser. Selenium open the browser and gets the session id. it tracks your browser through that session id only...

if you somehow trick selenium to get the session id of opened browser than it can work.

Anu said...

Hi
I am using RC
selenium.selectWindow(null); not works in IE
Kindly help :(

Anu said...

Hi All,
How to perform any actions like close,click on a child window that has NO window id. Please let me know how to do it in RC & web driver

Riy said...

Hi,
This blog is great but i have a difficulty in returning to my original window.
String[] windowNames = selenium.getAllWindowNames();
selenium.selectWindow(windowNames[1]);
After the automation of the new window returning back is not possible i dont know the reason. I tries using the window name and focus it gets focused but the code reads the error as (ERROR: Window does not exist. If this looks like a Selenium bug, make sure to read http://seleniumhq.org/docs/04_selenese_commands.html#alerts-popups-and-multiple-windows for potential workarounds.) Pls help

siri said...

how to test window based applications using selenium (like skype,outlook operations)

Unknown said...

@siri,

You can't test window based application using selenium. It's for web based application only.

Andrew said...

Thanks, brother!

This was what I needed. Perfect.

Unknown said...

Hi,
I'm doing automation of web application using the Selenium RC in IE9. I'm able to open the new window and selectwindow() command is also working but when I want to test something on the new window I'm not able to do that it is also opening a blank page when trying to test anything on the new window but able to switch to the original window and perform testing on that window but not the newly open window. can you please help me how to close that blank page.

Unknown said...

Hi,

I have problem. I'm doing the automation of web application using selenium RC in IE9. When I'm opening a new window it is also opening a blank page and i'm not able to test anything on the new window but i'm able to switch to the original window and perform testing. So, can you please help me how to handle the blank page. this scenario is working in FF but not in IE9.

Unknown said...

Hi , I have used below code to click on hidden element
WebElement tmpElement= driver.findElement(By.xpath("//*]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", tmpElement);
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
driver.findElement(By.name("name")).sendKeys("Tesintgfg");
driver.findElement(By.name("number")).sendKeys("222");
driver.findElement(By.id("name")).sendKeys("Ts");

once it is clicked it focuses to new window but iam not able to focus to new window.can anybody help

Post a Comment