πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Questions
Question 33 of 100
Assertions
Intermediate

What are Web-First Assertions in Playwright?

The Answer

Web-First Assertions are Playwright's built-in `expect` methods that automatically wait and retry until the condition is met or the timeout expires. They are the correct way to assert UI state.

Deep Dive Explanation

The retry-until-pass mechanism eliminates race conditions. If a button becomes disabled 200ms after a click (while Playwright's assertion runs), toBeDisabled() will catch it rather than failing prematurely. This is the biggest quality-of-life improvement in modern Playwright vs Selenium.

example.spec.ts
// These all AUTO-RETRY until true (up to timeout)
await expect(page).toHaveTitle('Dashboard');
await expect(page).toHaveURL('/dashboard');
await expect(locator).toBeVisible();
await expect(locator).toBeHidden();
await expect(locator).toBeEnabled();
await expect(locator).toBeDisabled();
await expect(locator).toBeChecked();
await expect(locator).toHaveText('Welcome, John');
await expect(locator).toHaveValue('john@email.com');
await expect(locator).toHaveCount(5);
await expect(locator).toHaveAttribute('aria-expanded', 'true');
await expect(locator).toHaveClass(/active/);