Windows / Tab management

In this tutorial, we will discuss about advanced Windows / Tab management in Selenium 4. Addition to this we will execute some test script to implement the window and Tab Management.

Windows / Tab management

Now a days there are several scenarios in Automation Testing where we need to work on new Tab or new Window. To work with this scenario we need to open new Tab and new Windows through our Test script.

These test scenarios need to be performed many times while working in big project .

To achieve this scenario in selenium 3, We had to create new driver reference object. After creation of driver object reference, we need to switch operation using windowhandle() method and proceed with sub-sequence steps.

This is little complex process in selenium 3 to perform this test scenario. But in case of selenium 4, it becomes very easy and well maintained.

In Selenium 4, a new API called newWindow added to achieve the scenarios related to window and tab management. This allows to create and switch to the new window or Tab without creating any new webdriver object reference.

We need to pass one argument in newWindow() method which is WindowType.TAB or WindowType.WINDOW.

Whenever we need open a new Window through the test script then we will use the WindowType.WINDOW parameter in newWindow() method.

Like window, if we need to open a new TAB through the test script then we will use the WindoeType.TAB parameter in newWindow() method.

Syntax :

WebDriver newWindow = driver.switchTo().newWindow(WindowType.WINDOW);
 WebDriver newWindow = driver.switchTo().newWindow(WindowType.TAB);

Let’s understand the Window management and Tab Management with an simple test script.

Test Scenario : <New Window>

In this Test Scenario, we will open a website named “https://phptravels.org/index.php?rp=/login” with webdriver object. Once the website open, we will open a new Window and enter another website named “https://www.testinglpoint.com” in new window.

Let’s have a look on below Test Script.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class New_WIndow {

	public static void main(String[] args) {
		  System.setProperty("webdriver.chrome.driver", "chromedriver.exe");  
		  WebDriver driver = new ChromeDriver();
		 
		  driver.get("https://phptravels.org/index.php?rp=/login");
		  WebDriver newWindow = driver.switchTo().newWindow(WindowType.WINDOW);
		  newWindow.get("https://www.testinglpoint.com");
		  
		  WebDriver newWindow1 = driver.switchTo().newWindow(WindowType.TAB);
		  newWindow1.get("https://www.facebook.com");
		 }
}

Explanation:

In the above example, you can notice that we we have opened a new window by using switchTo() and newWindow() method. And WindowType.WINDOW will open a new Window and we can perform the task based on the requirement on the newly opened window as it is focus now.

Test Scenario : <New TAB>

In this Test Scenario, we will open a website named “https://phptravels.org/index.php?rp=/login” with webdriver object. Once the website open, we will open a new TAB and enter another website named “https://www.testinglpoint.com” in new TAB.

Let’s have a look on below Test Script.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class New_TAB {

	public static void main(String[] args) {
		  System.setProperty("webdriver.chrome.driver", "chromedriver.exe");  
		  WebDriver driver = new ChromeDriver();
		 
		  driver.get("https://phptravels.org/index.php?rp=/login");
		  WebDriver newWindow = driver.switchTo().newWindow(WindowType.TAB);
		  newWindow.get("https://www.testinglpoint.com");
		  
		 }
}

Explanation:

In the above example, you can notice that we we have opened a new TAB by using switchTo() and newWindow() method. And WindowType.TAB will open a new TAB and we can perform the task based on the requirement on the newly opened TAB as it is focus now.  

In this tutorial we have understood about the Windows / Tab management in selenium 4. We will discuss on other advance feature of selenium 4 which makes the testing world smooth.

Leave a Reply

Your email address will not be published. Required fields are marked *