Selenium WebDriver Setup
Selenium WebDriver drives real browsers through a standard protocol. Here is a working C# setup from scratch.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 29, 2026
Related tutorials
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#
// 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();
}
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#
- SetUp creates a headless Chrome before each test.
- The test navigates, types and clicks.
- The assertion checks the redirect.
- 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#
Leftover browser processes slow down or crash CI agents.
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
var options = new FirefoxOptions();
options.AddArgument("-headless");
driver = new FirefoxDriver(options);
Mini quiz#
Why run browsers headless in CI?
Want more? Practise all Automation Testing MCQs · Automation Testing interview questions
My notes#
Saved in this browser.