Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Selenium WebDriver is a popular tool for automating web testing with many functionalities. One of the common actions when automating the web is to open a new tab in the browser window.

Opening a new tab can be useful in various scenarios, including testing multi-page workflows, verifying external links that open in a new tab, interacting with pop-up windows, and simulating multiple users interacting with different parts of the application simultaneously when running tests in parallel.

Earlier solutions were custom scripts like sending a combination of keys, such as “Ctrl” + “T,” which usually led to different results depending on the browser and operating system.

In this tutorial, we’ll explore stable approaches for opening a new tab – the newWindow() API introduced in Selenium 4 and JavaScript code execution.

2. Using the newWindow() API

Selenium 4 introduced a powerful and flexible API method newWindow(), for creating a new window in the current browser session. It allows us to open a new browser tab and switch to it automatically. This method takes a WindowType parameter WINDOW or TAB and creates it. The syntax is pretty straightforward:

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

3. Using JavaScript

Another approach to open a new tab with Selenium WebDriver is executing JavaScript code. It involves using the executeScript() method of the JavascriptExecutor interface, which allows us to run JavaScript code directly within the browser. The window.open() script is useful when we want more control over the new tab, such as specifying the URL to open.

Here’s how to use this approach to open a new tab:

((JavascriptExecutor) driver).executeScript("window.open()");

And the how to open a new tab with URL:

((JavascriptExecutor) driver).executeScript("window.open('https://google.com')");

It’s important to keep in mind that the driver will still be focused on the original tab after the window.open() method is executed. In order to interact with elements on the new tab, we need to switch the driver’s focus to that tab using the driver.switchTo().window() method.

Here’s an example of switching to the new tab after opening it with JavaScript:

String newTab = driver.getWindowHandles()
  .stream()
  .filter(handle -> !handle.equals(originalTab))
  .findFirst()
  .get();
driver.switchTo().window(newTab);

4. Conclusion

In this article, we explored two approaches to open a new tab with Selenium: the newWindow() method introduced in Selenium 4 and the window.open() method executed with JavaScript.

The newWindow() method is a new API introduced in Selenium 4 that makes creating a new tab or window simple and intuitive. On the other hand, executing window.open() with JavaScript provides greater control over how the new tab is opened and can be used with earlier versions of Selenium. However, it may require more code and be more difficult to use, particularly for beginners.

As always, the code examples are available on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!