Technical Project Lead @ Huawei Technologies

My photo
Bangalore/Hyderabad, Karnataka/Andhra Pradesh, India
Have an overall 13 + yrs of experience Currently working as an Senior Architect.Expertise in design,implementation of Automation frameworks for Opensource automation tools like Selenium,Jmeter, Webdriver,Appium, Robotium Expertise in integrating Test & ALM Management tools like Rally,Qmetry,JIRA-Zephyr with Automation frameworks Expertise in design and developmenet Mobile Automation frameworks for tools like Appium, Robotium Remote Control,Cucumber BDD,Manual Testing

Followers

Tuesday, September 7, 2010

How to Handle Windows if there are no titles


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

    * Title
    * Name
    * Variable Name (variable to which window is assigned)
    * Inbuilt APIs to retrieve Windows Names, Titles and IDs

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.

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.

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

1.selenium.selectWindow(getAllWindowTitles()[1]);
OR
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.

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.

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




 ****Note : This article has been picked from Gauraung Shah's blog. Thanks!!! for his very detailed explanation

4 comments:

  1. Hi Kiran..i have a question ....where did u get this command
    window.open("http://google.co.in","Google Window");

    how to identify the window name or ID can u provide an example?

    ReplyDelete
  2. It's not selenium command. Its a JavaScript which will open window

    ReplyDelete
  3. Kiran,

    Just curious to know how to inject javascript code to webdriver ? Please help me in lay man terms, thanks :)

    ReplyDelete
  4. Sorry Prasanna, It's been long time that I had visited my blog. Hope you might have already got the way to inject java script to webdriver.If not check the below to inject java script using JavaScript Executor
    Driver driver = new FireFoxDriver;
    (JavascriptExecutor) driver..executeScript('document.getElementById('login')";

    ReplyDelete