×

Selenium WebDriver – Window handling

Selenium WebDriver – Window handling

In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver.

We have different types of window, and pop-ups handling present in selenium WebDriver.

  • New window/New tab
  • Alert

Now we see the above points one by one to give you a better understanding of how we will handle all the pop-ups in selenium WebDriver.

New Window /New tab:

Selenium WebDriver does not have the feature of switching the control from one window to another window directly.

To handle the window in selenium web driver, we need a unique string value that identifies the browser window on desktop uniquely.

We need to take the help of 2 methods which is provided by the WebDriver interface to perform these operations,

  • getWindowHandle()
  • getWindowHandles()

getWindowHandle():-

This method is used to get the currently active window Id.

The return type of this method is String.

getWindowHandles()

This method is used to get all the window Id’s, which is open by the WebDriver.

The return type of this method is Set of String.

Set command= driver.getWindowHandles();

getWindowHandles()

Note: To switch the control from one window to another window, we need to use the “switchTo()” method.

driver.switchTo().window(String);

And, we will also use the close() and quit() method while handling windows in selenium WebDriver.

close():-

It is used to close the currently active window.

quit():-

It is used to close all the browsers which are opened by a WebDriver.

Let us take one example to give you a better understanding of the new window /new tab, and we created a sample test script which is as follows;

For our testing purpose, we are using the demo.automationtesting.in website to perform all the actions for new window handling in selenium WebDriver.

In this test case, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1. Open the Firefox Browser. System.setProperty()   The Firefox browser must be opened.
2. Navigate to the. get() http://demo.automationtesting.in/Windows.html The home page window must be displayed.
3. Identify the main window.     The main window should be identified.
4. Handling all the new opened window. getwindowHandles()   All the new windows should be handled and return the no of the window opened by the web driver.
5. Switching to the child window and print the title of the child window. switchTo()   The child window should come after the switching, and the title also be printed in the console window of the Eclipse.
6. Closing the child window. close()   The child window should be closed.
7. Switching to the parent, i.e., the main window, and print the title of the parent window. switch.To()   The parent window should be switched, and the title also printed in the console window of the eclipse.
8. Closing the parent window. close()   The parent window should be closed.

Open the Eclipse IDE and the existing test suite new_test, which we have created in WebDriver Installation section of the WebDriver tutorial.

Then, right-click on the src folder and create a new Class File from New ? Class.

Eclipse IDE and the existing test
  • Give the Class name as Win_handle and click on the Finish button.

Step1:

To launch the Firefox browser, we need to download the geckoDriver.exe file and set the system property to the path of geckoDriver.exe file.

Here is the code for setting the system property of the Firefox Browser:

// System Property for gecko Driver   System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-v0.25.0-win64\geckodriver.exe");
// create an object for FirefoxDriver class.      
WebDriver driver=new FirefoxDriver(); 

Step2:

Now, we will get to the desired URL:

//navigate to the website.
driver.get("http://demo.automationtesting.in/Windows.html"); 

Step3:

After that, we will move to our next step, where we are print the main window title and identify the click button, which is presented on the main window of the website.

To identify the click button first, right-click on the click button and select the Inspect element field.

 main window of the website.

The developer tool window will be launched, with all the specific codes used in the development of the click button.

specific codes used in the development

Here the sample code,

//print the title of the main window
System.out.println("the title of the main window :"+ driver.getTitle());
WebElement wb=driver.findElement(By.xpath("//*[@id='Tabbed']/a/button"));
wb.click();
Thread.sleep(3000); 

Step4:

Once we identify the main window, we will handle the new window using the getWindowHandles() method and get all the window id’s which is opened by the WebDriver.

Note: the Iterator is used to get the value from the set object.

//It returns the no. of the windows opened by the web driver and handle all new opened window.
Set set=driver.getWindowHandles();
//to get the value from set object, we will use iterator
Iterator it=set.iterator();
String parentId=it.next();
//print the parent window id
System.out.println("parent window pop-up id"+ parentId);
String childId=it.next(); 
//print the child window id
System.out.println("child window pop-up id" +childId); 

Step5:

After that, we will switch to the child window using the switch.To() method, and printing the title of the child window.

Here the sample code,

//switch to child window
driver.switchTo().window(childId);
//print the title of child window
System.out.println("child window pop-up title:"+ driver.getTitle());
Thread.sleep(3000); 

Step6:

Now, we are closing the child window,

//close the child window 
driver.close();

Step7:

In this step, we are switching to the parent window, which is also known as the main window, and print the title of the window for no further confusion.

Here the sample code,

