What are web-first assertions in Playwright?
Written by Jatin Thacker, Automation Architect · Updated
This answer is one piece of Chapter 11: Assertions.
- Web-First Assertions Cookbook
- Retry & Polling Patterns
- Visual Regression Testing
- Making a Failure Explain Itself
Modern web applications render asynchronously: elements appear, change attributes, and update text after API responses or CSS transitions finish. Traditional test runners evaluate assertions instantly on whatever is in the DOM right now, requiring manual sleep statements to avoid timing race conditions.
Playwright's web-first assertions, such as await expect(locator).toBeVisible() or await expect(locator).toHaveText('Success'), resolve this by continuously polling the target element until the expectation passes or the assertion timeout (default 5 seconds) is reached.
Because web-first assertions retry automatically, test suites remain fast and free of arbitrary sleep delays. If the condition is never met, Playwright provides a comprehensive failure log detailing every value observed across retries until timeout.
import { test, expect } from '@playwright/test';
test('web-first assertions wait and retry until condition holds', async ({ page }) => {
await page.goto('/catalog.html?mock=static&seed=42&autologin=1');
const cartLink = page.getByTestId('cart-link');
await expect(cartLink).toBeVisible();
await expect(cartLink).toHaveText(/Cart/);
});1 passed (2.0s)
await expect(locator).toBeVisible() handle elements that appear after an API response finishes?Practised in the full course: the drill The assertion that isn't one.
See the chapterRelated questions
AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.
163 lessons · 226 drills · start free