One of the most common issues Selenium users look to Sauce Labs for assistance on is how to deal with popup windows, or even how to create your own new windows to do something in a shared session that could be connected with the main flow of your test.
In this week’s tip, we’re going to explain how to handle new windows to run tests like this one recorded with our cloud hosted Selenium service, Sauce OnDemand.
For multiple window tasks, Selenium provides the following commands:
- openWindow(url, windowId): Will open your new window
- waitForPopup(windowId, timeout): Will wait for the window to be opened and loaded
- selectwindow(windowId): Will focus on this windows and any future actions will act on it
So for example, we could create the above test using Sauce IDE to run on http://google.com:
| open | / | |
| type | q | sauce labs |
| click | btnG | |
| waitForTextPresent | Sauce Labs | |
| openWindow | /search?q=santiago | santiago |
| waitForPopUp | santiago | 30000 |
| selectWindow | santiago | |
| assertTitle | santiago – Google Search | |
| selectWindow | null | |
| assertTitle | sauce labs – Google Search |
However, due to an active bug, IDE won’t be able to play back that test as written. To fix that, we’ll export this script to a programming language like Java and run in on Sauce OnDemand.
selenium.open("/");
selenium.type("q", "sauce labs");
selenium.click("btnG");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isTextPresent("Sauce Labs")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
selenium.openWindow("/search?q=santiago", "santiago");
selenium.waitForPopUp("santiago", "30000");
selenium.selectWindow("santiago");
assertEquals("santiago - Google Search", selenium.getTitle());
selenium.selectWindow("null");
assertEquals("sauce labs - Google Search", selenium.getTitle());
The script will work as expected, opening the window and doing the parallel work that you expected it to do.
Notice that at the end of the script, we are using selectWindow(“null”), which means that we want to focus on the main window back.
Did you find this tip useful? Need some help adapting this to your needs? Let us know in the comments!
Until next time, happy testing!
Related posts:
Selenium Tips: Parametrizing Selenese tests

I think this tip will be very helpful. My struggle is dealing with windows and pop ups that do not have IDs or titles. How do you handle those situations?
Keep up the great work!
Carl
Thanks Carl :)
I’ll be working on addressing those cases soon, so stay tuned!
Carl you could use the xpath or css to locate them
HTH
jeff
Has any one used selenium to open a web page from a working page and
doing some processing there and bringing control back to the main
page. (NOT a pop up window but a web page.) like by clink a link on a
page which opens another page.
Hello,
I have added the same commands as mentioned above and here is the source:
openWindow
http://www.test.com/privacy/
privacy
waitForPopUp
privacy
3000
selectWindow
privacy
When I executing this through IDE, I am getting the following error:
[error] Could not find window with title privacy
Why is that this is not working as expected? We have decided to use HTML format for all our test cases.. is there any workaround for this?
Please suggest.
Thanks in advance,
Srinivas.
Thanks for your valuable post, But selenium.select(“null”) command not working for me so I cant focus on main window back could you pls help in this
How to close pop up windows.Is there any command for closing pop up window while play back
I have a search button.After entering the search criteria and clicking on search button opens the search results in the same page. Whatever actions I perform in the second page is not working in selenium RC(Java client driver).Execution result is neither pass nor fail. ie execution will never stop unlss the selenium server is forcibly shut down…
Can you please help me in closing this issue???
selenium.open(“/package-holidays.html”);
selenium.select(“calendar_day”, “label=26″);
selenium.select(“calendar_month_year”, “label=Dec 2011″);
selenium.select(“duration”, “label=21 nights”);
selenium.select(“departureAirportCode”, “label=Any London”);
selenium.select(“countryDestCode”, “label=Spain”);
selenium.click(“searchbutton”);
selenium.waitForPageToLoad(“30000″);
selenium.selectWindow(“null”);
selenium.click(“//a[@id='chooseandcontinue_000974132497100000013249857000001222132680370000013268118000001223']/span[2]“);
The commands that are written after clicking on the search button are not being executed.
Thanks a lot! selenium.selectWindow(“null”); worked for me.