Skip to content

qa-shrine/Selenium-Basic-Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Selenium Basic Example

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#.

Table of contents

  • About
  • Prerequisites
  • Install
  • Run tests
  • Selenium configuration highlights
  • Tests and examples
  • Project structure
  • Troubleshooting & tips

About

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.

Prerequisites

  • .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.

Install

  1. Clone the repository (if you haven't already):
git clone https://github.com/qa-shrine/Selenium-Basic-Example.git
cd Selenium-Basic-Example
  1. Install dependencies:
dotnet restore

Note: Selenium WebDriver, NUnit, and WebDriverManager are installed as dependencies when you run dotnet restore.

Run tests

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 test

Run tests with detailed output:

dotnet test --verbosity=normal

Run a specific test class:

dotnet test --filter "BasicTests"

Selenium configuration highlights

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.

Tests and examples

There are two example tests in the Tests/ directory:

  • BasicTest.cs — simple smoke test that navigates to https://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 uses id selectors as data-testid attributes 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.

Project structure

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)

Troubleshooting & tips

  • WebDriver not found:

    • Ensure WebDriverManager NuGet package is installed: dotnet add package WebDriverManager.
    • WebDriverManager will automatically download and manage WebDriver versions.
  • Tests are failing to find elements:

    • Use browser DevTools to inspect the page and verify element selectors.
    • Add implicit waits in BaseTest.cs or use explicit waits (WebDriverWait) for specific elements.
    • Consider using data-testid attributes 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=diagnostic for more debugging information.
  • Running against a local app:

    • If your app runs on a specific port (e.g., localhost:3000), update the BaseUrl constant in BaseTest.cs or use driver.Navigate().GoToUrl("http://localhost:3000/path") in individual tests.
  • 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 build before running tests.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages