End-to-End Tests for a React App With Playwright: A Full Dress Rehearsal
Two Playwright journeys for a React task board in real Chromium: the webServer block that starts the app, auto-waiting in the call log, a first draft that times out on '6 of 6 shown', and the page snapshot and trace that show the test was wrong, not the app.
What you will learn
- Explain what an end-to-end test runs: test runner, real browser and the app
- Let Playwright start the dev server with the webServer block
- Write a journey with getByRole, fill, click and web-first expect assertions
- Read a timeout failure, the page snapshot and the trace to find the real cause
Before you start
- React component tests with Vitest and Testing Library
- Running a Vite app with npm run dev
Before opening night, a theatre runs a full dress rehearsal: real stage, real costumes, the whole play from the first scene to the last. An end-to-end test is that rehearsal for your app. It opens a real browser, uses the page like a person, and checks what the page says at the end.
This article runs two Playwright journeys against the task board from the component tests article. Every log line, error and timing comes from the project's own test runs.
Three things run together
Playwright starts the app for you
import { defineConfig, devices } from '@playwright/test'
export default defineConfig({
testDir: './e2e',
reporter: 'list',
expect: { timeout: 3_000 },
use: {
baseURL: 'http://localhost:5173',
trace: 'retain-on-failure',
},
projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
// start the app before the tests, stop it after
webServer: {
command: 'npm run dev',
url: 'http://localhost:5173',
reuseExistingServer: true,
},
})
The webServer block runs npm run dev, keeps knocking on the url until the app answers, and only then starts the tests. reuseExistingServer: true means: if the app is already running, use that one. A few other lines matter later: baseURL so tests can write page.goto('/'), a 3-second expect timeout, and trace: 'retain-on-failure', which keeps a trace for every failed test. The project runs it with "e2e": "playwright test".
The test is a script for the browser
import { expect, test } from '@playwright/test'
test('the done filter lists only finished tasks', async ({ page }) => {
await page.goto('/')
await expect(page.getByText('5 of 5 shown')).toBeVisible()
await page.getByRole('button', { name: 'done' }).click()
await expect(page.getByText('2 of 5 shown')).toBeVisible()
await expect(page.getByText('Write report')).toBeVisible()
await expect(page.getByText('Email Jack')).toBeHidden()
})
test('a new task joins the open list', async ({ page }) => {
await page.goto('/')
await page.getByRole('button', { name: 'open' }).click()
await page.getByRole('textbox', { name: 'New task' }).fill('Pay rent')
await page.getByRole('button', { name: 'add' }).click()
await expect(page.getByText('4 of 6 shown')).toBeVisible()
await expect(page.getByText('Pay rent')).toBeVisible()
})
The second test is the journey: open the page, click open, fill the box named New task with "Pay rent", click add, then check. Everything is found by role and name, the way a person would describe it. With the open filter on and six tasks in total, the page should say "4 of 6 shown".
Playwright waits until the page is ready
Here is Playwright's own call log for the click on open:
waiting for getByRole('button', { name: 'open' })
locator resolved to <button class="" aria-pressed="false">open</button>
attempting click action
waiting for element to be visible, enabled and stable
element is visible, enabled and stable
scrolling into view if needed
done scrolling
performing click action
click action done
waiting for scheduled navigations to finish
navigations have finished
It finds the button, waits until it is visible, enabled and stable, clicks, then waits for the page to settle. A button that's still moving or still disabled doesn't get clicked. That is why the test has no sleep or manual waits, and why expect(...).toBeVisible() keeps retrying until its timeout.
One flow passes, one fails
The first draft of the second test expected "6 of 6 shown". Running npx playwright test:
Running 2 tests using 1 worker
ok 1 [chromium] › e2e\board.spec.ts:3:1 › the done filter lists only finished tasks (548ms)
x 2 [chromium] › e2e\board.spec.ts:12:1 › a new task joins the open list (3.6s)
Error: expect(locator).toBeVisible() failed
Locator: getByText('6 of 6 shown')
Expected: visible
Timeout: 3000ms
Error: element(s) not found
> 17 | await expect(page.getByText('6 of 6 shown')).toBeVisible()
1 failed
1 passed (9.1s)
(Shortened; the full output also prints the call log, the code around line 17 and the trace path.) The 3.6 seconds is a clue: almost all of it was waiting. Playwright looked for "6 of 6 shown" for 3000 ms and never found it. The error says what was missing, but not why.
The trace shows the page at that moment
Because of retain-on-failure, the run kept a trace, and the output prints the command to open it (npx playwright show-trace with the trace.zip path). The trace lists every action with its time:
248.5 ms Navigate to "/"
79.8 ms Click getByRole('button', { name: 'open' })
28.1 ms Fill "Pay rent" getByRole('textbox', { name: 'New task' })
63.6 ms Click getByRole('button', { name: 'add' })
3019.4 ms FAILED Expect "toBeVisible"
Next to the actions it shows the page as it looked at that moment. The failing run also wrote a page snapshot (trimmed to the relevant lines):
- button "open" [pressed] [ref=e6] [cursor=pointer]
- paragraph [ref=e9]: 4 of 6 shown
- list [ref=e10]:
- listitem [ref=e11]: â—‹ Email Jack
- listitem [ref=e12]: â—‹ Plan sprint
- listitem [ref=e13]: â—‹ Call Olivia
- listitem [ref=e14]: â—‹ Pay rent
The open button is pressed and the page says 4 of 6 shown. The filter was still on, so of course it never said 6 of 6. The test was wrong, not the app. Fix the expectation to "4 of 6 shown":
Running 2 tests using 1 worker
ok 1 [chromium] › e2e\board.spec.ts:3:1 › the done filter lists only finished tasks (539ms)
ok 2 [chromium] › e2e\board.spec.ts:12:1 › a new task joins the open list (497ms)
2 passed (5.5s)
It failed first and passed after, so we can trust it.
Keep the journeys few
Keep end-to-end tests for the paths that matter, and cover the rest with fast component and unit tests.
Recap
| Idea | What to remember |
|---|---|
| What runs | Playwright, a real Chromium and the app itself |
webServer | Starts npm run dev before the tests and waits for the URL |
| Locators | getByRole with a name, getByText |
| Waiting | Actions wait for visible, enabled and stable; expect retries until its timeout |
| Failures | Read the error, then the page snapshot and the trace |
| How many | A few journeys that matter |
Try it yourself: add a third journey that adds a task with the done filter on and checks it does not appear in the list. Run it, watch it fail once on purpose, then make it pass.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
One Request, Two Answers: the Same Post as an HTML Page and as JSON
Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.
A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.
How a Web Page Works: HTML Tags, Elements and Attributes
A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.
Semantic HTML: header, nav, main, aside and footer as Page Landmarks
The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.
