In this article, we will talk about how to maximize the browser window with Selenium in C#. We will use an xUnit test application with Selenium to show the maximized browser window.
Let’s start.
What Is Testing With Selenium
Selenium is a popular open-source library for UI testing of web applications. It enables developers to write automated UI tests, test the code in native browsers, and observe behavior the same way the users would use the application. Selenium also supports all modern browsers and can manipulate a wide range of HTML elements.
In our other article, we can acquaint ourselves with Automated UI Tests with Selenium and ASP.NET Core, while in this article we will cover a specific scenario related to maximizing the browser window.
Setting up the xUnit Test Project for Selenium
We will create the xUnit test project in our solution. Let’s start by installing Selenium.WebDriver
, Selenium.WebDriver.ChromeDriver
libraries.
Next, we can utilize .NET CLI to install libraries:
dotnet add package Selenium.WebDriver dotnet add package Selenium.WebDriver.ChromeDriver
Then, we implement the IDisposable
interface to our test class:
public class MaximizeBrowserWindowLiveTest : IDisposable { private IWebDriver ?_driver; public void Dispose() { _driver?.Quit(); _driver?.Dispose(); } }
Here, we declare _driver
as a private IWebDriver
interface and implement the Dispose()
method which calls Quit()
and Dispose()
methods on our driver.
Maximize Browser Window in Chrome
We can proceed to write our first test in our MaximizeBrowserWindowLiveTest
class:
public class MaximizeBrowserWindowLiveTest : IDisposable { private IWebDriver ?_driver; [Fact] public void GivenTestInChrome_WhenBrowserNavigatesToURL_ThenBrowserWindowMaximizes() { _driver = new ChromeDriver(); _driver.Navigate().GoToUrl("https://code-maze.com/"); _driver.Manage().Window.Maximize(); } public void Dispose() { _driver?.Quit(); _driver?.Dispose(); } }
In the test with the Chrome web browser, we use the _driver.Manage().Window.Maximize()
method to maximize the browser window. As an argument of our GoToUrl()
method we pass https://code-maze.com/
url address.
We run the tests by typing in the terminal:
dotnet test
We see the browser window maximized after navigating to the specified URL, which means our test passes.
Start Chrome With Options
Also, we can add arguments to options while instantiating Chrome driver which will open the web browser in maximized mode.
Let’s see how we can apply this behavior with the test:
[Fact] public void GivenTestInChromeWOptions_WhenBrowserOpens_ThenBrowserWindowIsMaximized() { var options = new ChromeOptions(); options.AddArguments("--start-maximized"); _driver = new ChromeDriver(options); _driver.Navigate().GoToUrl("https://code-maze.com/"); }
Before we initialize a new ChromeDriver
, we initialize a new instance of ChromeOptions
. Then, we use the AddArguments()
method to append options to the browser’s executable command line. Next, we add the --start-maximized
option and pass it when we initialize the new ChromeDriver
.
Finally, we run the tests using the dotnet test
command in the terminal and see that the test passed.
Maximize Browser Window in Firefox
Let’s try using GeckoDriver to maximize the Firefox browser window. First, we add Selenium.WebDriver.GeckoDriver
via NuGet Package Manager or by using .NET CLI:
dotnet add package Selenium.WebDriver.GeckoDriver
Then, we can implement our test:
[Fact] public void GivenTestInFirefox_WhenBrowserNavigatesToURL_ThenBrowserWindowMaximizes() { var service = FirefoxDriverService.CreateDefaultService(); _driver = new FirefoxDriver(service); _driver.Navigate().GoToUrl("https://code-maze.com/"); _driver.Manage().Window.Maximize(); }
Here, we use the identical code to maximize the web browser as in the previous test with ChromeDriver
. After we run the dotnet test
command, we get the browser window to maximize, and our test passes.
Maximize Browser Window in MS Edge
We want to cover all major web browsers so we will continue with MS Edge. As with the Firefox, we first need to add Selenium.WebDriver.MSEdgeDriver
via NuGet Package Manager or with .NET CLI:
dotnet add package Selenium.WebDriver.MSEdgeDriver
Then we add a test to our test class:
[Fact] public void GivenTestInMSEdge_WhenBrowserNavigatesToURL_ThenBrowserWindowMaximizes() { var service = EdgeDriverService.CreateDefaultService(); _driver = new EdgeDriver(service); _driver.Navigate().GoToUrl("https://code-maze.com/"); _driver.Manage().Window.Maximize(); }
For the test with MS Edge, we again use the same line of code to maximize a browser window. After we run the test, the MS Edge browser window will maximize, and our test passes.
Maximize Browser Window in Safari
The last web browser that we will cover is Safari. As with previous examples, we first need to add Selenium.WebDriver.SafariDriver
using NuGet Package Manager or .NET CLI:
dotnet add package Selenium.WebDriver.SafariDriver
If we run this test on MacOS, enable the Allow remote automation
flag within Developer settings. Let’s continue with writing our test for the Safari web browser:
[Fact] public void GivenTestInSafari_WhenBrowserNavigatesToURL_ThenBrowserWindowMaximizes() { var service = SafariDriverService.CreateDefaultService(); _driver = new SafariDriver(service); _driver.Navigate().GoToUrl("https://code-maze.com/"); _driver.Manage().Window.Maximize(); }
We use the same _driver.Manage().Window.Maximize()
method for our test in the Safari browser. When we run the test, the Safari browser window maximizes, and the tests are successful.
Conclusion
Selenium, as a library for automation of UI testing, shines when you need to test real-world scenarios within web browsers. We saw how we can maximize the browser window when testing with Selenium with one line of code. Additionally, this method also works with all major web browsers so we can be certain that we will cover almost all users when we test our application with Selenium.