Lesson 84 of 93 · 6 min read · Updated
End-to-End Tests with Playwright for .NET: Real Browser, Real Kestrel Host
On this page
Integration tests with WebApplicationFactory stop at the HTTP layer. They never run the HTML and JavaScript that your users actually click. End-to-end tests close that gap: a real browser loads the page, the page calls the API, and the API talks to the database. This lesson shows how to start the real ASP.NET Core app on a Kestrel port inside the test process, drive it with Playwright's Chromium, and keep the tests few, stable and free of sleeps.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
Microsoft.Playwright.Xunit1.65.0 (providesPageTestandExpect)Microsoft.AspNetCore.Mvc.Testing11.0.0Microsoft.EntityFrameworkCore.Sqlite11.0.0- Chromium is downloaded on the first run (about 150 MB). Docker is not required.
The Page Under Test
The API serves a small static page from wwwroot/index.html. It has a form to add a book and a list of books. It is written without a framework on purpose: the test checks what the user sees, not how the page is built. The page is in Turkish, so the labels the tests use are Turkish too.
M09.IntegrationApi/wwwroot/index.html
Each input is wrapped in a <label>, so it has an accessible name. The error paragraph has role="alert". The list and counter have data-testid attributes. These are stable handles for tests. CSS classes can change without breaking anything.
Starting the Real App on a Real Port
WebApplicationFactory normally hosts the app on an in-memory TestServer, and a browser cannot connect to that. BooksAppServer uses the UseKestrel and StartServer support added in .NET 10 to start the same Program.cs on a real TCP port.
M09.IntegrationApi.E2E.Tests/Infrastructure/BooksAppServer.cs
Three details matter here.
Microsoft.Playwright.Program.Main(["install", "chromium"])installs the browser from .NET code. You do not need PowerShell orplaywright.ps1. Later runs use the cache (~/Library/Caches/ms-playwrighton macOS,~/.cache/ms-playwrighton Linux).UseKestrel(0)asks the operating system for a free port. The real address is read back fromIServerAddressesFeatureand becomes the browser's base URL.- The database is a shared SQLite in-memory database (
Mode=Memory;Cache=Shared). Kestrel handles requests on parallel threads, so eachDbContextopens its own connection instead of sharing one. The_keepAliveconnection stays open so the database is not deleted between requests.
The fixture also exposes ResetDatabaseAsync and SeedAsync, the same helpers as the integration-test factory.
Writing the Journeys
The test class inherits Playwright's PageTest, which gives each test a fresh browser context and Page. It shares one BooksAppServer through a collection fixture.
M09.IntegrationApi.E2E.Tests/BookCatalogJourneyTests.cs
The locators target what a user sees: a field by its label, a button by its role and name, an alert by its role. When a locator needs a hook that is not visible, it uses data-testid. No CSS selectors, so a redesign does not break the test.
There is no Thread.Sleep or Task.Delay. Expect(...) retries its assertion until it passes or the timeout (5 seconds by default) expires. The page fetches the list asynchronously after the click, and the assertion simply waits for the new row.
The duplicate test seeds its existing book directly into the database instead of filling the form twice. Only the step under test goes through the UI. Using the UI for arrange steps would make the test slower and give it more ways to fail for reasons unrelated to what it checks.
Pitfall: Too Many End-to-End Tests
E2E tests sit at the top of the test pyramid. They are the slowest and most fragile tests you have. This project has only three, one per critical journey: the list loads, a user can add a book, and a duplicate shows the server's conflict message. The 409 status code is already proven by an integration test and the unique index by an EF Core test. The E2E test exists to prove that the page shows that message to the user, which no lower-level test can do.
When a journey fails, the test keeps a Playwright trace. InitializeAsync starts tracing with screenshots and DOM snapshots, and DisposeAsync saves the trace only if the test failed (TestOk is false), under bin/.../playwright-traces/*.zip. Open it at https://trace.playwright.dev to step through every action. Green tests leave nothing on disk.
Summary
- End-to-end tests run the real HTML, JavaScript, API and database together in a real browser.
UseKestrel(0)plusStartServer()hosts the realProgram.cson a free TCP port that Playwright can reach.- Use a shared SQLite in-memory database with a keep-alive connection, because Kestrel serves requests in parallel.
- Locate elements by role, label and
data-testid; letExpectwait instead of sleeping. - Keep E2E tests few, seed data directly, keep them in their own project, and save traces only for failures.