In the fast-paced world of modern web development, automated testing is more than a luxury—it’s a necessity. Enterprise companies and growing startups need reliable, scalable, and maintainable automation frameworks to ensure the quality of their applications.
One of the most powerful tools available today is Playwright for C#, a robust browser automation framework by Microsoft tailored for the .NET ecosystem.
Whether you’re just getting started with browser automation in .NET or looking to scale your automated testing infrastructure, this guide will walk you through everything you need to know about using Playwright with C# for high-quality, end-to-end testing.
Why Use Playwright for C#?
Playwright for C# offers a compelling set of features that make it a strong contender for any C# browser testing framework:
- Cross-browser automation with Chromium, Firefox, and WebKit
- Auto-waiting for elements to be ready before interactions
- Parallel test isolation using independent browser contexts
- Support for modern web apps like SPAs, iframes, and shadow DOM
- Robust C# API for building scalable automation scripts
Setting Up Playwright in a C# Project
Prerequisites
To begin, make sure you have the following tools installed:
- .NET SDK (6.0 or later)
- Visual Studio, VS Code, or JetBrains Rider
Project Initialization
Create a new console project and install the required Playwright package:
bash
CopyEdit
dotnet new console -n PlaywrightDemo
cd PlaywrightDemo
dotnet add package Microsoft.Playwright
Now install the Playwright browsers:
For Windows:
bash
CopyEdit
dotnet build
pwsh bin/Debug/net6.0/playwright.ps1 install
For Linux/macOS:
bash
CopyEdit
dotnet build
bash bin/Debug/net6.0/playwright.sh install
Your First Playwright Script in C#
csharp
CopyEdit
using Microsoft.Playwright;
class Program
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new() { Headless = false });
var page = await browser.NewPageAsync();
await page.GotoAsync(“https://playwright.dev”);
await page.ScreenshotAsync(new() { Path = “screenshot.png” });
}
}
This simple script launches a browser, navigates to a website, and captures a screenshot.
Core Concepts of Playwright in .NET
Understanding the building blocks of Playwright for C# is crucial for designing reliable test suites.
- Browser: The actual browser engine (Chromium, Firefox, WebKit)
- BrowserContext: An isolated session like incognito mode
- Page: A single tab or window inside a context
Each context can have multiple pages and supports session isolation for parallel test execution.
Element Interactions with Auto-Waiting
Playwright automatically waits for the DOM elements to be ready before acting:
csharp
CopyEdit
await page.GotoAsync(“https://example.com”);
await page.ClickAsync(“button#submit”);
await page.FillAsync(“#search”, “Playwright C#”);
await page.SelectOptionAsync(“select#country”, “US”);
This makes tests more stable and reduces timing-related errors.
Using Locators for Better Control
Locators in Playwright give developers a powerful way to interact with page elements:
csharp
CopyEdit
var button = page.Locator(“button#submit”);
await button.ClickAsync();
var activeItem = page.Locator(“.list”).Locator(“.item.active”);
Locators support chaining, filtering, and text-based querying—essential for dynamic web apps.
Writing Tests Using NUnit with Playwright for C#
You can integrate Playwright with testing frameworks like NUnit, xUnit, or MSTest.
Example: Basic NUnit Test
csharp
CopyEdit
[TestFixture]
public class PlaywrightTests
{
private IPlaywright _playwright;
private IBrowser _browser;
private IPage _page;
[SetUp]
public async Task SetUp()
{
_playwright = await Playwright.CreateAsync();
_browser = await _playwright.Chromium.LaunchAsync();
_page = await _browser.NewPageAsync();
}
[TearDown]
public async Task TearDown()
{
await _browser.DisposeAsync();
_playwright.Dispose();
}
[Test]
public async Task HomepageHasCorrectTitle()
{
await _page.GotoAsync(“https://playwright.dev”);
Assert.AreEqual(“Playwright”, await _page.TitleAsync());
}
}
This setup enables you to start building a scalable, automated testing suite in C#.
Advanced Features of Playwright for .NET
Network Request Mocking
csharp
CopyEdit
await page.RouteAsync(“**/api/users/**”, async route =>
{
await route.FulfillAsync(new()
{
Status = 200,
Body = “[{\”id\”: 1, \”name\”: \”Test User\”}]”,
ContentType = “application/json”
});
});
Session-Based Authentication
csharp
CopyEdit
await context.StorageStateAsync(new() { Path = “auth.json” });
var authContext = await browser.NewContextAsync(new()
{
StorageStatePath = “auth.json”
});
Real-World Login Test with Playwright for C#
csharp
CopyEdit
[Test]
public async Task UserCanLogin()
{
await _page.GotoAsync(“https://example.com/login”);
await _page.FillAsync(“#email”, “test@example.com“);
await _page.FillAsync(“#password”, “password123”);
await _page.ClickAsync(“#login-button”);
await _page.WaitForSelectorAsync(“.dashboard”);
var welcome = await _page.TextContentAsync(“.welcome-message”);
Assert.That(welcome, Does.Contain(“Welcome, Test User”));
}
Best Practices for Scalable C# Test Automation
Use Page Object Model (POM)
csharp
CopyEdit
public class LoginPage
{
private readonly IPage _page;
public LoginPage(IPage page) => _page = page;
public async Task LoginAsync(string user, string pass)
{
await _page.GotoAsync(“https://example.com/login”);
await _page.FillAsync(“#username”, user);
await _page.FillAsync(“#password”, pass);
await _page.ClickAsync(“#login-button”);
}
}
Selector Strategies
- text=Submit
- button:has-text(“Submit”)
- data-testid=submit-button
Timeout Configuration
csharp
CopyEdit
var context = await browser.NewContextAsync(new() { NavigationTimeout = 30000 });
await page.ClickAsync(“#slow-button”, new() { Timeout = 60000 });
CI/CD Pipeline Integration
Azure DevOps Example:
yaml
CopyEdit
trigger:
– main
pool:
vmImage: ‘ubuntu-latest’
steps:
– task: UseDotNet@2
inputs:
packageType: ‘sdk’
version: ‘6.0.x’
– script: |
dotnet build
dotnet test
displayName: ‘Build and Test with Playwright for C#’
Conclusion
Playwright for C# is a powerful and reliable solution for teams looking to automate browser tests in the .NET ecosystem.
From cross-browser automation to real-world testing scenarios, Playwright simplifies test writing while offering deep control and flexibility.
For enterprise development teams and startups seeking to improve quality assurance pipelines, adopting Playwright in your C# projects can lead to faster releases, fewer bugs, and a more scalable test infrastructure.
Important Links:
Additional Resources: