Skip to main content

Introduction

Playwright Test includes assertion methods optimized for the web with automatic retrying and detailed error messages.

Basic Assertions

From src/matchers/expect.ts:206-243:

Web-First Assertions

Playwright provides auto-retrying assertions for web elements from src/matchers/matchers.ts:

Visibility Assertions

async
Assert element is visibleFrom src/matchers/matchers.ts:160-171:
async
Assert element is hiddenFrom src/matchers/matchers.ts:150-158:

Attachment State

async
Assert element is attached to DOMFrom src/matchers/matchers.ts:56-67:

Element State Assertions

async
Assert element is enabledFrom src/matchers/matchers.ts:127-138:
async
Assert element is disabledFrom src/matchers/matchers.ts:94-102:
async
Assert element is editableFrom src/matchers/matchers.ts:104-115:
async
Assert element is emptyFrom src/matchers/matchers.ts:117-125:
async
Assert element is focusedFrom src/matchers/matchers.ts:140-148:
async
Assert checkbox is checkedFrom src/matchers/matchers.ts:69-92:
async
Assert element is in viewportFrom src/matchers/matchers.ts:173-181:

Text Content Assertions

async
Assert element has exact textFrom src/matchers/matchers.ts:380-397:
async
Assert element contains text substringFrom src/matchers/matchers.ts:183-200:

Attribute Assertions

async
Assert element has attributeFrom src/matchers/matchers.ts:238-261:
async
Assert element has CSS classFrom src/matchers/matchers.ts:263-280:
async
Assert element contains CSS classFrom src/matchers/matchers.ts:282-303:
async
Assert element has IDFrom src/matchers/matchers.ts:342-352:
async
Assert input has valueFrom src/matchers/matchers.ts:399-409:
async
Assert select has valuesFrom src/matchers/matchers.ts:411-421:

CSS Assertions

async
Assert element has CSS propertyFrom src/matchers/matchers.ts:316-340:

JavaScript Property Assertions

async
Assert element has JavaScript propertyFrom src/matchers/matchers.ts:354-364:

Count Assertion

async
Assert locator matches count of elementsFrom src/matchers/matchers.ts:305-314:

Accessibility Assertions

async
Assert element has accessible nameFrom src/matchers/matchers.ts:214-224:
async
Assert element has accessible descriptionFrom src/matchers/matchers.ts:202-212:
async
Assert element has accessible error messageFrom src/matchers/matchers.ts:226-236:
async
Assert element has ARIA roleFrom src/matchers/matchers.ts:366-378:

Page Assertions

async
Assert page has titleFrom src/matchers/matchers.ts:423-433:
async
Assert page has URLFrom src/matchers/matchers.ts:435-454:

API Response Assertions

async
Assert API response is successful (status 200-299)From src/matchers/matchers.ts:456-481:

Snapshot Assertions

async
Assert screenshot matches baseline
sync
Assert value matches snapshot

Polling Assertions

From src/matchers/expect.ts:127-131,367-403:
async
Poll until condition is met
async
Retry assertion until it passesFrom src/matchers/matchers.ts:483-517:

Soft Assertions

From src/matchers/expect.ts:117-121:

Custom Assertions

From src/matchers/expect.ts:101-114:

Assertion Options

All web-first assertions support timeout:
From src/matchers/expect.ts:136-153,181-198:

Negation

All assertions can be negated with .not:

Custom Messages

From src/matchers/expect.ts:86-88,305-309:

Best Practices

  1. Use web-first assertions: They auto-retry and wait for conditions
  2. Prefer specific assertions: Use toHaveText instead of toBe
  3. Use soft assertions for multiple checks: Continue test execution
  4. Set appropriate timeouts: Based on your application’s behavior
  5. Add custom messages: For better debugging

Next Steps

Test Fixtures

Learn about test fixtures

Test Hooks

Set up test environment with hooks