Skip to main content

_csharp

TL;DR - Show Me The Code

4-work-with-multiple-windows/code/csharp/MultipleWindowsTest.cs
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace csharp
{
[TestClass]
public class MultipleWindowsTest
{
IWebDriver _driver;
WebDriverWait _wait;

[TestInitialize]
public void SetUp()
{
_driver = new FirefoxDriver();
_wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(2));
}

[TestCleanup]
public void TearDown()
{
_driver.Quit();
}

[TestMethod]
public void MultipleWindowsExample1()
{
_driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/windows");
_driver.FindElement(By.CssSelector(".example a")).Click();

var Windows = _driver.WindowHandles;

_driver.SwitchTo().Window(Windows[0]);
Assert.AreEqual("The Internet", _driver.Title);

_driver.SwitchTo().Window(Windows[1]);
// Waiting for the new window to have a title, then we consider it is loaded
_wait.Until(driver => !string.IsNullOrEmpty(driver.Title));
Assert.AreEqual("New Window", _driver.Title);
}

[TestMethod]
public void MultipleWindowsExample2()
{
_driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/windows");
string FirstWindow = _driver.CurrentWindowHandle;
string SecondWindow = "";

_driver.FindElement(By.CssSelector(".example a")).Click();

var Windows = _driver.WindowHandles;
foreach(var Window in Windows)
{
if (Window != FirstWindow)
SecondWindow = Window;
}

_driver.SwitchTo().Window(FirstWindow);
Assert.AreEqual("The Internet", _driver.Title);

_driver.SwitchTo().Window(SecondWindow);
// Waiting for the new window to have a title, then we consider it is loaded
_wait.Until(driver => !string.IsNullOrEmpty(driver.Title));
Assert.AreEqual("New Window", _driver.Title);
}
}
}

Code Walkthrough

Importing Libraries, Setup and Teardown

Lines 1 to 5 are pulling in our requisite classes for our testing framework (e.g., using Microsoft.VisualStudio.TestTools.UnitTesting;), driving the browser with Selenium (e.g., using OpenQA.Selenium;), launching an instance of Firefox (e.g., using OpenQA.Selenium.Firefox;), and Selenium's wait functions (e.g., using OpenQA.Selenium.Support.UI;).

Next, we'll start our class off with some setup and teardown methods, covered in lines 12 to 26.

Example 1

Line 29 shows a test that exercises new window functionality from an application. In this case, we'll be using the new window example from the-internet.

After loading the page we click the link which spawns a new window. We then grab the window handles (a.k.a. unique identifier strings which represent each open browser window) and switch between them based on their order (assuming that the first one is the originating window, and that the second one is the new window). We round this test out by performing a simple check against the title of the page to make sure Selenium is focused on the correct window.

While this may seem like a good approach it can present problems later because the order of the window handles is not consistent across all browsers. Some return it in the order opened, others alphabetically.

Here's a more resilient approach. One that will work across all browsers.

Example 2

Line 46 shows that after loading the page we store the window handle in a variable (e.g., FirstWindow) and then proceed with clicking the new window link.

Now that we have two windows open we grab all the window handles and search through them to find the new window handle (e.g., the handle that doesn't match the one we've already stored). We store the new window result in another variable (e.g., SecondWindow) and then switch between the windows, checking the page title each time to make sure the correct window is in focus.

For more information about switching windows (and tabs) visit the official Selenium documentation page.

Executing the Test

Before executing the test, we need to make sure the required dependencies are declared on the project file (e.g., csharp.csproj in this case).

Toggle to see the csharp.csproj file.
4-work-with-multiple-windows/code/csharp/csharp.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
<PackageReference Include="Selenium.Support" Version="4.24.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.24.0" />
</ItemGroup>

</Project>

Finally, we can run the test by executing dotnet test from the command-line.