Command-Line Interface (CLI)
Relevant source files
The following files were used as context for generating this wiki page:
- packages/playwright/cli.js
- packages/playwright/src/program.ts
- packages/playwright-core/src/cli/program.ts
- packages/playwright/package.json
- packages/playwright-core/package.json
- tests/installation/playwright-cli.spec.ts
- tests/playwright-test/reporter-blob.spec.ts
- packages/playwright/types/test.d.ts
- tests/playwright-test/test-server.spec.ts
- tests/playwright-test/only-changed.spec.ts
The Playwright Command-Line Interface (CLI) is a powerful tool for managing and interacting with the Playwright framework. It provides a suite of commands for essential development tasks, including installing browser binaries, running tests, generating code, and viewing reports. The CLI is the primary entry point for developers to automate and debug their web applications with Playwright.
The CLI's functionality is distributed across two main packages: playwright-core and playwright. The playwright-core package provides fundamental commands for browser management and basic tooling, while the playwright package extends this with a full-featured test runner and its associated commands. This separation allows for a lightweight core installation with the option to add the comprehensive testing capabilities of @playwright/test.
Architecture and Entry Point
The playwright command is made available in the user's environment through the bin field in packages/playwright/package.json. This points to cli.js, which serves as the main entry point for the CLI.
Sources: packages/playwright/package.json:49-51
The cli.js script's primary responsibility is to dispatch the command to the appropriate handler. It determines whether the command is related to the test runner (like test, show-report) or a core utility (like install, codegen). If a test-related command is detected, it forwards the execution to the test runner's CLI program; otherwise, it falls back to the playwright-core CLI.
This diagram illustrates the dispatch flow from the user command to the responsible program.
Sources: packages/playwright/cli.js
Core Commands (playwright-core)
The playwright-core package provides the foundational CLI commands. These are available even with a minimal playwright-core installation and are managed in packages/playwright-core/src/cli/program.ts.
| Command | Description |
|---|---|
install | Installs browser binaries (e.g., chromium, firefox, webkit) and their dependencies. |
install-deps | Installs OS-level dependencies for the browsers. |
uninstall | Removes installed browser binaries. |
screenshot | Captures a screenshot of a web page. |
pdf | Saves a web page as a PDF. |
codegen | Launches the Code Generator to record user actions and generate scripts. |
open | Opens a page in a browser. |
cr / ff / wk | Shorthands to launch Chromium, Firefox, and WebKit respectively. |
run-server | Starts a Playwright server instance. |
Sources: packages/playwright-core/src/cli/program.ts
Code Generation (codegen)
The codegen command is a powerful tool for getting started with Playwright. It records interactions with a web page and generates corresponding Playwright script code. It supports multiple target languages.
Example usage from tests:
# Generate a Playwright Test script (default)
npx playwright codegen --output=test.spec.js
# Generate a plain JavaScript library script
npx playwright codegen --target=javascript --output=script.js
# Generate a Python script
npx playwright codegen --target=python --output=script.py
Sources: tests/installation/playwright-cli.spec.ts:21-61
Test Runner Commands (@playwright/test)
The playwright package, which includes @playwright/test, adds a rich set of commands for test execution and reporting. These are defined in packages/playwright/src/program.ts.
| Command | Description |
|---|---|
test | Runs Playwright tests. |
show-report | Shows the last HTML report. |
show-trace | Shows a saved trace file in the Trace Viewer. |
merge-reports | Merges multiple blob reports from sharded runs into a single report. |
test-server | Starts a server for IDE integration to list and run tests. |
Sources: packages/playwright/src/program.ts
Merging Blob Reports (merge-reports)
When running tests in a sharded environment, each shard produces a "blob" report. The merge-reports command is used to combine these individual blob reports into a single, comprehensive report. This is essential for getting a unified view of a distributed test run.
The process typically involves:
- Running tests across multiple shards with the
blobreporter enabled. - Collecting all the generated
.zipblob reports into a single directory. - Running
npx playwright merge-reports <blob-report-dir>to produce a final report (e.g., HTML).
This diagram shows the flow for merging sharded reports.
This workflow is extensively tested in reporter-blob.spec.ts, which serves as a reference for its functionality.
Sources: tests/playwright-test/reporter-blob.spec.ts:193-211, tests/playwright-test/reporter-blob.spec.ts:88-100
Viewing Reports and Traces
Playwright provides dedicated commands to easily access debugging and reporting artifacts.
show-report: Launches a local web server to serve the HTML report. It looks for the report in theplaywright-reportdirectory by default. If the report is not found, it exits with an error.show-trace: Opens the Playwright Trace Viewer for a given trace file (.zip). This is crucial for post-mortem debugging of test failures.
Sources: tests/installation/playwright-cli.spec.ts:84-91
Test Server (test-server)
The test-server command starts a WebSocket server that allows IDEs and other tools to interact with the Playwright test runner. This enables features like listing tests in a project, running specific tests, and receiving live results without repeatedly invoking the playwright test command. The connection is managed by the TestServerConnection class.
Sources: tests/playwright-test/test-server.spec.ts:39-50
Filtering Changed Tests (--only-changed)
The --only-changed flag for the test command provides an efficient way to run only the tests that are affected by local file changes compared to a git base commit. It analyzes the dependency graph to determine which test files to run. This is particularly useful for pre-commit checks or local development loops.
This feature is not compatible with --watch or --ui modes. It also supports running project dependencies for the changed tests.
Sources: tests/playwright-test/only-changed.spec.ts:100-123, tests/playwright-test/only-changed.spec.ts:213-236
CLI Options for Reporters
The CLI supports various reporters that can be configured with the --reporter flag. Some reporters accept options to customize their behavior. These options are defined as types in the type definition files.
| Reporter | Options |
|---|---|
blob | outputDir, fileName |
list | printSteps |
junit | outputFile, stripANSIControlSequences, includeProjectInTestName |
json | outputFile |
html | outputFolder, open, host, port, attachmentsBaseURL, title, noSnippets |
Sources: packages/playwright/types/test.d.ts:11-17
Conclusion
The Playwright CLI is a comprehensive and well-structured tool that serves as the backbone for most development and CI workflows. It effectively separates core functionalities from the advanced test runner, providing both a lightweight utility and a powerful testing framework entry point. Commands like codegen, merge-reports, and test-server highlight its role in simplifying test creation, managing complex test runs, and integrating with the broader developer ecosystem.