# Write Playwright Tests for a Blazor Web App

Yes. An agent adds a Playwright test project to a Blazor Web App, runs the app against a real SQL Server in its sandbox and opens a PR with passing tests.

[Run this on Rush](https://cloud.getrush.ai/run/write-playwright-tests/blazor/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

End-to-end tests for Blazor fail for one reason more than any other: the page is on screen before it is interactive. So the agent reads which pages are which before it writes a selector. In an app like `10.0/BlazorWebAppMovies` in dotnet/blazor-samples, `Components/Pages/MoviePages/Index.razor` declares `@rendermode InteractiveServer`: it is prerendered, then a circuit connects, and only then does the search box, bound with `@bind:event="oninput"`, filter the QuickGrid. `Create.razor`, `Edit.razor` and `Delete.razor` declare no render mode. They are static server rendering with an `EditForm` marked `Enhance`, so a submit is a fetch that patches the page with no full reload. `Counter.razor` is interactive and `Weather.razor` uses stream rendering. There is no test project of any kind in the folder.

It adds an NUnit project beside the app with `Microsoft.Playwright.NUnit`, targeting `net10.0` like the app, and a fixture that starts the app as a real process on the `http` profile's address from `Properties/launchSettings.json`, `http://localhost:5128`, with the connection string supplied through `ConnectionStrings__BlazorWebAppMoviesContext`. The in-memory test server ASP.NET Core offers for integration tests has no port a browser can open, which is why the fixture launches the app itself and waits for it to answer. Browsers come from the `playwright.ps1 install` script the package generates in the build output; that script needs PowerShell, so the agent installs it as a .NET tool first.

The tests follow what the files say. On `/movies` a test waits for the `/_blazor` WebSocket before typing, then asserts with retrying expectations that typing `Warrior` leaves one row, `The Road Warrior`. `SeedData.cs` inserts five films and the grid's `PaginationState` has `ItemsPerPage = 5`, so a sixth film lands on a second page; the create test therefore finds its new row through the search box, never by expecting it in the visible list. Validation tests come from `Models/Movie.cs`: a two-character title, a genre starting with a lowercase letter, a price of 101 and a rating of `X` must each show a message and leave the URL on `/movies/create`. A valid submit must end on `/movies`. Edit and delete tests work on a film the test created, with a unique title, so the seeded rows other tests read are never changed.

To run them the agent needs SQL Server, since `appsettings.json` points at LocalDB, which exists only on Windows. The sandbox has no Docker, so it installs it with `sudo apt-get install -y docker.io`, starts `dockerd`, runs Microsoft's SQL Server image for Linux, and applies the migrations with `dotnet ef database update`. It runs `dotnet test` until the suite passes three times in a row, and treats a test that passes only sometimes as a missing wait to find, not as something to retry.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write Playwright end-to-end tests for this Blazor Web App and open a pull request. Read the .csproj, Program.cs, appsettings.json, Properties/launchSettings.json, Data/SeedData.cs, Models/Movie.cs, Components/Layout/NavMenu.razor and every page under Components/Pages first, and note each page's render mode. Add an NUnit test project next to the app using Microsoft.Playwright.NUnit on the app's target framework, and add it to the solution file. Write a fixture that starts the app as a process on the http launch profile's URL, with the connection string taken from the ConnectionStrings environment variable, and waits until it answers. Cover: navigation through the menu links; the movie list showing the seeded films; search filtering the grid, waiting for the interactive connection before typing; sorting by title; creating a film and finding it through search; each validation rule on the model showing its message without leaving the form; editing and deleting a film the test itself created; and the not-found page for an unknown id. Never modify or delete seeded rows. Use role and label locators and retrying assertions, with no fixed sleeps. Install Docker in the sandbox, start SQL Server from Microsoft's Linux image, apply the migrations, install the Playwright browser, and run dotnet test until it passes three times in a row. Add a README section with the commands, and put the dotnet test summary in the pull request description. Do not change the app's code.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that adds one test project and touches nothing in the app: a project file with the Playwright and NUnit packages, the fixture that launches the app, a small helper that waits for the interactive connection, and test classes grouped by page. The solution file gains the project, and the README gains the commands to start a database, install the browser and run the suite. The description quotes the `dotnet test` summary from the last of the consecutive runs and lists which page each test class covers.

A reviewer reads the helper first, because every interactive test depends on it, and checks that no test contains a fixed delay or depends on the order tests run in. They check the fixture stops the app process when the run ends, and that the database password used in the sandbox appears nowhere in the committed files. If the team's CI has no SQL Server yet, the description says what the tests need from it, a connection string in one environment variable, and leaves the workflow change to a follow-up on the same session.

The agent also reports what it did not test and why: the reconnect dialog in `ReconnectModal.razor` appears only when the circuit drops, which needs the server to be interrupted mid-test, and the description proposes that as a separate, slower test if the team wants it.

## Run it on every pull request that changes a page

In the Project that holds the repository, add an automation with a `github` trigger on `pull_request.opened` and `pull_request.synchronize`, and a prompt that tells the run to look at which `.razor` files under `Components/Pages` the pull request changes, add or update the end-to-end tests for those pages on the pull request's branch, and comment with the test summary. The trigger places the pull request after the prompt and fills in nothing else, so the prompt has to state the rule in full.

Set the automation's max runs per hour to match how often the team pushes; events past the cap wait as queued runs. The trigger fires only for an owner, member or collaborator, so a pull request from a fork gets no run.

## Why run it on Rush?

Browser tests for this kind of app need four things running together: SQL Server, the migrated schema, the app itself and a browser. On a developer's machine that is an afternoon of setup before the first assertion, and the flakiness that follows is hard to tell apart from a slow laptop. The agent assembles all four in its own sandbox, runs the suite repeatedly there, and the output of every run is kept in the session, so a test that failed once on the way is visible and not quietly retried away.

Because the sandbox has a real browser, the agent can also look at the page while it writes, which is how it learns that a row count changes after the circuit connects and not before. The pull request is the result; the transcript, read with `GET /o/{org}/sessions/{id}/messages`, is the proof that the tests passed against a real database and not a stand-in.

## Frequently asked questions

### Why do Playwright tests on Blazor Server pages fail intermittently?

Usually because the test acts on prerendered HTML before the circuit is connected. The element exists, the click or keystroke is delivered, and nothing on the server hears it. Waiting for the interactive connection once per page, and asserting with expectations that retry, removes most of it.

### Does it use the .NET Playwright package or the Node one?

The .NET package by default, so the tests live in the solution and run under `dotnet test` with everything else. If the repository already has a Node toolchain and the prompt says so, the agent writes the suite with `@playwright/test` and the same fixtures.

### Can the tests run without SQL Server?

Not without changing the app, which the prompt forbids. The app is built on the SQL Server provider and its migrations are written for it. The suite takes a connection string from the environment, so any reachable SQL Server works, including a container in CI.

### How does it keep tests from interfering with each other?

Every test that writes creates its own film with a unique title and removes it afterwards, and tests that read rely only on the seeded rows, which no test modifies. That keeps the suite independent of execution order and safe to run in parallel classes.

## More Blazor jobs

- [Add CSV Export to a Blazor Web App](https://getrush.ai/run/add-csv-export/blazor/)
- [Add Dark Mode to a Blazor Web App](https://getrush.ai/run/add-dark-mode/blazor/)
- [Add a Dockerfile to a Blazor Web App](https://getrush.ai/run/add-dockerfile/blazor/)

## The same job on other stacks

- [Write Playwright Tests for an Eleventy Blog](https://getrush.ai/run/write-playwright-tests/eleventy/)
- [Write Playwright Tests for a FastAPI and React App](https://getrush.ai/run/write-playwright-tests/fastapi/)
- [Write Playwright Tests for a Gatsby Site](https://getrush.ai/run/write-playwright-tests/gatsby/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
