Getting Started
Relevant source files
The following files were used as context for generating this wiki page:
Playwright is a framework for Web Testing and Automation, enabling cross-browser testing for Chromium, Firefox, and WebKit with a single API. It is designed to provide ever-green, capable, reliable, and fast web automation. This guide provides the essential steps to install Playwright, configure your project, write your first test, and leverage its powerful tooling.
Playwright Test is the dedicated test runner for writing end-to-end tests. It provides full test isolation by creating a new browser context for each test, which is equivalent to a brand new browser profile. This ensures that tests are independent and do not interfere with each other, while only taking milliseconds to create.
Sources: README.md:7-9, README.md:65-68
Installation
Getting started with Playwright can be done either automatically using the init command or by manually setting up the project. The recommended approach is using the init command as it scaffolds a complete project structure.
Sources: README.md:28-30
Using the Init Command
The easiest way to begin is by running the init command from your project's root directory. This command sets up a configuration file, adds example tests, and a GitHub Actions workflow.
# Run from your project's root directory
npm init playwright@latest
# Or to create a new project
npm init playwright@latest new-project
This process automates the initial setup, allowing you to start writing tests immediately.
Sources: README.md:33-37
Manual Installation
For a manual setup, you need to add @playwright/test as a dev dependency and then install the required browsers.
- Add the dependency:
npm i -D @playwright/test - Install browsers:
npx playwright install
You can also install specific browsers. For more details, refer to the install browsers documentation.
Sources: README.md:41-44
The installation process can be visualized as follows:
This diagram shows the two primary paths for setting up a Playwright project.
Sources: README.md:33-44
Configuration
Playwright tests are configured using a playwright.config.ts (or .js) file. This file allows you to define projects, specify browser settings, configure reporters, and set various test options.
Sources: packages/playwright/src/common/configLoader.ts
Project Configuration
You can define multiple projects to run your tests in different configurations. For example, you can set up projects for different browsers or devices.
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
projects: [
{
name: 'chromium',
use: devices['Desktop Chrome'],
},
{
name: 'firefox',
use: devices['Desktop Firefox'],
},
],
});
Sources: packages/playwright/types/test.d.ts:79-99
Common Configuration Options
The configuration file supports numerous options to customize your test runs.
| Option | Description | Example | Source |
|---|---|---|---|
testDir | Directory to scan for test files. | './tests' | packages/playwright/types/test.d.ts:400-403 |
reporter | Specifies the reporter(s) to use. | [['html', { outputFolder: 'my-report' }]] | tests/playwright-test/reporter-html.spec.ts:1143-1145 |
use | An object with options to apply to all tests in the project. | { browserName: 'chromium', headless: false } | packages/playwright/types/test.d.ts:102-104 |
retries | The number of times to retry failed tests. | 1 | tests/playwright-test/reporter-blob.spec.ts:25-28 |
timeout | Timeout for each test in milliseconds. | 30000 | packages/playwright/types/test.d.ts:421-423 |
fullyParallel | Run all tests in all files in parallel. | true | packages/playwright/types/test.d.ts:290-292 |
This table summarizes some of the most common configuration settings available in playwright.config.ts.
Writing Tests
Tests are written using the test and expect functions from @playwright/test. The test function takes a title and an async function, which receives fixtures like page.
Basic Test Structure
Here is a simple example of a test that navigates to a page and takes a screenshot.
import { test } from '@playwright/test';
test('Page Screenshot', async ({ page }) => {
await page.goto('https://playwright.dev/');
await page.screenshot({ path: `example.png` });
});
This test uses the page fixture to interact with a web page.
Sources: README.md:92-98
Test Execution Flow
The following diagram illustrates the sequence of a simple test execution.
This diagram shows how the test runner orchestrates the test function, which in turn uses the page fixture to control the browser.
Advanced Examples
Playwright supports more complex scenarios like mobile emulation, geolocation, and network interception.
Mobile and Geolocation
import { test, devices } from '@playwright/test';
test.use({
...devices['iPhone 13 Pro'],
locale: 'en-US',
geolocation: { longitude: 12.492507, latitude: 41.889938 },
permissions: ['geolocation'],
})
test('Mobile and geolocation', async ({ page }) => {
await page.goto('https://maps.google.com');
await page.getByText('Your location').click();
await page.waitForRequest(/.*preview\/pwa/);
await page.screenshot({ path: 'colosseum-iphone.png' });
});
Sources: README.md:103-118
Network Interception
import { test } from '@playwright/test';
test('Intercept network requests', async ({ page }) => {
// Log and continue all network requests
await page.route('**', route => {
console.log(route.request().url());
route.continue();
});
await page.goto('http://todomvc.com');
});
Sources: README.md:133-142
Running Tests
To run your tests, use the playwright test command.
npx playwright test
You can also run specific tests or use different configurations:
npx playwright test my-test.spec.ts- Run a single file.npx playwright test -g "my-test"- Run tests matching a grep pattern.npx playwright test --project=chromium- Run tests for a specific project.
Sources: CONTRIBUTING.md:68-76, packages/playwright/types/test.d.ts:219-225
Tooling
Playwright comes with a suite of tools to aid in test development and debugging.
| Tool | Description |
|---|---|
| Codegen | Generate tests by recording your actions and save them into any language. |
| Playwright Inspector | Inspect the page, generate selectors, step through test execution, and see click points. |
| Trace Viewer | Capture a detailed trace of test execution, including a screencast, live DOM snapshots, and action explorer. |
Sources: README.md:71-79
UI Mode
For a more interactive experience, you can use UI Mode, which provides a live, hot-reloaded interface for developing and debugging tests.
npx playwright test --ui
The UI mode is served from an HTML file that loads the necessary React components.
Sources: packages/trace-viewer/uiMode.html:1-16, CONTRIBUTING.md:27-28
Contributing
If you wish to contribute to Playwright, you'll need to set up the development environment.
Development Setup
- Prerequisites: Ensure you have Node.js 20 or later.
- Clone & Install:
git clone https://github.com/microsoft/playwright cd playwright npm ci - Build & Watch:
npm run watch - Install Browsers for Testing:
npx playwright install
This setup enables a watch mode that automatically rebuilds the project on file changes.
Sources: CONTRIBUTING.md:17-25
Running Local Tests
Playwright has multiple test suites. The two most important ones to run locally are:
- Library tests:
npm run ctest(fast, Chromium only) ornpm run test(all browsers). - Test runner tests:
npm run ttest.
This workflow helps ensure that your changes are valid and do not break existing functionality.
This diagram outlines the typical process for contributing code to the Playwright repository.
Sources: CONTRIBUTING.md:19-84