End-to-End Testing in a Real Browser
Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.
What you will be able to do
#- Describe what an end-to-end test proves that lower layers cannot.
- Write a stable end-to-end test using role-based locators.
- State accurately which environments can run browser end-to-end tests.
Overview
#What only end-to-end can prove
An end-to-end test drives the real application in a real browser: real routing, real rendering, real network to a test backend, real storage. It proves the whole system works for a user, which no lower layer can prove because lower layers replace the real environment with doubles. If the wiring between frontend and backend is broken, only an end-to-end test sees it.
Environment capability, stated honestly
End-to-end browser tests need a real browser engine and a running application. A tool such as Playwright launches Chromium, Firefox or WebKit as separate processes and talks to them over a protocol. That requires a native runtime, so it runs on a developer machine or a CI runner, not inside an in-browser JavaScript sandbox. A browser-based playground cannot launch native Playwright, and a test that claims to do so is mislabeled. State the capability of each environment explicitly.
Locate like a user
Use role-based locators: getByRole('button', { name: 'Save' }). They match the accessibility tree, so they are stable across styling changes and they fail when an element becomes unreachable. CSS and XPath locators are brittle and prove nothing about accessibility.
Wait for the app, not for time
Never use a fixed sleep. Wait for a condition: the element appears, the URL changes, the network response arrives. Playwright auto-waits for actionability, so most explicit waits are unnecessary and a sleep only adds flakiness and latency.
Keep the suite small and critical
End-to-end tests are the slowest and most expensive layer. Cover the critical user journeys: sign in, create the primary entity, edit it, delete it, and the main error path. Push detailed coverage down to unit and integration tests. A large end-to-end suite becomes slow and flaky, which erodes the trust that makes it valuable.
Isolate test data
Each end-to-end run should create the data it needs and clean up, or run against a fresh environment. Shared mutable data makes tests order-dependent and flaky. Use unique identifiers per run so parallel runs do not collide.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Which statement about where a native Playwright end-to-end test can run is correct?
Exercise 2Explain
Explain why role-based locators are preferred over CSS locators in end-to-end tests.
Reveal the reference answer
reference.javascriptRole-based locators match the accessibility tree, which is how users and assistive technology find elements. They stay stable when class names or DOM structure change, and they fail when an element loses its accessible role or name, which is a real defect. CSS locators bind to implementation details, so a styling refactor breaks them for no product reason and they cannot detect an accessibility regression.The answer should connect locator choice to stability and to accessibility as a correctness signal.
Exercise 3Implement
Implement `waitFor(predicate, options)` that polls `predicate` until it returns true or a timeout elapses, without a fixed sleep. `await waitFor(() => ready); console.log('ready')` should print `ready` once `ready` becomes true.
starter.javascriptfunction waitFor(predicate, options) { // your code here }Reveal the reference answer
reference.javascriptfunction waitFor(predicate, options = {}) { const timeout = options.timeout ?? 1000; const interval = options.interval ?? 10; const start = Date.now(); return new Promise((resolve, reject) => { const check = () => { if (predicate()) return resolve(); if (Date.now() - start >= timeout) return reject(new Error('timeout')); setTimeout(check, interval); }; check(); }); }Authored expected output: ready
The helper polls on an interval and resolves as soon as the condition is true, rejecting on timeout. Polling a condition is the deterministic alternative to sleeping a fixed amount and hoping the app is ready.
Exercise 4Debug
This end-to-end test is flaky because it uses a fixed sleep. Replace the sleep with a condition wait.
starter.javascriptasync function test(page) { await page.click('text=Save'); await new Promise((r) => setTimeout(r, 2000)); const visible = await page.isVisible('.toast'); console.log(visible); }Reveal the reference answer
reference.javascriptasync function test(page) { await page.getByRole('button', { name: 'Save' }).click(); await page.getByRole('status').waitFor({ state: 'visible' }); const visible = await page.getByRole('status').isVisible(); console.log(visible); }Authored expected output: true
A two-second sleep is either too short on a slow machine or wasted time on a fast one. Waiting for the status element to become visible resolves as soon as the app is ready and fails clearly if it never appears. The locators also move from CSS to roles.
Exercise 5Test
Without a browser, write a test that proves `waitFor` resolves when the condition becomes true and rejects on timeout.
starter.javascriptfunction waitFor(predicate, options = {}) { const timeout = options.timeout ?? 1000; const interval = options.interval ?? 10; const start = Date.now(); return new Promise((resolve, reject) => { const check = () => { if (predicate()) return resolve(); if (Date.now() - start >= timeout) return reject(new Error('timeout')); setTimeout(check, interval); }; check(); }); }Reveal the reference answer
reference.javascriptfunction assertEqual(actual, expected, label) { if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual); } async function test() { let ready = false; setTimeout(() => { ready = true; }, 20); await waitFor(() => ready, { timeout: 500, interval: 5 }); assertEqual(ready, true, 'resolved after condition'); let rejected = false; try { await waitFor(() => false, { timeout: 30, interval: 5 }); } catch (error) { rejected = error.message === 'timeout'; } assertEqual(rejected, true, 'rejects on timeout'); console.log('waitFor verified'); } test();Authored expected output: waitFor verified
This tests the waiting primitive itself in Node, without a browser, which is honest about capability. Both the resolve and reject paths are covered, so the helper used by browser tests is trustworthy.
Exercise 6Interview
In an interview, explain how many end-to-end tests you would write and why.
Reveal the reference answer
reference.javascriptI write a small number covering the critical user journeys: authentication, the primary create-read-update-delete flow, and the main failure path such as a rejected submission. End-to-end tests are slow and flaky, so a large suite erodes trust and slows delivery. I push detail down to integration and unit tests and keep end-to-end for the journeys where a break would stop users entirely.The answer should justify a small suite by cost and flakiness and delegate detail to lower layers.
Common misconceptions
#- An in-browser sandbox can launch native Playwright.
- End-to-end tests should cover every branch of the application.
- A fixed sleep is a reliable way to wait for the app.
Follow-up practice
#- testing-contracts
- testing-capstone-quality-task-board
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.