Assertions

Common expect matchers, ranked by how often you'll write them. Auto-retrying until pass or timeout.

PlaywrightPHP Assertions
playwright-php.dev

Element state

Call Note
expect($el)->toBeVisible() The workhorse: attached and painted
->toBeHidden() Gone or invisible, passes on both
->toBeEnabled() / ->toBeDisabled() Buttons and inputs
->toBeChecked() Checkboxes, radios, switches
->toBeFocused() Keyboard-flow checks
->toBeEmpty() No text, no child elements

Tip. Default timeout is 5s per assertion. Override once with ->withTimeout(10_000), not with sleeps.

Content

Call Note
->toContainText('Saved') Substring match
->toHaveText('3 items') Substring match in the testing helper
->toHaveExactText('3 items') Exact, whitespace included
->toHaveValue('42') Inputs and selects
->toHaveCount(3) List length, without the flake
->toHaveAttribute('href', '/docs') Any attribute
->toHaveClass('is-active') Complete class list, token order ignored
->toHaveCSS('display', 'grid') Computed style

Gotcha. Assert on locators, not extracted values: assertSame($el->textContent(), ...) reads once and never retries.

Page & control

Call Note
expect($page)->toHaveURL('https://app.test/done') Exact URL match
expect($page)->toHaveTitle('Checkout') Document title
->not()->toBeVisible() Negate any assertion
->withTimeout(10_000) Override the 5s default, in ms
->withPollInterval(250) How often it re-checks

Tip. One behavior per test, several expects per behavior. Every matcher retries on its own; nothing to wait for first.

Anatomy

expect($target)[->not()]->matcher($expected)

PHPUnit

use PlaywrightTestCaseTrait; // expect() wired into your suite

Deeper reads

PHP 8.2+ · MIT · playwright-php.dev/cheatsheets/assertions Not affiliated with Microsoft