Skip to content

Chapter 3 of 8

Assertions, Waits, and Avoiding Flakiness

Playwright's auto-wait engine waits for elements to be attached, visible, stable, and enabled before acting, and it waits for navigation when appropriate. That means most tests don't need explicit sleeps at all. When you do need to wait, prefer expect(locator) assertions such as toBeVisible, toHaveText, toHaveURL, toHaveValue, toHaveAttribute, toHaveCount, toBeChecked, toBeDisabled, and toBeFocused. These auto-retry up to a configurable timeout and produce much better error messages than arbitrary sleeps.

Some patterns that frequently cause flaky tests are worth avoiding. page.waitForTimeout() should be a last resort, reserved for genuinely non-deterministic animations or third-party widgets you can't otherwise signal. waitForLoadState('networkidle') is rarely reliable because polling apps never actually go idle; prefer waiting for an explicit signal such as a spinner becoming hidden or a specific API response. When the next click triggers navigation, race the click against waitForURL with Promise.all so the navigation event isn't lost. For conditions that don't map to a built-in matcher, expect.poll runs a function repeatedly until it passes.

Soft assertions let you collect multiple failures in a single run via expect.soft, which is useful for broad health checks where one failure shouldn't stop the rest of the report. Grouping steps with test.step('create report', async () => { ... }) produces a cleaner HTML report and makes failures easier to attribute. For dynamic UI like toasts or status banners, target the role='status' element and assert on a regex with toContainText, since exact text often varies by locale. test.setTimeout and test.describe.configure({ timeout }) let you extend limits for slow flows without making the whole suite slow.

All chapters
  1. 1Foundations: Installing and Configuring Playwright
  2. 2Selectors, Locators, and Resilient Tests
  3. 3Assertions, Waits, and Avoiding Flakiness
  4. 4Running, Tagging, and Debugging Tests
  5. 5Authentication, State, and Data Isolation
  6. 6Visual Regression and Snapshots
  7. 7CI/CD, Reports, and Parallel Execution
  8. 8Workspace Workflows and AI Agent Collaboration

Drill it

Reading is not remembering. These come from the Anki Playwright Agents deck:

Q

Playwright (Node): minimal setup?

npm i -D @playwright/testnpx playwright installnpx playwright test

Q

Where do Playwright tests live?

Common: ./tests or ./e2e (you choose). Configure testDir in playwright.config.ts so agents know where to add new tests.

Q

Key playwright.config.ts settings?

use.baseURL, webServer (start app), projects (chromium/firefox/webkit), retries/workers, reporter (html + junit), trace/video/screenshot policies.

Q

How do I auto-start my app for tests?

Use playwright.config.ts webServer: { command: "npm run dev", url: "http://127.0.0.1:3000", reuseExistingServer: !process.env.CI }.