Skip to main content
Playwright Test provides a rich API for writing end-to-end tests. This guide covers the fundamentals of writing tests, using locators, making assertions, and organizing your test suite.

Test structure

Playwright tests are written using the test function from @playwright/test. Each test receives a page object for browser interaction.

Locators: Finding elements

Playwright uses locators to find elements on the page. Locators are auto-waiting and retry-able, making tests more reliable.
The most resilient way to locate elements (recommended):
Prefer getByRole() over CSS or XPath selectors. Role-based selectors are more resilient to UI changes and improve accessibility.

Chaining and filtering locators

Combine locators to narrow down your selection:

Web-first assertions

Playwright assertions automatically retry until the condition is met or timeout is reached.

Common assertions

Custom timeout for assertions

Real-world test examples

Example 1: Form submission

Complete form interaction from the TodoMVC example:

Example 2: Editing with double-click

Example 3: API testing

Test REST APIs directly from the GitHub API example:

Example 4: Mocking browser APIs

Mock the Battery API from the mock-battery example:

Organizing tests

Test groups

Organize related tests with test.describe():

Test hooks

Set up and tear down test state:

Custom fixtures

Extend test context with reusable setup:

Configuration

Configure test behavior in playwright.config.ts:

Best practices

Always use Playwright’s expect() assertions instead of regular assertions. They automatically retry and wait for conditions to be met.
Don’t use setTimeout() or page.waitForTimeout(). Playwright’s auto-waiting handles timing automatically.
Test names should clearly describe what they verify:
Each test should be able to run in isolation. Don’t rely on the order of test execution.
Extract repeated setup code into custom fixtures to keep tests DRY:
Common mistakes to avoid:
  • Forgetting await before async operations
  • Using CSS selectors instead of semantic locators
  • Not using web-first assertions
  • Adding manual timeouts
  • Making tests dependent on each other

Next steps

Locators

Deep dive into locator strategies

Assertions

Master web-first assertions

Configuration

Configure advanced test options

Best practices

Learn testing best practices

Auto-waiting behavior

Playwright automatically waits for elements to be:
  • Attached to the DOM
  • Visible on the page
  • Stable (not animating)
  • Enabled and not disabled
  • Not covered by other elements
This eliminates the need for manual waits and reduces flaky tests.