Skip to main content

Handling PopupWindows, modal dialogs etc

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.
1.selenium.selectWindow("Google Window"); // select new window
2.//Do whatever you want to do on new window
3.Selenium.close(); //Close the newly opened window
4.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.
view sourceprint?
1.selenium.waitForPopUp("GoogleWindow""30000");
2.selenium.selectWindow("GoogleWindow");
3.//Execute steps on new window
4.selenium.close(); //close the new window
5.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.
view sourceprint?
1.selenium.selectWindow(getAllWindowNames()[1]);
OR
view sourceprint?
1.selenium.selectWindow(getAllWindowTitles()[1]);
OR
view sourceprint?
1.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.
view sourceprint?
1.Selenium.openWindow("","MyWindow");
2.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.
view sourceprint?
1.Selenium.openWindow("http://google.co.in","MyWindow");
2.Selenium.selectWindow("MyWindow")

Comments

Popular posts from this blog

Some good Resources / Blogs / Sites for Selenium Users

Here I have listed out some good blogs and sites by extensive selenium automation users. Hope these will help you a lot. http://automationtricks.blogspot.com  - by NirajKumar http://www.theautomatedtester.co.uk/ http://testerinyou.blogspot.com   - by Naga/Mathu http://seleniumready.blogspot.com  - by Farheen Khan http://seleniumdeal.blogspot.com/  - Amit Vibhuti http://seleniumexamples.com/blog Sauce Labs and BrowserMob are companies doing cloud and extensive selenium automation services, products, etc http://saucelabs.com/blog http://blog.browsermob.com http://testingbot.com/ Cedric Beust -  creator of the TestNG Java testing framework. http://beust.com/weblog/ http://blog.reallysimplethoughts.com/  - by Samit Badle, Created many Selenium IDE Plug-Ins Available Colud Testing: 1. SauceLabs 2. Soasta 3. BrowserMob 4. CloudTesting.com  etc. Selenium Testing Products: 1. Twist by ThoughtWorks 2.  TestMaker by  PushToTest 3. Element34 company providi

UFT - Take full page screenshot by scrolling the page

'######################################################################################## 'This is navigate through the full page and taking individual screenshot of visible area '######################################################################################## Function TakeScreenshot Dim intScrolls, intScroll, strScrollPos Set pgApp = Browser ( " " ) .Page ( " " ) intScrolls = Round ( pgApp . RunScript ( " document.documentElement.scrollHeight / (screen.height) " ) , 2 ) If intScrolls < 1 Then intScrolls = - 1 pgApp . RunScript " window.scrollTo(0, 0); " Wait 1 Browser ( " " ) .CaptureBitmap " C:\screenshot0.png " , True For intScroll = 0 To intScrolls If Environment . Value ( " Browser " ) = " CHROME " Then strScrollPos = " scrollY " Else strScrollPos = " document.documentElement.scrollTop " End If If p

Change IE Browser ZOOM settings

Lot of UI automation testers could have faced this problem as we could change the zoom settings while operating manually for our convenience and forgot to reset to 100%. But our QTP and some other related tools would operate the browser perfectly if browser zoom is 100%. So wee need to change the zoom before start to run the scripts. Its better to have a code snippet in our framework to change this zoom setting right? Here we go... 1. We can simply change the Registry values before Invoking IE Function  ChangeRegistry   Dim  objShell   Set  objShell =  CreateObject ( "WScript.Shell" )  objShell.RegWrite  "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Zoom\ZoomFactor" ,  "100000" ,  "REG_DWORD"   Set  objShell =  Nothing End   Function This option is very useful. But in real time, lot of customers could have restricted write access to windows registry. So we can try other options. 2. Use IE COM object and Change