Code Reference

Assertions

Playwright · Reference cheat sheet

Assertions

Playwright · Reference cheat sheet


📋 Overview

Playwright's expect auto-retries until the condition passes or timeout. Prefer web-first assertions (toBeVisible, toHaveURL) over manual polling.

🔧 Core concepts

AssertionChecks
toBeVisible / toBeHiddenVisibility
toBeEnabled / toBeDisabledInteractability
toHaveText / toContainTextText content
toHaveValueInput value
toHaveAttributeAttribute
toHaveCountLocator match count
toHaveURL / toHaveTitleNavigation
toHaveScreenshotVisual baseline

Soft assertions: expect.soft continues after failure within the test.

💡 Examples

Element state:

import { test, expect } from '@playwright/test';

await expect(page.getByRole('button', { name: 'Save' })).toBeEnabled();
await expect(page.getByText('Saved')).toBeVisible();
await expect(page.getByLabel('Name')).toHaveValue('Ada');
await expect(page.getByRole('listitem')).toHaveCount(3);

Page-level:

await expect(page).toHaveURL(/\/dashboard/);
await expect(page).toHaveTitle(/Dashboard/);
await expect(page.getByRole('alert')).toContainText('success');

Custom timeout / soft:

await expect(page.getByText('Ready')).toBeVisible({ timeout: 15_000 });
await expect.soft(page.getByTestId('a')).toBeVisible();
await expect.soft(page.getByTestId('b')).toBeVisible();
// test fails at end if any soft failed

API response:

const res = await request.get('/api/health');
expect(res.ok()).toBeTruthy();
expect(await res.json()).toMatchObject({ status: 'up' });

Negation:

await expect(page.getByText('Error')).not.toBeVisible();

⚠️ Pitfalls

  • Using Node assert or Jest expect without auto-wait.
  • Asserting immediately after click without waiting for navigation/network.
  • Overly strict full-string toHaveText when UI adds whitespace/icons.
  • Screenshot assertions without stable fonts/animations (see visual comparisons).
  • Swallowing failures with try/catch around expect.

On this page