Wednesday, February 23, 2022

How to Open and Close Tab in Selenium WebDriver

Introduction 


In the modern era of Web Development, most web applications allow users to open multiple tabs from their landing page to give a better user experience rather than using back and forward buttons frequently.

While users effectively use multiple tabs from a testing perspective we have to test clearly the scenarios where the user moves to a new tab, from the current page, is able to interact in the new tab, comes back to the parent window without any hassle or loss of data.


When it comes to testing web applications, Selenium is the preferred choice of framework across industries for web automation. This open-source framework works with popular programming languages and browsers and can be integrated with other frameworks like JUnit, TestNG, PyTest, and PyUnit. 


Here I will explain how to open a new tab using Selenium WebDriver On Java language binding and TestNG Framework.

I have set up my project workspace in IntelliJ IDEA  as a Maven Project.


import io.github.bonigarcia.wdm.WebDriverManager;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.WindowType;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.annotations.Test;



public class ClosingTabDemo {


    @Test

    public  void closeTabTest(){

        WebDriverManager.chromedriver().setup();


        WebDriver driver= new ChromeDriver();

        driver.manage().window().maximize();

        driver.get("https://www.lambdatest.com/selenium-playground/");

        String urlToOpeninNewWindow=driver.findElement(By.linkText("Simple Form 

        Demo")).getAttribute("href");

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

        String newWindowURL=driver.getCurrentUrl();

        System.out.println("newWindowURL  "+newWindowURL);

        driver.close();//Check Whether its close only newly open tab

        System.out.println("currentWindow   "+driver.getCurrentUrl());


    }

}



Code walkthru


WebDriverManager.chromedriver().setup();



WebDriverManager is an openSource library that helps to avoid setting up driver exe in the system path.



 WebDriver driver= new ChromeDriver();

 driver.manage().window().maximize();

 driver.get("https://www.lambdatest.com/selenium-playground/");





driver. get() method launch the URL in the newly opened session using the driver object once it is maximized.



String urlToOpeninNewWindow=driver.findElement(By.linkText("Simple Form 

Demo")).getAttribute("href");



On the above page, to open the "Simple Form Demo "  link on the new page, first, get the element (here I have used linkText identifier ) and then get the value of attribute "href".



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


WebDriver objects have methods to switch to new windows, frames, and JavaScript alerts. Here I have mentioned switching to a new window type of Tab.


Once the new tab is opened and the driver object gets focused on the newly opened tab, can open a URL in this new tab using the get method.


The console output statements help to print the URL of the first and new tab page URL to check whether its moved driver object focuses in the new tab or not. We can add TestNG assertions also to compare the current URL of the page.


driver.close();


When the driver object is focused on the newly opened tab, close() methods close only the active window session. 

If we want to close all the window sessions in one statement, then we can use the driver.quit() method.



Conclusion

Testing user experience in our web application is as important as testing application functionality. This post gives a better understanding of how the selenium framework test navigates to a new tab, closes the newly opened tab, and comes back to the parent window. 


Happy Testing!







No comments:

Post a Comment