Playwright with C#: building a first test suite that lasts
By Denis Bojkovski, Test Automation Architect · Published November 25, 2026
Where C# fits
Playwright was the most named test tool on QAAnywhere on 25 November 2026: 29 of 236 remote QA roles mentioned it. Most tutorials use TypeScript, but Playwright has an official .NET library, and it is the natural choice for a team whose product is written in C#. C# or .NET appeared in 6 of the 86 listings that carried a full description that day, against 16 for TypeScript, so treat it as a fit for .NET teams rather than the widest door.
Set up the project
dotnet new nunit -n Shop.E2E
cd Shop.E2E
dotnet add package Microsoft.Playwright.NUnit
dotnet build
pwsh bin/Debug/net8.0/playwright.ps1 install
The last command downloads the browsers. Forgetting it is the most common first failure, locally and in CI. The folder name must match the project's target framework.
A first test
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
[Parallelizable(ParallelScope.Self)]
public class CheckoutTests : PageTest
{
[Test]
public async Task Guest_can_reach_payment()
{
await Page.GotoAsync("https://shop.example.com");
await Page.GetByRole(AriaRole.Link, new() { Name = "Blue mug" }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "Add to cart" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "Checkout" }).ClickAsync();
await Expect(Page).ToHaveURLAsync(new Regex("/paymentquot;));
}
}
PageTest gives every test its own browser context, so cookies and storage never leak between tests. Expect retries until the condition holds or times out, which is why there is no sleep in the test.
Choose locators people would recognise
GetByRolewith a name: what a user, or a screen reader, perceives.GetByLabelfor form fields.GetByTestIdwhen the text changes often or is translated.- CSS only when nothing else identifies the element.
A test that cannot find a button by its role is sometimes telling you about a real accessibility defect. Report it before reaching for CSS.
Log in once, not in every test
Signing in through the interface before each test is slow and makes every test depend on the login page. Sign in once, save the state, and reuse it:
await Page.Context.StorageStateAsync(new() { Path = "state/customer.json" });
public override BrowserNewContextOptions ContextOptions() =>
new() { StorageStatePath = "state/customer.json" };
Keep one dedicated test that does exercise the login form. Use separate accounts per role, add the state folder to .gitignore, and create it at the start of the pipeline: the file contains live session cookies.
Capture traces for failures
A trace records actions, snapshots, network calls and the console, so a colleague in another time zone can investigate without rerunning anything.
[SetUp]
public Task StartTrace() => Context.Tracing.StartAsync(new()
{
Screenshots = true, Snapshots = true, Sources = true,
});
[TearDown]
public async Task SaveTrace()
{
var failed = TestContext.CurrentContext.Result.Outcome.Status
== NUnit.Framework.Interfaces.TestStatus.Failed;
await Context.Tracing.StopAsync(new()
{
Path = failed ? quot;traces/{TestContext.CurrentContext.Test.ID}.zip" : null,
});
}
Open one with the Playwright script's show-trace command. Traces can hold personal data and tokens, so limit who can download them and how long they are kept.
Mistakes that age badly
- Testing everything through the browser. Price rules and permissions belong in API or unit tests; keep the browser for journeys.
- A base class that does everything. Prefer small helpers that tests call explicitly.
- Shared accounts with parallel tests. Two tests editing the same cart will eventually collide.
- Retries as a cure. A retry hides a fault until it fails twice. Log every retried test and fix it.
What to show an employer
A small public repository with ten clear tests, a pipeline that runs on every push, saved traces and a README explaining what you chose not to automate says more than a hundred shallow tests. See building a QA portfolio and Playwright vs Selenium vs Cypress.