Home
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- CONTRIBUTING.md
- packages/playwright/types/test.d.ts
- tests/playwright-test/reporter-html.spec.ts
- tests-playwright-test/reporter-blob.spec.ts
- tests/library/selector-generator.spec.ts
- tests/page/page-aria-snapshot.spec.ts
- packages/playwright-ct-core/src/vitePlugin.ts
- tests/library/browsercontext-har.spec.ts
Playwright is a framework for Web Testing and Automation, designed to enable cross-browser web automation that is ever-green, capable, reliable, and fast. It provides a single API to test modern web applications across Chromium, Firefox, and WebKit. The framework is built with a focus on eliminating flaky tests through features like auto-waits, web-first assertions, and detailed tracing capabilities.
The core of the testing experience is the Playwright Test runner, which is specifically designed for end-to-end tests. It provides full test isolation through browser contexts, powerful tooling like Codegen and Trace Viewer, and robust configuration options to handle complex testing scenarios. This document provides a high-level overview of Playwright's architecture, features, and usage patterns, derived from its source code and documentation.
Sources: README.md:1-10
Core Capabilities
Playwright's architecture is designed to provide a robust and efficient testing experience. Its capabilities are built on a few key principles: resilience, isolation, and powerful tooling.
This diagram illustrates the relationship between Playwright's core API, its guiding principles, and its developer tooling.
Sources: README.md:30-52
Browser Support
Playwright supports the latest versions of major rendering engines.
| Browser | Linux | macOS | Windows |
|---|---|---|---|
| Chromium | ✅ | ✅ | ✅ |
| WebKit | ✅ | ✅ | ✅ |
| Firefox | ✅ | ✅ | ✅ |
Sources: README.md:12-18
Getting Started
Getting started with Playwright involves initializing a project, which sets up the necessary configuration and example files.
Installation
The recommended way to start a new Playwright project is by using the init command.
# Run from your project's root directory
npm init playwright@latest
This command creates a playwright.config.ts file, adds example tests, and can set up a GitHub Actions workflow. Alternatively, you can install it manually.
npm i -D @playwright/test
npx playwright install
Sources: README.md:61-73
Development Setup
For contributing to Playwright itself, the setup involves cloning the repository and installing dependencies using npm ci. The npm run watch command starts the build in watch mode, which is essential as some files, like API types, are generated from source documentation.
This flowchart outlines the basic development workflow for contributors.
Sources: CONTRIBUTING.md:16-29
Writing Tests
Tests are written using the test and expect functions provided by the @playwright/test package. The test runner provides fixtures like page and context to interact with the browser.
Basic Test Structure
A test file consists of one or more test blocks, each representing a single test case.
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev/');
const name = await page.innerText('.navbar__inner .navbar__title');
expect(name).toBe('Playwright');
});
Sources: packages/playwright/types/test.d.ts:2519-2527
Test Runner API
The test runner's API is extensive, allowing for fine-grained control over test execution.
| API | Description |
|---|---|
test(title, body) | Declares a test. |
test.describe(title, body) | Declares a group of tests. |
test.beforeEach(hook) | Runs a hook before each test in its scope. |
test.afterEach(hook) | Runs a hook after each test in its scope. |
test.beforeAll(hook) | Runs a hook once before all tests in a worker. |
test.afterAll(hook) | Runs a hook once after all tests in a worker. |
test.skip() | Skips a test. |
test.fail() | Marks a test as expected to fail. |
test.use(options) | Specifies options or fixtures for a scope. |
test.step(title, body) | Declares a test step for reporting. |
Sources: packages/playwright/types/test.d.ts:2511-3093
Assertions
Playwright includes a rich set of "web-first" assertions that automatically wait for conditions to be met.
Snapshot Testing
Playwright supports visual and data snapshot testing to track changes in UI or serialized data.
toMatchSnapshot(): Compares a text or binary buffer with a stored snapshot. This is useful for any serializable data.toHaveScreenshot(): Compares a locator or page screenshot with a stored image. It automatically waits for the element to be stable before taking a screenshot.toMatchAriaSnapshot(): Compares the accessibility tree of a locator with a stored snapshot, ensuring that the accessible structure of the UI remains consistent.
The following diagram shows the flow of a snapshot assertion.
Selector Generation
Playwright's selector generation engine, used by tools like Codegen, creates human-readable and resilient selectors. It prioritizes attributes in a specific order to find the best possible selector.
Selector Priority Order:
- Role-based selectors with accessible name (e.g.,
internal:role=button[name="Submit"i]). data-testidattributes.- Text content (
internal:text="Some text"). - Readable
idattributes. placeholderattributes for inputs.- Other attributes like
nameortype. - Structural selectors (
nth-child,>>) as a fallback.
The generator is smart enough to handle shadow DOM, prefer parent elements like <button> over inner <span> elements, and normalize whitespace in text content.
Sources: tests/library/selector-generator.spec.ts:24-38, 102-105, 120-123, 218-221
Test Configuration
Playwright tests are configured via a playwright.config.ts (or .js) file. This file allows for setting up global options, defining projects for cross-browser testing, and configuring reporters.
Configuration Hierarchy
The configuration is hierarchical, allowing global settings to be overridden at the project level.
This diagram shows how a global configuration can define multiple projects, each with its own specific settings that inherit from the global scope.
Sources: packages/playwright/types/test.d.ts:100-119, 933-942
Key Configuration Options
| Option | Description | Type |
|---|---|---|
testDir | Directory to scan for test files. | string |
timeout | Timeout for each test in milliseconds. | number |
retries | The number of retry attempts for failed tests. | number |
use | Default options for browser, context, and page fixtures. | object |
projects | An array of test projects for different configurations. | TestProject[] |
reporter | The reporter(s) to use for test results. | string | ReporterDescription[] |
fullyParallel | Run all tests in all files in parallel. | boolean |
forbidOnly | Fail the run if test.only is found. | boolean |
webServer | Launch a dev server before tests. | object | object[] |
Sources: packages/playwright/types/test.d.ts:928-1594
Advanced Features & Tooling
Test Reporting
Playwright includes several built-in reporters to display test results.
| Reporter | Description |
|---|---|
html | Generates a self-contained HTML report with detailed test results, traces, and attachments. |
list | Prints a detailed list of tests and their status to the console. |
dot | Prints a dot for each test to the console. |
json | Outputs a JSON file with the full test results. |
junit | Outputs a JUnit XML file, compatible with many CI systems. |
blob | Outputs a zip file containing all test results, used for merging sharded reports. |
markdown | Outputs a summary in a Markdown file. |
Sources: packages/playwright/types/test.d.ts:26-37, tests/playwright-test/reporter-markdown.spec.ts:14
Blob Reporter and Merging
The blob reporter is a key feature for running tests at scale. It outputs test results from a single shard into a zip file. These individual report files can then be merged into a single, comprehensive report using the playwright merge-reports command. This is particularly useful in distributed CI environments where tests are run in parallel across multiple machines.
This flowchart illustrates the process of running sharded tests and merging the resulting blob reports.
Sources: tests/playwright-test/reporter-blob.spec.ts:60-72, 218-226
HTML Reporter
The HTML reporter provides an interactive web-based view of test results. It allows you to:
- Filter tests by status (passed, failed, flaky, skipped).
- View detailed error messages and stack traces.
- Inspect attachments, such as screenshots and videos.
- Launch the Trace Viewer directly from a test result.
- View
stdoutandstderrfor each test. - Examine git commit metadata associated with the test run.
Sources: tests/playwright-test/reporter-html.spec.ts:40-57, 203-211, 804-811
Component Testing with Vite
Playwright supports component testing and provides a Vite plugin (@playwright/experimental-ct-core) to facilitate this. The plugin handles the transformation of test files and component imports, creating a bundle that can be served for in-browser component testing. It intelligently manages a build cache to avoid rebuilding if component sources have not changed.
Sources: packages/playwright-ct-core/src/vitePlugin.ts:80-92, 160-168
Network Mocking with HAR
Playwright can use HTTP Archive (HAR) files to mock network requests, which is useful for creating fast and reliable tests that are independent of backend services.
context.routeFromHAR(path): Routes all network requests for a browser context based on the entries in a.harfile.page.routeFromHAR(path): Routes network requests for a single page.- Options:
notFound: Determines the behavior for requests not found in the HAR file. Can be'abort'(default) or'fallback'(let the request pass through).url: A glob or regex to filter which requests are handled by the HAR router.update: Iftrue, new network requests are recorded and added to the HAR file.
Sources: tests/library/browsercontext-har.spec.ts:21-38, 51-56, 301-309
Contributing
Contributions to Playwright are welcome and follow a standard GitHub workflow. Every contribution requires an associated issue.
This flowchart shows the end-to-end process for contributing to the Playwright project.