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<String> 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<String> set=driver.getWindowHandles();
//to get the value from set object, we will use iterator
Iterator<String> 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<String> set=driver.getWindowHandles();
 //to get the value from set object, we will use iterator
 Iterator<String> 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