Handle Multiple Windows In Selenium

Learn via video courses
Topics Covered

Overview

Window handling in Selenium refers to the process of switching between multiple windows or tabs during test execution. When a web application opens a new window or tab, Selenium WebDriver assigns a unique identifier called a "window handle" to each window. To interact with elements in a different window, testers must switch to that window using its handle. Selenium provides built-in methods such as getWindowHandles() and getWindowHandle() to manage window handling and ensure the smooth execution of automated tests. Proper window handling in Selenium is crucial to ensure the accuracy and reliability of automated test results.

Introduction

In Selenium, window handling is a technique used to handle multiple windows that are opened during the execution of a test case. When we click on a link or button that opens a new window, the focus of the driver switches to the new window, and we need to handle the new window to perform further actions.

Window handling in Selenium is essential because many web applications open multiple windows during use. With proper handling of these windows, we may be able to interact with the application as intended.

What is a Window in Selenium?

In Selenium, a window refers to a web page or pop-up window opened within the web browser. When a user interacts with a web application, multiple windows, such as pop-ups, new tabs, or child windows, may be opened. The Selenium WebDriver provides methods to interact with these windows, such as opening, closing, and switching between them. Each window is identified by a unique window handle that can be used to switch focus between different windows. This allows testers to perform various actions, such as verifying the content of multiple windows or testing the behavior of pop-up windows. There are two types of windows in Selenium - parent windows and child windows.

  • A parent window is the window that the WebDriver initially opens. It is the primary window to perform actions like navigating to a URL, entering text, or clicking on elements. When clicking a link or button on the parent window, it may open a new child window.
  • A child window is the window that is opened by clicking a link or button on the parent window. Child windows depend on the parent window and are typically used to display additional information or perform a specific task. When a child window is opened, the focus of the WebDriver shifts from the parent window to the child window.

How do We Identify Parent Windows and Child Windows?

In Selenium, a window is identified by a unique window handle. A window handle is a unique identifier that Selenium assigns to each window when it is opened.

When a new window is opened from a parent window, it becomes a child window, and the parent window remains as it is. To interact with parent and child windows, we must identify them using their respective handles.

To get the window handle of the current window, we can use the driver.getWindowHandle() method. This method returns the window handle of the current window.

To get the window handles of all the currently open windows, we can use the driver.getWindowHandles() method. This method returns a set of all the window handles.

Once we have the window handles, we can switch between windows using the driver.switchTo().window() method. We need to pass the parent window handle to this method to switch to the parent window. We need to pass the child window handle to this method to switch to a child window.

Here's an example of how to switch between windows in Selenium:

Why do We Need to Handle Multiple Windows in Selenium?

Handling multiple windows is a crucial aspect of web application testing using Selenium. Here are some reasons why we need to handle multiple windows:

  1. Pop-up Windows: Many web applications use pop-up windows to display important information or accept user inputs. In such cases, switching to the pop-up window becomes necessary, performing the required operations and then switching back to the parent window.
  2. Multiple Browser Tabs: Some web applications may open multiple tabs based on user actions or system events. Testers must switch between the tabs and perform various operations in such cases.
  3. Parallel Test Execution: Selenium supports parallel test execution, meaning testers can execute multiple tests simultaneously. Handling multiple windows becomes necessary when tests require interaction between different browser windows.
  4. Cross-window verifications: Some web applications may display critical information or alerts in a pop-up window or a new tab. Switching to that window to verify the information becomes essential, and then switching back to the original window to continue with the test.
  5. Multi-window operations: Some web applications require multiple windows to be opened for different tasks, such as a file upload operation that opens a separate window. In such cases, handling multiple windows becomes necessary to complete the task successfully.

What is a Window Handle in Selenium?

In Selenium, a window handle is a unique identifier assigned by the WebDriver to each window or tab opened by a web application. When a new window or tab is opened, the WebDriver assigns a unique alphanumeric string value, which can be used to switch between windows or perform actions on a specific window.

Window handles are important in Selenium because web applications often have multiple windows or tabs for different tasks or functionalities. For example, a login window may open when the user clicks on a "Login" button, or a popup window may appear when the user clicks on an advertisement. To interact with these windows, we must identify them using their window handle and switch to them before performing any actions.

Window handles can be obtained using the getWindowHandle() method provided by the WebDriver interface. This method returns a string value representing the current window handle. Once we have the window handle, we can use the switchTo().window() method to switch to the desired window or tab.

In Selenium, a window handle is a unique identifier that is assigned by the browser to each open window or tab. A window handle is an alphanumeric string that allows Selenium to differentiate between multiple browser windows or tabs and interact with them individually. Here are some important points to keep in mind about window handles in Selenium:

  • Unique identifier: Every window or tab opened in a browser is assigned a unique window handle by the browser. The window handle is a string of alphanumeric characters that Selenium uses to identify and interact with the window.
  • Multiple window handles: A single browser session can have multiple windows or tabs open at once. Each window or tab will have its unique window handle.
  • Switching between windows: To interact with a specific window or tab, Selenium needs to switch to that window's handle. This can be done using the switchTo().window() method, which takes the desired window handle as an argument.
  • Handling pop-up windows: When a pop-up window or dialog is opened by a web page, Selenium can use the getWindowHandles() method to get a list of all open window handles. By iterating through this list and comparing the handles to the current handle, Selenium can identify and switch to the pop-up window.
  • Managing multiple windows: When dealing with multiple windows or tabs, it's important to keep track of each window's handle to interact with them correctly. Selenium provides methods for opening, closing, maximizing, and minimizing browser windows, as well as navigating between pages within each window.
  • Limitations: It's worth noting that not all browsers handle window handles in the same way. Some browsers may generate different handles for the same window in different sessions, or may not provide a reliable way to switch between windows. In addition, some web pages may use techniques to prevent pop-up windows from being identified by Selenium.

