Selenium C# Tutorial: Handling Multiple Browser Windows
Himanshu Sheth
Posted On: April 10, 2020
106506 Views
9 Min Read
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium C# Tutorial.
A common scenario in a website (or web application) is opening up a new browser (or tab) on the click of a button. With this multiple browser windows can be automated using Selenium.
Once the Selenium WebDriver instance is instantiated, a unique alphanumeric id is assigned to the window. This is called window handle and is used to identify browser windows. Since the id is unique, it is used by the Selenium WebDriver to switch between different windows (or tabs).
The id is retained till the closure of the Selenium WebDriver session via WebDriver.Quit or manual killing of the process. The SwitchTo() command is used to do a context switch to the intended browser window.
In my previous Selenium C# tutorial, I’ve covered how to Here in this part of Selenium C# tutorial, I’ll guide you on window handling in Selenium C#.
TABLE OF CONTENT
Commands Used For Window Handling In Selenium C#
These commands are vital for performing Selenium Test Automation as they allow help in switching to a new window or a tab, knowing the info about current window and of all the windows open. Here are some of the widely used commands that are used for window handling in Selenium C#.
SwitchTo Window
This command is used to switch the focus to a new browser window (or tab) by supplying the Window Name or Window Handle as an argument to the command.
1 2 |
driver.SwitchTo().Window(WindowHandle); driver.SwitchTo().Window(WindowName); |
CurrentWindowHandle
This command returns the window handle (as a String) of the current window.
1 |
String currentWindowHandle = driver.CurrentWindowHandle; |
WindowHandles
The WindowHandles command gets the handles of all the open instances of the browser under test. The return type is a String ReadOnlyCollection.
The syntax of WindowHandles
is:
1 |
ReadIOnlyCollection<string> IWebDriver.WindowHandles { get;}; |
Example usage of WindowHandles:
1 |
IList<string> totWindowHandles = new List<string> (WebDriver.WindowHandles); |
Now in the next section this Selenium C# tutorial, we answer, how to handle multiple browser windows in Selenium C#.
Handling Multiple Browser Windows In Selenium C#
The fundamental principles of Window Handles are used for window handling in Selenium C#. By default, the Parent Window is the one that has the focus. To switch the context from the Parent Window to a Child Window, the command WebDriver.SwitchTo(WindowHandle)
is used. Instead of Window Handle, Window Id can also be used as both these identifiers are unique to every browser window (or tab).
To demonstrate window handling of multiple browser in Selenium C#, a Chrome WebDriver instance is initiated with the URL under test as https://www.lambdatest.com. A unique Window handle of the type string is used to identify the window.
The window.open() method with relevant parameters such as URL, _blank, window size, etc. is used to open the URL https://www.lambdatest.com/blog/. This would be a child window to the parent window which is already open
The WindowHandle count now becomes two (0 and 1). The SwitchTo().Window()
command with Window Handle of the second window (i.e. child window) is used to switch to that browser window.
1 2 3 |
/*WindowHandle count and SwitchTo().Window() for window handling in Selenium C# window handling in Selenium C# */ var newWindowHandle = driver.WindowHandles[1]; Assert.IsTrue(!string.IsNullOrEmpty(newWindowHandle)); |
1 2 3 |
/* Assert.AreEqual(driver.SwitchTo().Window(newWindowHandle).Url, "https://www.lambdatest.com/blog/"); */ string expectedNewWindowTitle = test_url_2_title; Assert.AreEqual(driver.SwitchTo().Window(newWindowHandle).Title, expectedNewWindowTitle); |
The Child Window is closed and the window count becomes one. The context is switched to the parent window using the command driver.SwitchTo().Window(driver.WindowHandles[0])
As seen in the output snapshot, the two browser windows are open with URL 1 – https://www.lambdatest.com and URL 2 – https://www.lambdatest.com/blog/. The WindowHandle of the second browser window is used with the SwitchTo()
command to switch the context to that window. Once that child window is closed, the same approach is used to switch to the parent window (driver.WindowHandles[0]).
Now that you know how to open multiple windows in Selenium C#, let’s move on to learn how to handle multiple browser tabs for windows handling in this Selenium C# tutorial.
Take this certification to master the fundamentals of Selenium automation testing with C# and prove your credibility as a tester.
Here’s a short glimpse of the Selenium C# 101 certification from LambdaTest:
Handling Multiple Browser Tabs In Selenium C#
The concept of Window Handle remains the same whether a URL is opened in a new window or a new tab. For demonstrating how to handle multiple browser tabs in Selenium C#, we use the test URL as http://the-internet.herokuapp.com/windows The Click Here link on the test page is located using the XPath property. In our earlier articles, we covered XPath in Selenium in greater detail.
Once the button is clicked, the URL http://the-internet.herokuapp.com/windows/new opens up in a new browser tab. The window handle count becomes two with driver.WindowHandles[0] representing window handle of ‘parent window’ and driver.WindowHandles[1] representing window handle of ‘child window’.
The SwitchTo().Window(driver.WindowHandles[0]) is used to switch back to the parent window after closing the child window.
Here are the Window Handles of the two browser windows that were instantiated during automated browser testing:
The output snapshot from Visual Studio is below:
Now, let’s move on to handling browser pop-up windows in this selenium C# tutorial.
You can also refer to the below video tutorial on how to handle Windows and Frames in selenium.
Handling Browser Pop-up Windows In Selenium C#
There are many types of websites (e.g. job sites) that throw a couple of pop-up windows. Selenium test automation can be used to automate handling of these pop-ups. It does not depend on the number of pop-ups being shown as each new window will be assigned a unique window handle. The window handle can be used with the SwitchTo() command to switch between different windows.
To demonstrate handling of browser pop-ups, we use the test URL http://www.popuptest.com/popuptest1.html. Pop-ups for Chrome can be enabled by going to chrome://settings/ ? Privacy and security ? Site Settings ? Pop-ups and redirects and disabling Block option for http://www.popuptest.com:80
Alternately, you can open the test URL in the Chrome browser and enable pop-ups for that particular site.
The site opens up 6 pop-up windows. In this Selenium test Automation scenario, we close pop-up windows, one at a time in a reverse chronological order i.e. http://www.popuptest.com/popup6.html will be closed first, http://www.popuptest.com/popup5.html will be closed next and so on.
Window Handles of these pop-ups are used to perform the following operations:
Switch to a window (or pop-up)
1 |
driver.SwitchTo().Window(WindowHandle) |
Close the browser window
1 2 |
driver.SwitchTo().Window(driver.WindowHandles[window-number]).Close() or driver.Close() (if the window to be closed is in focus) |
Once all the pop-windows are closed, the Window Count becomes 1. The Window Title is then matched with the expected title to make sure that the parent window is in focus.
On confirmation, the parent window is also closed and the resources used by Chrome WebDriver instance are freed using driver.Quit().
For modularity, we have created few Helper Functions:
getCurrWindowTitle()
Get the Window title of the currently focused window.
getMainWinHandle(IWebDriver driver)
It returns the handle of the window under focus. This API is called after switching to the intended window hence, driver.CurrentWindowHandle is sufficient to return the window handle.
closeAllWindows(String currWindowHandle)
This API closes all the pop-up windows one by one till the Window Handle count becomes 1 (driver.WindowHandles.Count == 1) i.e. only the parent window is open.
The Window handles are stored in a Collection of type String.
1 |
IList<string> totWindowHandles = new List<string>(driver.WindowHandles); |
The currently focused pop-window is closed and a SwitchTo() command is used to switch to the other pop-up window shown on the screen. The API will close pop-up windows in the order popup6.html ? popup5.html ? popup4.html ? popup3.html ? popup2.html ? popup1.html. The API returns true when all pop-ups are closed and Parent window is the currently focused window.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
foreach (String WindowHandle in totWindowHandles) { if (!WindowHandle.Equals(currWindowHandle)) { driver.SwitchTo().Window(WindowHandle); driver.Close(); } } if (driver.WindowHandles.Count == 1) return true; .................................... .................................... |
The implementation under [Test] annotation uses these helper functions to perform Selenium test automation. The Window Title of the parent window is compared to verify the closure of all the pop-up windows.
The Window handles of the pop-up windows and parent window is below:
Below is the execution snapshot from Visual Studio which indicates that our Selenium Automation tests have passed
As the implementation is written in a generic manner, the same implementation can be used for verifying pop-up window handling with any other website.
This article was a part Selenium C# tutorial series, where I’ve explained how to set up visual studio, handle implicit wait, handle explicit and fluent waits and handling alert windows. If you haven’t gone through them, I’d encourage you to go through them!
Handling Alert Windows in Selenium C#
All in All
Browser windows, including tabs are identified using Window Handles. These handles are used in conjunction with SwitchTo.Window() Selenium API to handle browser windows, tabs, and pop-up windows. In this Selenium C# tutorial, we had a deep-dive look into all the available scenarios with web browsers.
Keen to learn more? Don’t worry we’ve got you covered! In the next tutorial for this Selenium C# tutorial series, where I’ll show you how to handle frames and iframes in Selenium C#.Let’s Automate!
Update: We’ve now completed the Selenium C# tutorial series, so in order to help you easily navigate through the tutorials, we’ve compiled the complete list of tutorials, which you can find in the section below.
Selenium C# Tutorials With Examples
- Selenium C# Tutorial: Setting Up Selenium In Visual Studio
- Running First Selenium C# Script With NUnit
- Selenium C# Tutorial: Using Implicit Wait in Selenium
- Selenium C# Tutorial: Using Explicit and Fluent Wait in Selenium
- Selenium C# Tutorial: Handling Alert Windows
- Selenium C# Tutorial: Handling Multiple Browser Windows
- Selenium C# Tutorial: Handling Frames & iFrames With Examples
- Selenium C#: Page Object Model Tutorial With Examples
It’s a wrap! This was all I had to share in this Selenium C# tutorial. If you liked this article feel free to share it with your friends, you can retweet it on twitter or your favourite social media account to help us reach out to more people.
Happy Testing ☺
Got Questions? Drop them on LambdaTest Community. Visit now