Quality Engineering
min read
May 24, 2023
May 23, 2023

How to handle multiple windows with Selenium 4

How to handle multiple windows with Selenium 4
Table of contents

Opening an URL in a new window or new tab is one of the features provided by Selenium 4. In Selenium 3, we can achieve this by performing switch operation using the WindowHandle method. In Selenium 4, this is going to change by using the new API newWindow.

So why do we need to create a new tab/window?

Let’s consider a test scenario where you have two interfaces, one for admin and another for users. As a user, you need to perform some steps then you have to log in as an admin. As an admin, if you do some configuration changes, these changes should be visible to the user. In such cases, we can open the website in one window and log in as a user. Then we can open the same website in another window and log in as an admin. Once the admin performs the configuration changes, we can go back to the user window and verify whether the changes are reflected.

How to handle the new tab/window in Selenium 4?

Selenium 3 vs Selenium 4

In Selenium 3, we needed to create a new WebDriver object and then switch to a new tab or window. But with Selenium 4, we can avoid creating new WebDriver objects and directly switch to the required window. We have a new API called newWindow in Selenium 4. This API creates a new tab/window and automatically switches to it.

New Tab

Here’s how you can create a new tab in Selenium 4 and switch to it:


driver.switchTo().newWindow(WindowType.TAB);

Let’s consider that you are on the QED42 homepage and you need to switch to another URL in a new tab.

Here is the snippet to open an URL in the new tab:


@Test
public void openNewTab() {
	WebDriver newTab = driver.switchTo().newWindow(WindowType.TAB);
	newTab.get("https://www.google.com/");
	System.out.println("New Tab Page Title:" + newTab.getTitle());
}

New Window

Creating a new window in Selenium 4 and switching to it:


driver.switchTo().newWindow(WindowType.WINDOW);

Similar to a new tab, here is the snippet to open an URL in the new window:


@Test
public void openNewWindow() {
	WebDriver newWindow = driver.switchTo().newWindow(WindowType.WINDOW);
	newWindow.get("https://www.facebook.com/");
	System.out.println("New Window Page Title:" + newWindow.getTitle());
}

Switching back to the Original Window

Suppose, there are multiple tabs and windows open and you need to switch to a particular or original window/tab. This can be achieved using driver.switchTo().window(windowHandle) method. 

Let’s implement the below steps to understand it:

  1. Load QED42 homepage using driver.get("https://www.qed42.com/")
  2. Open a new tab and load the other URL "https://www.google.com/"
  3. Next, open a new window and load "https://www.facebook.com/"
  4. Switch to the original window (step #1) by looping the windowhandles based on certain conditions and selecting the original window handle.
  5. Close the driver.

package example.test;

import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class WindowHandling {
    public WebDriver driver;
    String originalWindow;

    @BeforeClass
    public void setUp() {
	WebDriverManager.chromedriver().setup();
	driver = new ChromeDriver();
	driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
	driver.get("https://www.qed42.com/");
	originalWindow = driver.getWindowHandle();
	System.out.println("Original Window Page Title:" + driver.getTitle());
    }

    @AfterClass
    public void afterClass() {
       driver.close();
       driver.quit();
}

    @Test
    public void switchToOriginalWindow() {
	driver.switchTo().newWindow(WindowType.TAB);
	driver.navigate().to("https://www.google.com/");
	System.out.println("New Tab Page Title: " + driver.getTitle());
		
	driver.switchTo().newWindow(WindowType.WINDOW);
	driver.navigate().to("https://www.facebook.com/");
	System.out.println("New Window Page Title: " + driver.getTitle());
		
	// Loop through until we find a original window handle
	for (String windowHandle : driver.getWindowHandles()) {
		if (originalWindow.contentEquals(windowHandle)) {
			driver.switchTo().window(originalWindow);
			break;
		}
	}
	System.out.println("Original Window Page Title after switch: " + driver.getTitle());
    }
}

Below is the console output:

Selenium 4

Point to note: Closing a window will leave WebDriver executing on the closed page and will trigger a No Such Window Exception. Therefore, you must switch back to a valid window handle in order to continue execution.

The sample codes are available at the Github repository here.

Conclusion

Selenium 4 provides better window/tab handling using a new API. We can now create a new tab/window and automatically switch to it with fewer lines of code and without creating a new WebDriver object. 

Written by
Editor
No art workers.