Exit course
Chapter 1 of 10 Intermediate 5 min read Updated

Selenium C# Tutorial with NUnit

A free Selenium C# tutorial on .NET 10 and NUnit: project setup with Selenium Manager, locators, WebDriverWait, [TestCase] data, constraint assertions, page objects, parallel runs, a cloud grid and CI.

What you will learn

  1. Selenium C# Tutorial with NUnit
  2. Set up .NET, Selenium and NUnit
  3. Selenium C# locators and WebDriverWait
  4. NUnit fixtures, attributes and [TestCase]
  5. NUnit assertions and screenshots on failure
  6. Page objects in C#
  7. Run Selenium C# tests on the TestingBot grid
  8. Parallel Selenium tests with NUnit
  9. Selenium C# tests in CI
  10. Selenium C# Quiz
On this page
  1. Selenium drives the browser, NUnit runs the tests
  2. Why NUnit?
  3. What you'll build
  4. Prerequisites

Last reviewed November 2026 against Selenium.WebDriver 4.52.0, NUnit 5.0.0, NUnit3TestAdapter 6.3.1, Microsoft.NET.Test.Sdk 18.12.0 and the .NET 10 SDK (10.0.404).

This course builds a small but complete browser test suite in C#. Every example in it was compiled and run, first against local Chrome and Firefox and then, in chapter 6, on the TestingBot grid. By the end you have one project that runs on your laptop, in CI and on a cloud grid without changing a single test.

Selenium drives the browser, NUnit runs the tests#

The suite is built from two libraries that do very different jobs. Most early confusion comes from mixing them up.

Selenium WebDriver is a browser automation library. It starts a browser, navigates, finds elements, clicks, types and reads text back. It has no idea what a test is: it does not know what "passed" means, and it leaves a browser running forever if nobody calls Quit().

NUnit is a test framework. It finds methods marked [Test], runs [SetUp] and [TearDown] code around them, feeds them data with [TestCase], checks results with Assert.That, runs tests in parallel and reports what passed and failed. It knows nothing about browsers. The dotnet test command, through the NUnit test adapter, is what discovers and runs the tests.

Concern Who handles it
Start and stop a browser Selenium (new ChromeDriver(), driver.Quit()), called from NUnit's [SetUp] and [TearDown]
Find elements, wait for them, interact Selenium (By, WebDriverWait, SelectElement)
Discover, filter and run tests NUnit and dotnet test ([Test], --filter, [Category])
Check results NUnit (Assert.That with constraints)
Run one test with several inputs NUnit ([TestCase], [TestCaseSource])
Run tests in parallel NUnit ([Parallelizable]); each test still needs its own browser
Screenshots on failure NUnit's TearDown and TestContext, using Selenium to take the screenshot

Here is the whole idea in one class:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

public class FirstTest
{
    private IWebDriver driver = null!;

    [SetUp]
    public void StartBrowser() => driver = new ChromeDriver();   // Selenium

    [Test]                                                        // NUnit
    public void WebFormHasTheRightTitle()
    {
        driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html");
        Assert.That(driver.Title, Is.EqualTo("Web form"));       // NUnit
    }

    [TearDown]
    public void StopBrowser() => driver.Quit();                   // runs pass or fail
}

Nothing here downloads a ChromeDriver binary. Since Selenium 4.6, Selenium Manager ships inside the Selenium.WebDriver package and finds or downloads the right driver the first time you start a browser. Tutorials that start with "install the Selenium.WebDriver.ChromeDriver package" or "download chromedriver.exe" are out of date.

Why NUnit?#

.NET has three widely used test frameworks, and Selenium works with all of them. This course uses NUnit because it has the most to offer a browser suite out of the box:

  • Setup and teardown at every level: per test, per fixture class, per namespace and per assembly.
  • Parameterised tests with [TestCase] and [TestCaseSource], so one method covers many inputs.
  • Fine-grained parallelism with [Parallelizable] and [FixtureLifeCycle], which chapter 7 depends on.
  • The constraint model, Assert.That(actual, Is.EqualTo(expected)), which reads well and fails with useful messages.

If your team already uses MSTest or xUnit, the Selenium code in this course carries over unchanged; only the attributes differ. TestingBot has examples for MSTest, xUnit, and for behaviour-driven tests with Reqnroll, the successor of SpecFlow.

What you'll build#

Chapter You add
1. Set up .NET, Selenium and NUnit a test project, Selenium Manager, a first passing test
2. Locators and waits the By strategies and WebDriverWait with lambdas, instead of Thread.Sleep
3. NUnit fixtures and attributes a BaseTest class, a driver factory, [TestCase] data, categories
4. Assertions and screenshots the constraint model, multiple-assert scopes, a screenshot for every failure
5. Page objects two small page objects with fluent methods
6. The TestingBot grid RemoteWebDriver, tb:options, pass/fail status on the grid
7. Parallel runs [Parallelizable], one fixture instance per test, worker counts
8. CI a GitHub Actions workflow with TRX results, and the Azure Pipelines equivalent
9. Quiz ten questions on the decisions the course made

The final project looks like this:

SeleniumCourse/
  SeleniumCourse.csproj   package references
  .editorconfig           one analyzer setting
  Parallelism.cs          assembly-level parallel settings
  Support/
    DriverFactory.cs      local or remote browser, from environment variables
    BaseTest.cs           SetUp/TearDown, screenshots, grid status
  Pages/
    WebFormPage.cs
    SubmittedPage.cs
  Tests/
    LocatorTests.cs
    WaitTests.cs
    WebFormTests.cs

The tests use the practice pages the Selenium project publishes for this purpose: a web form and a page that adds elements after a delay. They are public, stable and safe to hit from a test.

Prerequisites#

  • The .NET SDK. The examples use .NET 10, the current long-term support release; NUnit 5 runs on .NET 8 and later, so a .NET 8 SDK works too if you change the target framework.
  • Basic C#: classes, properties, lambdas.
  • Chrome installed locally (Firefox too, for the cross-browser parts).
  • No prior Selenium experience.

Sibling courses build the same suite in other languages: Selenium with Python and pytest and Selenium with Java and TestNG. And a common question up front: the Selenium project does not offer an official certification. What employers look for is a suite you can explain, which is what this course builds; TestingBot's own course certificate, if you want one, is described at the end.