getWindowHandle- Handle multiples windows in Selenium

Category: Selenium WebDriver 34 0

In the previous post, we discussed different types of alerts. We also learned the commands to handle alerts when they pop up. Now today we are going to discuss how to handle multiple windows in Selenium using Java. There are many scenarios where new windows appear so in that case Selenium WebDriver does not automatically start its operation on new windows; rather we have to give commands to switch to the new window to perform further testing activities. Selenium library has a command, named, getWindowHandle & getWindowHandles to get the address of new window(s).

After getting the address of new windows we need to use switch commands (that we also used previously to switch to Alert pop up). There are different techniques in Java programming to handle multiple windows; we will look into each technique one by one. But, before that, I will make you understand how to proceed with this scenario by writing its test steps.

Test steps to proceed with handling multiple windows by using getWindowHandle

Step#1: You are working on your default window

Step#2: You click on some web element on default window and new window appears after the click

Step#3: The focus will remain on default window, so change the focus to new window

Step#4: Now complete all the testing activities on new window

Step#5: Again you need to move the focus back to the default window to continue the testing activities

What are the scenarios where handling multiple windows takes place?

Many newbies ask this question because they are not much aware of the web application. Hence, there are no specific scenarios where handling multiple windows occurs. But for your reference, click on below button you will see a new window, Thus, you can assume how the situations would be like!

Let me tell you some generic situations where you may face scenarios to handle multiple windows. Those generic scenarios are listed below:

  • Advertising windows, example, Naukri.com
  • Date picker in separate window
  • Clicking on any link which leads to open a new window
  • Document upload window
  • Picture upload window

 

getWindowHandle

How to initialize getWindowHandle command?

The return type of getWindowHandle is String. Hence, here is the initialization when we are taking the reference to default window.

How to store the reference to multiple child windows?

Tip:

Let me tell you little about TreeSet. This is a part of Java Collection framework. TreeSet stores unique elements in an ascending order and their retrieval time is very fast.

Here to store multiple child windows, we use TreeSet in String so that reference to each window will get unique storage. Hence, its initialization would be like-

Here we are using getWindowHandles, instead of using getWindowHandle.

How to switch between multiple windows in WebDriver?

We have discussed ways to store the reference to default window as well as multiple child windows; now we have to switch to windows based on the test requirement. So, we use switchTo() command to perform switching operations among windows. Here is the switching declaration:

Replace String windowReference by its window handles

Programming approach to handling multiple windows

There won’t be any difficulty if you have to take a reference to default window or your parent window. Also, it will be easier to switch to the parent window. Now in case of handling multiple child windows, we take two approaches:

Approach# 1: By using For-Each loop

Approach# 2: By using While loop with Iterator

Let’s discuss each of the approaches one by one.

Approach# 1: By Using For-Each loop

 

Approach# 2: By using While with Iterator

 

I don’t think there should be any issues with Approach# 1 because it is a simple for-each loop. Therefore, I will explain the Approach# 2.

Here Iterator traverses over entire String and creates a reference to each index of the String. hashNext () used here to check whether there is any next object or not. If there is any object then next() just jump to the upcoming object and returns the output.

Here is the code snippet for you as a reference for your understanding:

 

Related Articles

Add Comment