Manuals / Playwright with Python / Chapter 13
Part 2 · Core Interactions · beginner · ~45 min · Chapter 13 of 61
7. Assertions with expect()
from playwright.sync_api import expect expect(page.get_by_role("button", name="Submit")).to_be_visible() expect(page.get_by_role("button", name="Submit")).to_be_enabled() expect(page.get_by_text("Order confirmed")).to_be_visible() expect(page.get_by_label("Email")).to_have_value("user@example.com") expect(page.locator(".error-message")).to_have_text("Invalid password") expect(page.locator(".cart-c
Step 1 of 4
Overview
The critical distinction from a plain Python assert: expect() is auto-retrying. It polls repeatedly for a few seconds (default 5s, configurable) instead of checking once and failing instantly, because a real element might take a moment to appear after a click triggers an API call. This is a classic source of flaky-test elimination. .to_be_checked() What it does: Auto-retrying assertion on element state. Types/params: Pointers: Retries repeatedly within the timeout window instead of checking once — eliminates most "not ready yet" flaky failures.
- timeout (number, ms, optional — overrides the default ~5000ms)
- Higher value → waits longer before failing, useful for known-slow elements
- Lower value → fails faster, useful for a quick negative check
Quick check
expect() differs from assert because it…
Chapter learning outcomes
- from playwright.sync_api import expect expect(page.get_by_role("button", name="S
Clear these before you leave
Side quest
7. Assertions with expect() deliverable
Apply one idea from “Overview” in a small script or note.