PLAYWRIGHT UIINTERMEDIATE

Playwright Assertions Guide: Web-First expect() in Practice

Everything you can assert with Playwright's built-in expect — visibility, text, counts, screenshots, and custom matchers.

iff Solution Academy July 7, 2026 9 min read Updated July 7, 2026
Playwright Assertions expect

What Are Web-First Assertions?

Any expect() called with a Locator or Page auto-retries until it passes or the timeout expires — no manual polling.

Visibility & Presence

ts
await expect(page.getByRole('dialog')).toBeVisible();
await expect(page.getByText('Loading...')).toBeHidden();

Text & Content

ts
await expect(page.locator('h1')).toHaveText('Dashboard');
await expect(page.locator('h1')).toContainText('board');
await expect(page.locator('input')).toHaveValue('admin');

State & Attributes

ts
await expect(page.getByRole('button')).toBeEnabled();
await expect(page.getByRole('checkbox')).toBeChecked();
await expect(page.locator('a')).toHaveAttribute('href', '/home');

Counting Elements

ts
await expect(page.getByRole('row')).toHaveCount(10);

Visual Snapshots

ts
await expect(page).toHaveScreenshot('dashboard.png');

Soft Assertions

Use expect.soft() to keep executing after a failure — the test still fails at the end.

ts
await expect.soft(page.locator('h1')).toHaveText('Wrong');
await page.getByRole('button').click(); // still runs

Get Playwright tutorials in your inbox

Weekly tips, real-world examples, and framework patterns – no spam, unsubscribe anytime.

Recommended Next Articles