What are the Different Methods Used for Window Handling in Selenium?

There are several methods used for window handling in Selenium. Some of the most commonly used methods are:

getWindowHandle( )

This method returns a unique identifier for the current window. It returns a string value representing the window handle of the current browser window. You can use this method to keep track of the current window when multiple windows are open.

getWindowHandles( )

This method returns a set of unique identifiers for all the open windows. It returns a collection of string values representing the window handles of all the browser windows currently open. You can use this method to get all the window handles and then switch between them.

switchto()

This method allows you to switch between different browser windows or frames. There are several methods available under switchTo(), which include:

  • window(): This method allows you to switch to a specific browser window using the window handle. You can use the getWindowHandle() and getWindowHandles() methods to get the window handle and then use it to switch to that window.
  • frame(): This method allows you to switch to a specific frame within a web page. You can use the frame index or name to switch to that particular frame.
  • alert(): This method allows you to switch to an alert box and perform actions on it, such as accepting or dismissing the alert.

action

The Actions class in Selenium provides a way to simulate complex user interactions such as mouse movements, key presses, and mouse clicks. It has several methods that allow you to perform various actions on the web page, such as:

  • moveToElement(): This method moves the mouse pointer to the center of the specified web element.
  • click(): This method simulates a left mouse click on the current element.
  • keyDown(): This method simulates a key press and hold on the keyboard.
  • keyUp(): This method simulates a key release on the keyboard.
  • dragAndDrop(): This method simulates a drag-and-drop action between two web elements.

How to Handle Child Windows in Selenium?

Handling child windows in Selenium involves a few steps:

  1. Switch to the parent window using driver.switchTo().window(parentWindowHandle), where parentWindowHandle is the handle of the parent window, which can be obtained using driver.getWindowHandle().
  2. Click on the element that opens the child window.
  3. Once the child window opens, obtain the handles of all open windows using driver.getWindowHandles(). This returns a set of window handles.
  4. Iterate over the set of window handles, and switch to the window handle that is not equal to the parent window handle.
  5. Perform actions on the child window.
  6. Switch back to the parent window using a driver.switchTo().window(parentWindowHandle).

Here's some sample code to handle child windows in Selenium:

How to Handle Multiple Windows in Selenium?

Handling multiple windows is an important part of web automation testing using Selenium. Here are the steps to handle multiple windows in Selenium:

  1. Get the parent window handle using driver.getWindowHandle(). This method returns the window handle of the current window.
  2. Click on the link/button that opens a new window.
  3. Get all the window handles using driver.getWindowHandles(). This method returns a set of window handles, which includes both the parent and child window handles.
  4. Switch to the child window using driver.switchTo().window(childWindowHandle). Here, childWindowHandle is the window handle of the child window.
  5. Perform the necessary actions on the child window.
  6. Switch back to the parent window using driver.switchTo().window(parentWindowHandle). Here, parentWindowHandle is the window handle of the parent window.
  7. Perform the necessary actions on the parent window.

Here's an example code snippet that demonstrates the above steps:

How to Switch Back to the Parent Window from the Child Windows in Selenium?

The steps to switch back to the parent window from the child windows in Selenium:

  1. Get the window handle of the parent window using the getWindowHandle() method and save it in a string variable.
  2. Get all the window handles of the open windows using the getWindowHandles() method and store them in a set.
  3. Loop through the set of window handles and switch to each window using the switchTo().window() method.
  4. In each loop iteration, check if the current window handle is not equal to the parent window handle. If it is not, then it means the current window is a child window. Perform the required operations in the child window.
  5. When you finish the child window, switch back to the parent window using the switchTo().window() method and pass the parent window handle string variable obtained in step 1.

Here is a sample Java code snippet to demonstrate the above steps:

How to Close All Windows in Selenium?

To close all windows in Selenium, you can follow these steps:

  1. Get all window handles using the getWindowHandles() method.
  2. Iterate through the window handles and switch to each window using the switchTo().window() method.
  3. Close each window using the close() method.
  4. Switch back to the main window using the switchTo().window() method.

Here's the sample code in Java:

Note that mainWindowHandle is the main window handle, which should be stored before opening any child windows.

Conclusion

  • A window in Selenium refers to a web page or pop-up window opened within the web browser.
  • There are two types of windows in Selenium - parent windows and child windows.
  • A parent window is the primary window used to perform actions, while a child window is opened by clicking a link or button on the parent window.
  • Window handles are unique identifiers that Selenium assigns to each window when it is opened.
  • We can use the driver.switchTo() to switch between the windows.window() method and pass the window handle of the desired window as an argument.
  • We can get the current window handle using the driver.getWindowHandle() method and get the window handles of all open windows using the driver.getWindowHandles() method.
  • Handling multiple windows is necessary for testing pop-up windows, multiple browser tabs, and parallel test execution.
  • Other methods used for window handling in Selenium include driver.close(), driver.quit(), and the Actions class for performing mouse and keyboard actions.