When automating a website or online application, we have probably seen instances where, upon clicking a button, several windows within the program appear and the user is required to take action on each of the opened windows. Now, a user might not be able to operate on all windows at once, thus he or she needs a method by which he or she can exert control over parent and child windows. Or, to speak from the standpoint of quality assurance, how to handle multiple windows in Selenium? Check out the automation testing certification courses to learn more about Windows in Selenium.
What is a window in Selenium?
Any browser’s home page that the user lands on after clicking a link or URL is called a window. The parent window is the name given to this type of window in Selenium. It opens when the Selenium WebDriver session is created and has all the focus of the WebDriver.
The context of the main window will always be present while navigating a URL with Selenium WebDriver. The “New Window” and “New Window Message” buttons, however, open new windows inside the parent window. They are called Child Windows. Let’s see where the parent and kid windows are located.
How do we distinguish between parent and child windows?
A web page opens when a user clicks on a URL. The user has currently landed on this main page and will execute all operations on it, making it the parent window. When our Selenium automation script runs, here is the webpage that will open. Your primary window’s openings will be referred to as “child windows” for all of them.
Note: Your parent window may have a single child window or several child windows.
There may or may not be a URL in a child window. As was already said, Child Window 1 lacks an explicit URL, but Child Window 2 has one.
As a result, it is very simple to evaluate the behaviour of child windows when manually testing a web application because they are readily visible in the context of the main window. However, when using Selenium to automate, the situation is different. Let’s examine why handling the various window kinds is necessary when automating an application with Selenium WebDriver.
Why do we need to handle multiple windows in Selenium?
It may happen that a new window will open inside your main window when you are using a web application. Imagine an online store offering clothing with a size chart attached to each item, which, when clicked, displays a child window. Now that we know Selenium only functions in a specific context, we can see that each of the child windows will have its context. Therefore, we must guide the WebDriver appropriately in order for Selenium to obtain the context of a particular window and carry out the necessary operations in that window when automating such scenarios using Selenium WebDriver.
Selenium WebDriver uses the window’s unique ID, or the “context” ID, to change the context handle of the window. Let’s understand what exactly is a window handle in the Selenium context.
What is a window handle in Selenium?
The distinct address of each browser window is kept in a window handle. It is merely a reference to a window with an alphanumeric return type. Selenium’s window handle makes managing numerous windows and child windows easier. Each browser will have a distinct window handle value that will allow for easy identification.
Each window, in this case, will have a distinct ID, which we can obtain using the Selenium Webdriver APIs, and then use the same to move the context to that particular window. Let’s first clarify the various window-handling techniques that Selenium WebDriver offers.
What are the different methods used for window handling in Selenium?
For handling Windows, Selenium WebDriver offers a number of different approaches. Most of them are not:
- getWindowHandle(): When a website loads, we must use the driver to manage the parent window, which is the primary window.the function getWindowHandle(). By using this function, we may obtain the current window’s special ID, which will serve as its identification within this driver instance. The value of the String type will be returned by this method.
- getWindowHandles(): We utilise the driver to manage all opened windows that are the web driver’s child windows.the method getWindowHandles(). Here, we can see how a web application switches from one window to another. Windows saves its data in a Set of String type. Set String> serves as the return type.
- switchto(): This technique allows us to switch between windows.
- action: This technique aids in carrying out specific window tasks.
How does Selenium manage child windows?
If a web application has child windows, like in the example above, interacting with them without correct window handling will result in an error.
Here, along with the switchto() method, we’ll be using the getWindowHandle() and getWindowHandles() methods.
How to handle multiple windows in Selenium?
When a web application has numerous windows, the Selenium technique may need to transfer control between several windows, i.e. from one window to another, to carry out any operation. To do this, we can use the switchto(); method. Additionally, a window handle will be used in this instance to save the windows’ unique values and carry out window handling using Selenium.
Recall that you must use the same procedure if you must switch between tabs.
How to close all windows in Selenium?
It is crucial to simultaneously close all open windows when we are working on several ones. We have a driver that allows us to close the window that WebDriver is now focused on.the method close(). This technique is primarily used when there are numerous windows open and we want to close just one of them.
Driver.quit() is an additional window-closing method that will close every window open during a specific session. In essence, it terminates the driver instance, and any additional interactions with WebDriver may raise an exception. Usually, the last statement of any code is this one.
In conclusion, a web page that loads when a user enters a URL is known as a window. The parent window and its child windows are the two different categories of windows in Selenium. The window handle is a distinctive identifier that aids in window management in Selenium by storing the values of windows launched on a webpage.
Windows are handled by Selenium’s getWindowHandles() and getWindowHandles() functions. To work on them, the user must utilise the switchTo() method to go from the parent window to the child window. Finally, there are two ways to shut down windows: driver.quit() and driver.close(). If you are an amateur Selenium tester, you can check out the Selenium tester foundation certification course to learn more about Windows in Selenium.