Set up .NET, Selenium and NUnit
On this page
This chapter creates the test project, adds Selenium, and runs a first test against a real browser. It takes about ten minutes, most of it waiting for downloads.
Install the .NET SDK#
Check what you have:
dotnet --list-sdks
If the list is empty or the command is missing, install the SDK from dot.net, or with a package manager:
# Windows
winget install Microsoft.DotNet.SDK.10
# macOS
brew install --cask dotnet-sdk
On Linux, Microsoft's install page lists the package source for each distribution.
The examples were run with SDK 10.0.404. Any 10.0 SDK works; so does a .NET 8 SDK if you change net10.0 to net8.0 in the project file below. You do not need Visual Studio. Every command here runs from a terminal, and VS Code, Rider and Visual Studio all open the same project.
Create an NUnit project#
The SDK ships a project template for NUnit:
dotnet new nunit -n SeleniumCourse
cd SeleniumCourse
dotnet add package Selenium.WebDriver
dotnet add package Selenium.Support
Selenium.WebDriver is the WebDriver client, including WebDriverWait. Selenium.Support adds helpers such as SelectElement for dropdowns. Do not add Selenium.WebDriver.ChromeDriver or similar driver packages: they pin a driver binary that falls out of date the next time Chrome updates itself.
The template's package versions lag behind. On SDK 10.0.404 it generated NUnit 4.3.2 and NUnit3TestAdapter 5.0.0, while NUnit 5.0.0 and adapter 6.3.1 were current. dotnet add package without a version installs the latest, so run it again for the test packages:
dotnet add package NUnit
dotnet add package NUnit3TestAdapter
dotnet add package Microsoft.NET.Test.Sdk
dotnet add package NUnit.Analyzers
Delete the generated UnitTest1.cs. The project file now looks like this (we also removed coverlet.collector, which measures code coverage and is no use for browser tests):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="NUnit" Version="5.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.15.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Selenium.Support" Version="4.52.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.52.0" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>
Three packages make dotnet test work, and leaving one out is the most common reason for "No test is available":
| Package | Job |
|---|---|
NUnit |
the attributes and Assert you write tests with |
NUnit3TestAdapter |
lets dotnet test and IDEs discover and run NUnit tests (the name is historical; it runs NUnit 3, 4 and 5) |
Microsoft.NET.Test.Sdk |
the test host that dotnet test starts |
NUnit.Analyzers is optional but worth keeping: it turns common NUnit mistakes into compiler warnings and errors, as you are about to see. The <Using Include="NUnit.Framework" /> line is a global using, so test files do not need their own using NUnit.Framework;.
A colleague's project has NUnit and Selenium.WebDriver, builds fine, but dotnet test runs nothing. What is the likely cause?
The NUnit package only provides the attributes. Discovery needs the adapter and the test SDK. A missing driver would make tests fail, not disappear, and NUnit test methods are instance methods.
The first test#
Create FirstTest.cs:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumCourse;
public class FirstTest
{
private IWebDriver driver = null!;
[SetUp]
public void StartBrowser()
{
driver = new ChromeDriver();
}
[Test]
public void WebFormHasTheRightTitle()
{
driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html");
Assert.That(driver.Title, Is.EqualTo("Web form"));
}
[TearDown]
public void StopBrowser()
{
driver.Quit();
}
}
NUnit creates the class, calls the [SetUp] method, then the [Test] method, then the [TearDown] method, which runs whether the test passed or failed. = null! tells the compiler's nullable checks that the field is set before use (in SetUp), which it cannot see by itself.
The analyzer's first complaint#
Run dotnet test and the build fails before any browser starts:
FirstTest.cs(8,24): error NUnit1032: The field driver should be Disposed in a method annotated with [TearDownAttribute]
IWebDriver is IDisposable, and the analyzer wants every disposable field disposed in a teardown. It does not know that Selenium's Quit() does exactly that: the API documentation describes it as "Close the Browser and Dispose of WebDriver". Tell the analyzer once, in an .editorconfig file next to the project file:
root = true
[*.cs]
# Selenium's Quit() closes the browser and disposes the driver
dotnet_diagnostic.NUnit1032.additional_dispose_methods = Quit
The alternative, calling driver.Dispose() instead of Quit(), also satisfies it, but Quit() is what every Selenium example uses and what readers of your code will look for.
Run it#
dotnet test
Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: 1 s - SeleniumCourse.dll (net10.0)
A Chrome window opens, loads the form and closes again. The first run takes longer because Selenium Manager looks up your Chrome version and, if needed, downloads a matching ChromeDriver into a cache folder (~/.cache/selenium on macOS and Linux, %USERPROFILE%\.cache\selenium on Windows). Later runs reuse it. If Chrome is not installed at all, Selenium Manager can download a Chrome for Testing build as well.
To see each test's name and outcome rather than just the summary, raise the console logger's verbosity:
dotnet test --logger "console;verbosity=normal"
Passed WebFormHasTheRightTitle [1 s]
Chrome updated overnight and a project that references the Selenium.WebDriver.ChromeDriver package now fails with "This version of ChromeDriver only supports Chrome version ...". What is the lasting fix?
Selenium Manager matches the driver to the installed browser at run time, so browser updates stop breaking the suite. Bumping the package works until the next Chrome release, and pinning the browser means testing against a version your users no longer run.
Running a subset#
As the suite grows you will want to run part of it. dotnet test --filter selects by name, class or category:
dotnet test --filter "Name=WebFormHasTheRightTitle"
dotnet test --filter "FullyQualifiedName~FirstTest" # ~ means "contains"
dotnet test --list-tests # show what would run
Chapter 3 adds categories, which are the better long-term way to group tests. The next chapter is about the part of Selenium where most of the real work happens: finding elements and waiting for them.