End-to-end tests with Playwright
Drive a real browser against a running Taskboard with Playwright: role and label locators, auto-waiting assertions, and a web server the config starts.
Integration tests for the API and databaseForms from submit to databaseThe sign-up and login flow
End-to-end tests with Playwright
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- An end-to-end test proves the browser, the client code, the API and the database agree, which no test below it can.
- Locate elements by role, label and visible text, the way a user finds them, so tests survive refactors that change class names.
- Playwright assertions such as toHaveText retry until the page settles, so you should not add sleeps or manual waits.
- The webServer option in the config starts the app before the run and waits on a URL, so one command runs the whole suite.
- Keep these tests to a handful of user journeys, and give each journey its own data so tests do not depend on one another.
What only a browser can check
Your API tests show that POST /api/projects/1/tasks with a good body stores a row. They cannot show that the form on the page sends that body, that the list refreshes afterwards, or that the error message the server chose is the one the user sees. Those are seams between the client and the API, and the only way to test them is to run both and look at the page.
That is what Playwright does. It starts a real browser (Chromium, Firefox or WebKit), loads your app, clicks and types as a user would, and asserts on what appears. Because it exercises everything, one passing test is strong evidence. Because it exercises everything, a failing one is hard to localise. That trade is why the previous lesson capped these at a handful of journeys.
One command starts the app and the tests
For Taskboard I made a tiny server that serves a page and the API from the same process, and pointed Playwright at it:
// playwright.config.ts
import { defineConfig } from "@playwright/test";
export default defineConfig({
testDir: "e2e",
use: { baseURL: "http://localhost:4173", trace: "retain-on-failure" },
webServer: {
command: "npx tsx src/server.ts",
url: "http://localhost:4173/health",
reuseExistingServer: false,
},
});
webServer runs the command, polls the URL until it answers, runs the tests, and shuts the server down. In your real repo the command would build and serve the Vite client and the API; the shape is the same. trace: "retain-on-failure" records a trace for each failing test, which you can open to step through the page state, network calls and DOM at each action.
Two journeys
import { test, expect } from "@playwright/test";
test("add a task and see it after a reload", async ({ page }) => {
await page.goto("/");
await page.getByLabel("New task").fill("Ship it");
await page.getByRole("button", { name: "Add" }).click();
await expect(page.getByRole("listitem")).toHaveText(["Ship it"]);
await page.reload();
await expect(page.getByRole("listitem")).toHaveText(["Ship it"]);
});
test("an empty title shows the error the server decided", async ({ page }) => {
await page.goto("/");
await page.getByRole("button", { name: "Add" }).click();
await expect(page.getByRole("alert")).toHaveText("Title is required");
});
I ran both with @playwright/test 1.65.1 on Chromium. They passed in 2.9 seconds total, including starting the server.
The reload in the first test is the important line. Seeing the task appear proves the client updated. Seeing it survive a reload proves it reached the database. That one extra step turns “the UI looks right” into “the data is stored”.
Locators and waiting
Two habits make these tests last.
Find things the way a user does. getByLabel("New task") and getByRole("button", { name: "Add" }) match the accessible name, not a class. If you rename a CSS class, nothing breaks. If you forget to associate a label with its input, the locator fails, and that is a real accessibility bug caught for free. Reach for getByTestId only when nothing user-facing identifies the element.
Never sleep. expect(...).toHaveText(...) is a retrying assertion: it checks, waits a moment, checks again, until it passes or hits the timeout. The click handler fetches, the server responds, the list re-renders, and the assertion simply catches up. A setTimeout(500) in a test is a bet on how slow your machine is, and CI always loses that bet eventually. Playwright also auto-waits before actions such as click, until the element is visible and enabled.
Data and sign-in
Two problems show up as soon as there are more than two tests.
Shared state. My two tests ran against one in-memory database. They happen not to interfere, but if the second asserted “the list is empty” it would fail whenever the first ran before it. Give each journey the data it needs. Either create it through the API at the start of the test (fast), or give each worker its own database, or reset between tests. Do not rely on test order.
Signing in. Most Taskboard journeys start signed in. Logging in through the form in every test is slow and makes every test depend on the login UI. Playwright can sign in once in a setup step, save the cookies and local storage to a file (storageState), and start each test already authenticated. The linked Artem Bondar video covers that pattern. Test the real login form in exactly one journey, as the sign-up and login flow lesson built it.
Flakiness checklist
A flaky end-to-end test usually has one of these causes:
- A fixed wait instead of a retrying assertion.
- A locator matching more than one element (Playwright errors on this in strict mode, which is a feature).
- A test that depends on data created by another.
- An animation or network call the assertion does not wait for.
- The test hitting a real third party, such as email or payments. Stub those at the network edge with
page.route, or point the app at a fake.
When one does fail in CI, the trace file shows each step with a DOM snapshot, so you can see what the page looked like at the moment of the failure instead of guessing.
Where these tests do not belong
Do not use Playwright to test validation rules, permission matrices or every error case. Those are quicker and clearer as API tests. Component-level behaviour belongs with the React testing tools. If your app is Next.js there is a framework-specific lesson, end-to-end testing with Playwright, that covers how the dev server and server components change the setup.
What the video covers
Cosden Solutions’ “React Testing with Playwright (Complete Tutorial)” runs about 33 minutes, is two years old, and, going by its title, tests a React app with Playwright. It is the closest match I found to a React plus Playwright introduction from a single creator. I could not read a transcript, so I do not know whether it covers sign-in state or CI. Playwright’s API moves quickly, so check names against the current docs. The Artem Bondar video is the follow-up for reusing a signed-in session.
Where this fits
Tests that live only on your laptop protect nobody. The next lesson runs the whole stack, from lint through integration to browser tests, on every push: continuous integration for a full-stack repo.
Check yourself
Why prefer getByRole and getByLabel over a CSS selector?
They match what a user perceives and what assistive technology uses, so the test keeps working when markup or class names change, and a failing locator often points at a real accessibility problem.
Why does await expect(locator).toHaveText(...) not need a sleep?
Playwright retries the assertion until it passes or times out, so it waits for the fetch and re-render to finish.
What does webServer in playwright.config.ts do?
It runs a command to start the app, polls the given url until it responds, and then runs the tests, stopping the server afterwards.
What is the risk of tests sharing one running database?
State from one test leaks into the next, so a test can pass or fail depending on order unless each journey creates its own data or the data is reset.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.