Best practices and anti-patterns
One place to read the habits that keep Playwright tests steady, and the habits that make them break. Each habit has a reason, a Daily List example and a fix.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: the habits of a careful driver. A careful driver checks the mirror and does not guess how long a light will stay green. A crash is rarely one big mistake. It is usually a small habit, repeated until the day it matters.
Tom has many Playwright tests for Daily List, a to-do list app. Some are steady. Others fail now and then, and nobody knows why. Mei asks for one list of the good habits and the bad ones.
What it is: Playwright is a free program, made by Microsoft, that drives real browsers from test code. A is a habit that looks sensible and then makes the suite slow, fragile or misleading. A is a pause for a set time, whatever the page is doing. A floating promise is a call whose result is thrown away, often because await is missing. A locator is how a test points at part of the page. A flaky test passes and fails on the same code. A Playwright fixture is a ready-made helper the runner hands the test, such as a fresh page. A good habit is often called a best practice. The neighbour of a flaky test is a real bug: one changes with the clock, the other stays broken.
Why it matters: a suite full of sleeps and shared tasks fails at random. The team then stops trusting every . Naming the habit gives Mei a short phrase in review, such as "that is a fixed wait".
Test habits still depend on the app. Each one needs a reason, or it becomes a slogan.
Kept simple: this lesson uses the habits the Playwright docs state in plain words. It is not a full copy of the best-practices page.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named here are invented.)
PLAIN WORDS · 3 in this section
- playwright
- a free Microsoft tool that drives a browser for tests
- locator
- a pointer telling you where to find something
- flaky
- passing sometimes and failing sometimes with no product change
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Spotting the small habits that make tests slow or flaky, and replacing each with a steadier habit.
- Find the habit
- Say why it hurts
- Apply the fix
- Measure again
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| Checking the mirror | Using a web-first check that looks again |
| Keeping a safe gap | Keeping each test on its own, with a clean state |
| Guessing how long a light stays green | A fixed wait that guesses how long the page needs |
| A small habit repeated until the day it matters | An anti-pattern that causes a flaky test |
WHERE THIS PICTURE STOPS
- Driving rules are the same on every road. Test habits depend on the app and the team.
- A crash is clear to everyone. A flaky test can hide for weeks.
- A driver has one road to watch. A suite has many tests, so habits need tools to check them.
HOOK
Look, don't guess.
REMEMBER IT THIS WAY
A contrast: two neighbours told apart by one test.
A guesses how long the page needs. A web-first check looks again until the page is ready.
| In the trick | What it stands for |
|---|---|
| Fixed wait | guesses how long the page needs |
| Web-first check | looks again |
TELL A FRIEND
A careful driver checks the mirror and does not guess how long a light stays green.
OFTEN MIXED UP WITH Keeping test code clean: linting, formatting, hooks and review. The clean-code lesson shows the tools that catch these habits. This lesson names the habits.
Sources & standards — 4 sources
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Test what the user sees, keep tests isolated, prefer role and text locators, use web-first assertions that retry, use TypeScript and ESLint to catch errors early, and run tsc --noEmit in the pipeline (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).
- Market practice
Auto-waiting (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Isolation (Playwright documentation) ↗ — Microsoft (Playwright) · Each test gets its own browser context, a clean-slate environment that is fast to create and fully isolated (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
eslint-plugin-playwright (npm package page) ↗ — npm registry page for a community package · The package adds Playwright rules to ESLint; the no-wait-for-timeout rule flags page.waitForTimeout and is in the recommended set (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number; read at the time of writing. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
PRACTISE AND KEEP
Sources for this topic — 4 sources
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Test what the user sees, keep tests isolated, prefer role and text locators, use web-first assertions that retry, use TypeScript and ESLint to catch errors early, and run tsc --noEmit in the pipeline (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).
- Market practice
Auto-waiting (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Isolation (Playwright documentation) ↗ — Microsoft (Playwright) · Each test gets its own browser context, a clean-slate environment that is fast to create and fully isolated (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
eslint-plugin-playwright (npm package page) ↗ — npm registry page for a community package · The package adds Playwright rules to ESLint; the no-wait-for-timeout rule flags page.waitForTimeout and is in the recommended set (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number; read at the time of writing. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.