A minimal Selenium with C# test example repository showing a simple test structure, NUnit framework integration, and dotnet CLI commands to run tests. This repo is intended as a learning starting point or quick template for writing end-to-end tests with Selenium and C#.
- About
- Prerequisites
- Install
- Run tests
- Selenium configuration highlights
- Tests and examples
- Project structure
- Troubleshooting & tips
This repository demonstrates how to set up Selenium WebDriver with C# and NUnit with a small number of example tests. It includes:
- A project file (
.csproj) with Selenium WebDriver and NUnit dependencies. - Example tests in the
Tests/directory. - Base test class for WebDriver initialization and teardown.
- Useful dotnet CLI commands for running tests.
- .NET 8.0 or newer. Check with
dotnet --version. - A compatible WebDriver for your browser (e.g., ChromeDriver for Chrome).
- ChromeDriver can be installed via NuGet package
WebDriverManager.
- ChromeDriver can be installed via NuGet package
- Clone the repository (if you haven't already):
git clone https://github.com/qa-shrine/Selenium-Basic-Example.git
cd Selenium-Basic-Example- Install dependencies:
dotnet restoreNote: Selenium WebDriver, NUnit, and WebDriverManager are installed as dependencies when you run dotnet restore.
Available dotnet CLI commands:
dotnet test— run all tests in the solution.dotnet test --verbosity=normal— run tests with detailed output.dotnet test --filter "ClassName"— run tests by class name.
Examples:
Run the full test suite:
dotnet testRun tests with detailed output:
dotnet test --verbosity=normalRun a specific test class:
dotnet test --filter "BasicTests"Key settings are in the base test class (BaseTest.cs):
- WebDriver initialization in
SetUp()— instantiates ChromeDriver at the beginning of each test. - Implicit wait configuration — waits up to 10 seconds for elements to be present.
- Base URL constant —
https://practicetestautomation.com/used in tests. - Driver cleanup in
TearDown()— ensures the browser closes after each test.
The example uses NUnit as the test framework with [TestFixture] and [Test] attributes.
There are two example tests in the Tests/ directory:
BasicTest.cs— simple smoke test that navigates tohttps://practicetestautomation.com/and asserts the page title contains "Practice Test Automation".LoginTest.cs— a small illustrative login flow that finds username/password inputs, enters credentials, and verifies a welcome element. It usesidselectors asdata-testidattributes are not available but would be recommended practice for test stability.
These tests are intentionally minimal. Use them as templates when adding real tests for your application.
At a glance:
SeleniumBasicExample.csproj # Project file with NuGet dependencies
BaseTest.cs # Base test class with WebDriver setup/teardown
Tests/
BasicTest.cs # Example smoke test
LoginTest.cs # Example login flow test
bin/ # Compiled output
obj/ # Intermediate build files
README.md # This file (Selenium example)
-
WebDriver not found:
- Ensure WebDriverManager NuGet package is installed:
dotnet add package WebDriverManager. - WebDriverManager will automatically download and manage WebDriver versions.
- Ensure WebDriverManager NuGet package is installed:
-
Tests are failing to find elements:
- Use browser DevTools to inspect the page and verify element selectors.
- Add implicit waits in
BaseTest.csor use explicit waits (WebDriverWait) for specific elements. - Consider using
data-testidattributes if you control the application being tested.
-
Browser not launching:
- Ensure your browser (Chrome, Firefox, etc.) is installed and up-to-date.
- Check that the WebDriver version matches your browser version.
- Try running
dotnet test --verbosity=diagnosticfor more debugging information.
-
Running against a local app:
- If your app runs on a specific port (e.g.,
localhost:3000), update theBaseUrlconstant inBaseTest.csor usedriver.Navigate().GoToUrl("http://localhost:3000/path")in individual tests.
- If your app runs on a specific port (e.g.,
-
NUnit not recognizing tests:
- Ensure your test class inherits from the base test class and has
[TestFixture]attribute. - Test methods must have the
[Test]attribute. - Rebuild the solution:
dotnet buildbefore running tests.
- Ensure your test class inherits from the base test class and has