//switch to parent window that is main window
driver.switchTo().window(parentId);
//print the title of parent window
System.out.println("parent window pop-up title:"+ driver.getTitle());
Thread.sleep(3000); 

Step8:                                                                                 

Finally, we close the parent window.

 //close the parent window
 driver.close();

After completing all the steps, our final test script will look like this:

package testpackage;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver; 
 public class Win_handle {
 public static void main(String[] args) throws InterruptedException {
 // System Property for gecko Driver   
 System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-v0.25.0-win64\geckodriver.exe");
 // create an object for FirefoxDriver class.       
 WebDriver driver=new FirefoxDriver(); 
 driver.manage().window().maximize(); 
 //navigate to the url
 driver.get("http://demo.automationtesting.in/Windows.html");
 //print the title of the main window
 System.out.println("the title of the main window :"+ driver.getTitle());
 WebElement wb=driver.findElement(By.xpath("//*[@id='Tabbed']/a/button"));
 wb.click();
 Thread.sleep(3000); 
 //It returns the no. of the windows opened by the web driver and handle all new opened window.
 Set set=driver.getWindowHandles();
 //to get the value from set object, we will use iterator
 Iterator it=set.iterator();
 String parentId=it.next();
 //print the parent window id
 System.out.println("parent window pop-up id"+ parentId); 
 String childId=it.next();
 //print the child window id
 System.out.println("child window pop-up id" +childId);
 //switch to child window
 driver.switchTo().window(childId);
 //print the title of child window
 System.out.println("child window pop-up title:"+ driver.getTitle());
 Thread.sleep(3000); 
 //close the child window 
 driver.close();
 //switch to parent window that is main window
 driver.switchTo().window(parentId);
 //print the title of parent window
 System.out.println("parent window pop-up title:"+ driver.getTitle());
 Thread.sleep(3000);
 //close the parent window 
 driver.close();                                                                    
 }
 }
  • To run the test script in Eclipse, right-click on the window and then click Run as ? Java application, and the test script will be launched in the Firefox browser and automate all the test scenarios.
Eclipse, right-click on the window

And the output of all the print command is as shown below,

output of all the print command

Related Topics

Selenium WebDriver- Microsoft Edge Browser

Selenium WebDriver- Microsoft Edge Browser: In this tutorial, we will learn how to launch the Microsoft Edge browser in the Selenium WebDriver. Before we start automating our test script with Microsoft Edge...

4 minutes read.

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

6 minutes read.

CSS Selector Locator in Selenium IDE

It used to identify the web element based on HTML tag, id, class, and attributes. To describe the look and formatting of a document written in the markup language are known...

1 minute read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

2 minutes read.

CSS Selector: Class in selenium IDE

CSS Selector: Class Identify a web element with the help of Class as a CSS Selector is like using id attribute. The only difference between Class and id is in their...

2 minutes read.

Selenium WebDriver-CSS Selector: Tag and ID

Selenium WebDriver-CSS Selector: Tag and ID We will locate the web element with the help of tag and id CSS selector in this section of the tutorials Where Tag and id are...

4 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium WebDriver Characteristics

Characteristics of Selenium WebDriver Some of the essential characteristics of Selenium WebDriver are as follows: Multiple Operating System Support Selenium Webdriver supports multiple operating systems like: Desktop OS: Window, Linux, and macOS Mobile OS:...

2 minutes read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 minutes read.

Difference between Selenium WebDriver and Selenium RC

Difference between Selenium WebDriver, and Selenium RC: Selenium WebDriver and selenium RC (Remote Control) both are different in any manners which we see later in this tutorial, let us see the...

3 minutes read.

Selenium Assertion

Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver. An Assertion is a feature available in TestNG, which is used to verify the expected result of...

7 minutes read.

WebDriver-Partial Link Text locator

WebDriver-Partial Link Text locator In selenium WebDriver, there is an additional way to find a web link element with the help of partial link text locator. The partial link text is used...

4 minutes read.

WebDriver-Link Text locator

WebDriver-Link Text locator The Link text locator is used to identify only web link elements based on the value of the link. The Link text locators cannot be used for other than...

4 minutes read.

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute read.

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

Drag and Drop Event Handling

Drag and Drop Event Handling: In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class. Before going further in this tutorial...

6 minutes read.

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

4 minutes read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Drawback or Limitations of Selenium

Drawback or Limitations of Selenium Followings are the Drawback or limitations of Selenium:- Drawback /LimitationsDescriptionDesktop applications cannot be automated by Selenium.To test the desktop application, we have tools like Test Complete, Winnium, and...

2 minutes read.