getWindowHandle- Handle multiples windows in Selenium

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.

String defaultWindow = driver.getWindowHandle();

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-

Set<String> childWindow = driver.getWindowHandles();

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:

driver.switchTo().window(String windowReference);

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

 String defaultWindow = driver.getWindowHandle();
  	  
  	  System.out.println("Default window name is- " +defaultWindow);
  	  
  	  Thread.sleep(3000);
	  
  	  Set<String> childWindows = driver.getWindowHandles();
  	  
  	  for(String child : childWindows){
  		  
  		  if(!child.equalsIgnoreCase(defaultWindow)){
  			  driver.switchTo().window(child);
  			  System.out.println("Child windows- "+child);
  		  } else {
  			  System.out.println("There are no child windows");
  		  }
  		  
  		  
  	  }

 

Approach# 2: By using While with Iterator

String defaultWindow = driver.getWindowHandle();
  	  
  	  System.out.println("Default window name is- " +defaultWindow);
  	  
  	  Thread.sleep(3000);
	  
  	  Set<String> childWindows = driver.getWindowHandles();
  	  
  	  Iterator<String> iterator = childWindows.iterator();
  	  
  	  while(iterator.hasNext()){
  		 String child = iterator.next();
  		 
  		 if(!child.equalsIgnoreCase(defaultWindow)){
 			  driver.switchTo().window(child);
 			  System.out.println("Child windows- "+child);
 		  } else {
 			  System.out.println("There are no child windows");
 		  }
  	  }

 

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:

package SeleniumTester;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class GetWindowHandle {
  @Test
  public void windowHandleDemoq() throws Exception { 
	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  	 
  	  WebDriver driver = new ChromeDriver();
  	  
  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  	  
  	  driver.get("https://www.naukri.com/");
  	  
  	  driver.manage().window().maximize();
  	  
  	  Thread.sleep(4000);
  	  
  	  String defaultWindow = driver.getWindowHandle();
  	  
  	  System.out.println("Default window name is- " +defaultWindow);
  	  
  	  Thread.sleep(3000);
	  
  	  Set<String> childWindows = driver.getWindowHandles();
  	  
  	  Iterator<String> iterator = childWindows.iterator();
  	  
  	  while(iterator.hasNext()){
  		 String child = iterator.next();
  		 
  		 if(!child.equalsIgnoreCase(defaultWindow)){
 			  driver.switchTo().window(child);
 			  System.out.println("Child windows- "+child);
 		  } else {
 			  System.out.println("There are no child windows");
 		  }
  	  }
	  
  	  driver.close();
  	  
  	  driver.quit();
  }
}

 

Leave a Reply