E2E Testing C#: A Guide for Those Tired of Bugs in Production

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Unit Tests Don't Save You, and When You Need E2E Tests

\n\n

Imagine: you've written perfect C# code. Unit tests are green, coverage is 95%, code review passed. You ship a release, and users write: "The 'Place Order' button doesn't work if there are more than 3 items in the cart." Sound familiar?

\n\n

Unit tests check methods in isolation. Integration tests check the connection between the database and API. But E2E testing C# applications (End-to-End) is the only way to ensure the entire system works as a single unit: browser, server, database, external APIs. End-to-end tests simulate real user actions: clicks, data input, waiting for page loads.

\n\n

In this article, we won't just cover theory. We'll show you how to write E2E tests in C# using Selenium and Playwright, how to organize code with the Page Object Model, and how to integrate automated tests into CI/CD. Let's go.

\n\n

What is E2E Testing C# in Practice

\n\n

An E2E test is a scenario that covers the entire user journey: from opening a page to completing a target action (purchase, registration, form submission). For a C# developer, this typically means:

\n\n
    \n
  • Launching a browser (Chrome, Firefox, Edge) via WebDriver
  • \n
  • Finding elements on the page (buttons, input fields, links)
  • \n
  • Performing actions (click, text input, selection from a list)
  • \n
  • Verifying the expected result (a message appears, URL changes, data updates)
  • \n
  • Closing the browser and cleaning up test data
  • \n
\n\n

The main difference from unit tests: E2E tests work with the real UI. They are slow, fragile (dependent on CSS selectors), but invaluable for checking critical business scenarios.

\n\n

Tools for E2E Testing C#: Selenium vs Playwright

\n\n

Choosing a tool is the first question a team faces. Let's look at the two most popular options.

\n\n

Selenium WebDriver

\n

Industry standard. Works via WebDriver — a separate process that controls the browser. Supports all browsers, has a huge community, thousands of ready-made solutions. Downside: setup requires drivers (ChromeDriver, GeckoDriver), and tests can be slow due to waits.

\n\n

Playwright for .NET

\n

A newer but already very popular tool from Microsoft. Works directly via the DevTools protocol, without extra layers. Faster, more stable, automatically waits for elements to load (no explicit Thread.Sleep). Supports not only browsers but also mobile emulators.

\n\n

In this article, we'll show examples using both frameworks so you can compare.

\n\n

Writing Your First E2E Test in C#: Selenium

\n\n

Let's start with the classic. Install the package via NuGet:

\n\n
dotnet add package Selenium.WebDriver\ndotnet add package Selenium.Support\ndotnet add package Selenium.WebDriver.ChromeDriver
\n\n

Now let's write a test for a sample website (let's assume it's an online store). We want to verify that after adding a product to the cart, a badge with the item count appears on the page.

\n\n
using OpenQA.Selenium;\nusing OpenQA.Selenium.Chrome;\nusing OpenQA.Selenium.Support.UI;\n\npublic class CartE2ETests\n{\n    [Fact]\n    public void AddItemToCart_ShouldUpdateCartBadge()\n    {\n        using var driver = new ChromeDriver();\n        \n        // Arrange\n        driver.Navigate().GoToUrl("https://demo-store.com");\n        driver.Manage().Window.Maximize();\n        \n        // Act\n        var addToCartButton = driver.FindElement(By.CssSelector(".product-card .add-to-cart"));\n        addToCartButton.Click();\n        \n        // Wait for badge to appear\n        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));\n        var cartBadge = wait.Until(drv => \n            drv.FindElement(By.CssSelector(".cart-badge")));\n        \n        // Assert\n        Assert.Equal("1", cartBadge.Text);\n        \n        driver.Quit();\n    }\n}
\n\n

Blogs

Book Recommendations