Introduction
Playwright Test includes assertion methods optimized for the web with automatic retrying and detailed error messages.Basic Assertions
Fromsrc/matchers/expect.ts:206-243:
Web-First Assertions
Playwright provides auto-retrying assertions for web elements fromsrc/matchers/matchers.ts:
Visibility Assertions
Assert element is visibleFrom
src/matchers/matchers.ts:160-171:Assert element is hiddenFrom
src/matchers/matchers.ts:150-158:Attachment State
Assert element is attached to DOMFrom
src/matchers/matchers.ts:56-67:Element State Assertions
Assert element is enabledFrom
src/matchers/matchers.ts:127-138:Assert element is disabledFrom
src/matchers/matchers.ts:94-102:Assert element is editableFrom
src/matchers/matchers.ts:104-115:Assert element is emptyFrom
src/matchers/matchers.ts:117-125:Assert element is focusedFrom
src/matchers/matchers.ts:140-148:Assert checkbox is checkedFrom
src/matchers/matchers.ts:69-92:Assert element is in viewportFrom
src/matchers/matchers.ts:173-181:Text Content Assertions
Assert element has exact textFrom
src/matchers/matchers.ts:380-397:Assert element contains text substringFrom
src/matchers/matchers.ts:183-200:Attribute Assertions
Assert element has attributeFrom
src/matchers/matchers.ts:238-261:Assert element has CSS classFrom
src/matchers/matchers.ts:263-280:Assert element contains CSS classFrom
src/matchers/matchers.ts:282-303:Assert element has IDFrom
src/matchers/matchers.ts:342-352:Assert input has valueFrom
src/matchers/matchers.ts:399-409:Assert select has valuesFrom
src/matchers/matchers.ts:411-421:CSS Assertions
Assert element has CSS propertyFrom
src/matchers/matchers.ts:316-340:JavaScript Property Assertions
Assert element has JavaScript propertyFrom
src/matchers/matchers.ts:354-364:Count Assertion
Assert locator matches count of elementsFrom
src/matchers/matchers.ts:305-314:Accessibility Assertions
Assert element has accessible nameFrom
src/matchers/matchers.ts:214-224:Assert element has accessible descriptionFrom
src/matchers/matchers.ts:202-212:Assert element has accessible error messageFrom
src/matchers/matchers.ts:226-236:Assert element has ARIA roleFrom
src/matchers/matchers.ts:366-378:Page Assertions
Assert page has titleFrom
src/matchers/matchers.ts:423-433:Assert page has URLFrom
src/matchers/matchers.ts:435-454:API Response Assertions
Assert API response is successful (status 200-299)From
src/matchers/matchers.ts:456-481:Snapshot Assertions
Assert screenshot matches baseline
Assert value matches snapshot
Polling Assertions
Fromsrc/matchers/expect.ts:127-131,367-403:
Poll until condition is met
Retry assertion until it passesFrom
src/matchers/matchers.ts:483-517:Soft Assertions
Fromsrc/matchers/expect.ts:117-121:
Custom Assertions
Fromsrc/matchers/expect.ts:101-114:
Assertion Options
All web-first assertions support timeout:src/matchers/expect.ts:136-153,181-198:
Negation
All assertions can be negated with.not:
Custom Messages
Fromsrc/matchers/expect.ts:86-88,305-309:
Best Practices
- Use web-first assertions: They auto-retry and wait for conditions
- Prefer specific assertions: Use
toHaveTextinstead oftoBe - Use soft assertions for multiple checks: Continue test execution
- Set appropriate timeouts: Based on your application’s behavior
- Add custom messages: For better debugging
Next Steps
Test Fixtures
Learn about test fixtures
Test Hooks
Set up test environment with hooks
