Automation Testing · Selenium WebDriver

Selenium WebDriver Setup

Selenium WebDriver drives real browsers through a standard protocol. Here is a working C# setup from scratch.

Beginner 5 min Lesson 5 of 24
0/24 in Automation Testing
On this page · 5 min · Beginner

Learning objectives#

  • Install Selenium for .NET
  • Write and run a first test
  • Run headless for CI
  • Clean up the browser after each test

Explanation#

Packages: Selenium.WebDriver plus a test framework (NUnit here). Selenium Manager downloads the matching browser driver automatically.

Create a driver (new ChromeDriver(options)), navigate, find elements, act and assert.

Headless mode runs without a visible window — required on most CI servers.

Always Quit() the driver in teardown, or browsers pile up and memory runs out.

Code example: First Selenium test#

C#LoginTests.cs Illustrative
// dotnet new nunit -n Shop.UiTests && cd Shop.UiTests
// dotnet add package Selenium.WebDriver
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;


public class LoginTests
{
    private IWebDriver driver = null!;


    [SetUp]
    public void Start()
    {
        var options = new ChromeOptions();
        options.AddArgument("--headless=new");
        options.AddArgument("--window-size=1366,900");
        driver = new ChromeDriver(options);
    }


    [Test]
    public void Valid_login_shows_dashboard()
    {
        driver.Navigate().GoToUrl("https://staging.shop.example/login");
        driver.FindElement(By.Id("email")).SendKeys("asha@example.com");
        driver.FindElement(By.Id("password")).SendKeys("Passw0rd!");
        driver.FindElement(By.CssSelector("button[type=submit]")).Click();


        Assert.That(driver.Url, Does.Contain("/dashboard"));
    }


    [TearDown]
    public void Stop() => driver.Quit();
}
Expected output
Passed!  - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: 3 s

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. SetUp creates a headless Chrome before each test.
  2. The test navigates, types and clicks.
  3. The assertion checks the redirect.
  4. TearDown always quits the browser.

Real-world use case#

A .NET team adds Selenium UI tests to the same solution as their xUnit/NUnit unit tests.

Common mistakes#

Forgetting driver.Quit()

Leftover browser processes slow down or crash CI agents.

Hard-coded URLs and credentials

Read them from configuration or environment variables.

Best practices#

  • Headless in CI
  • Quit in teardown
  • Config for URLs and users
  • Explicit waits (next lessons)

Remember this#

Selenium.WebDriver + test framework.

Selenium Manager handles drivers.

Always Quit().

Practice exercise#

Your task: Run the same test in Firefox.

Show solution
C# Illustrative
var options = new FirefoxOptions();
options.AddArgument("-headless");
driver = new FirefoxDriver(options);

Mini quiz#

Selenium WebDriver Setup quizMultiple choice Question 1 of 1

Why run browsers headless in CI?

Score so far: 0/0

Want more? Practise all Automation Testing MCQs · Automation Testing interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.