Playwright CLI Explained

Run, debug, record, and trace Playwright tests using CLI commands. Validate CLI-driven tests on real browsers with BrowserStack.

Get Started free
Playwright CLI
Home Guide Playwright CLI

Playwright CLI

Playwright is often described as a testing framework, but in practice most teams interact with it through the command line. The Playwright CLI is the control surface that handles everything from installing browsers to running tests in CI.

Understanding the CLI is essential because nearly every workflow-local development, debugging, recording tests, or scaling execution-depends on it.

Overview

Best Practices for Playwright CLI Usage

  • Using CLI flags for experimentation, not permanent behavior
  • Keeping configuration in version-controlled files
  • Running the same CLI commands locally and in CI
  • Refactoring recorded tests before scaling
  • Reviewing generated artifacts for flaky patterns

This article explains what the Playwright CLI is, how it works, the most important commands, common pitfalls, and how it fits into production-grade automation.

What is Playwright CLI?

Playwright CLI is the command-line interface that ships with Playwright and provides a standardized way to install, configure, run, debug, and generate Playwright tests. Instead of writing custom scripts or relying on IDE plugins, the CLI exposes Playwright’s capabilities through consistent terminal commands.

The CLI is responsible for:

  • Installing Playwright browser binaries and dependencies
  • Launching test runs across supported browsers
  • Generating test code through recording
  • Enabling debugging, tracing, and artifact collection
  • Controlling execution behavior in CI environments

In most projects, the CLI is the primary entry point to Playwright.

Why Use Playwright CLI for Test Automation?

The Playwright CLI exists to make test execution predictable and repeatable across environments. Without it, teams often end up with fragmented scripts that behave differently on each machine.

Using the CLI provides:

  • Consistency across machines, ensuring the same commands work locally and in CI
  • Built-in best practices, such as auto-waits, retries, and parallel execution
  • Lower setup overhead, since browser installation and configuration are automated
  • Tooling parity, where recording, debugging, and execution use the same interface

This consistency is critical when tests must scale beyond a single developer’s laptop.

How Playwright CLI Works?

Playwright CLI works as an orchestration layer that connects test code, configuration, browsers, and execution logic into a single, repeatable workflow. Instead of directly running browser automation logic, the CLI coordinates how and where tests run, what resources they use, and which artifacts they produce.

  • Reads and resolves Playwright configuration: When a CLI command is executed, Playwright first loads the configuration file (playwright.config.ts or equivalent). This defines browsers, devices, timeouts, retries, reporters, parallel workers, and environment-specific settings. CLI flags can override these values at runtime, but the config remains the source of truth.
  • Discovers and groups test files: The CLI scans the project based on configured test directories and file patterns. Tests are grouped into projects and workers, allowing Playwright to decide which tests can run in parallel and which must run serially.
  • Launches browser instances through Playwright APIs: Rather than relying on system browsers, the CLI launches Playwright-managed browser binaries. Each test gets an isolated browser context, ensuring no shared cookies, storage, or state unless explicitly configured.
  • Manages parallel execution and retries: The CLI schedules tests across multiple workers to optimize execution time. If retries are enabled, failed tests are automatically re-queued based on retry rules without manual intervention.
  • Handles automatic waits and synchronization: While tests run, Playwright enforces built-in auto-waiting for elements, navigation, and network activity. The CLI ensures this behavior is consistently applied across all tests without requiring custom wait logic.
  • Collects execution artifacts: During execution, the CLI controls artifact generation such as screenshots, videos, and traces. These artifacts are attached to failed or retried tests based on configuration and CLI flags.
  • Produces structured test reports: After execution completes, the CLI generates reports in the configured format, such as HTML, JSON, or JUnit. These reports are designed to integrate directly into CI systems.

Example execution flow:
Running the following command:

npx playwright test –project=chromium –trace=on
Triggers the CLI to:

  • Load the Playwright configuration
  • Select only the Chromium project
  • Start parallel workers
  • Launch Chromium browsers
  • Execute tests with tracing enabled
  • Save trace files for later inspection

In short, the Playwright CLI does not just run tests. It standardizes how tests are discovered, executed, isolated, debugged, and reported, making Playwright automation predictable across local machines and CI pipelines.

Installing Playwright Using CLI

Playwright installation is CLI-driven and handles more than just npm dependencies.

A standard installation command looks like:

npm init playwright@latest
This command:

  • Installs Playwright and Playwright Test
  • Downloads supported browser binaries
  • Creates a default configuration file
  • Sets up example tests and folders

For existing projects, browsers can be installed separately:

npx playwright install
This ensures that all required browsers are available without manual setup.

Core Playwright CLI Commands

Playwright CLI exposes several core commands that cover most workflows.

Key commands include:

  • playwright test for running test suites
  • playwright codegen for recording tests
  • playwright show-report for viewing test reports
  • playwright install for managing browsers
  • playwright install-deps for OS-level dependencies

These commands are designed to be composable and CI-friendly.

Running Tests with Playwright CLI

Test execution is the most common CLI use case.

A basic test run:

npx playwright test
This command:

  • Discovers all test files
  • Runs tests in parallel by default
  • Applies configured retries and timeouts
  • Generates reports and artifacts

Running tests for a specific browser:

npx playwright test –project=chromium
Running a single test file:

npx playwright test tests/login.spec.ts
These options allow fine-grained control without changing code.

Generating Tests with Playwright Codegen

Playwright CLI includes a recorder that converts real browser interactions into test code.

Starting code generation:

npx playwright codegen https://example.com
This opens:

  • A browser window for interaction
  • A code panel showing generated Playwright test code

Codegen is useful for:

  • Quickly capturing user flows
  • Reproducing bugs as executable tests
  • Onboarding team members unfamiliar with Playwright APIs

The generated code should be reviewed and refactored before production use.

Debugging Tests Using Playwright CLI

Debugging is built directly into the CLI, reducing the need for external tools.

Running tests in headed mode:

npx playwright test –headed
Running tests with Playwright Inspector:

npx playwright test –debug
Enabling trace collection:

npx playwright test –trace on
These options allow engineers to pause execution, inspect DOM state, and analyze failures using trace viewers and artifacts.

Debugging with Playwright CLI helps diagnose failures locally, but many issues surface only in real browser environments. BrowserStack Automate extends CLI debugging by providing videos, screenshots, logs, and traces from real browsers, making it easier to pinpoint environment-specific failures that local debugging cannot reproduce.

Talk to an Expert

Configuring Browsers, Devices, and Environments via CLI

The CLI respects configuration files but also allows overrides at runtime.

Examples include:

  • Running tests on a specific device profile
  • Switching browsers without code changes
  • Adjusting workers or retries dynamically
  • Setting environment variables for different stages

For example:

npx playwright test –workers=2
This flexibility is especially useful when tuning CI performance.

Managing Test Execution in CI with Playwright CLI

Playwright CLI is designed for CI-first execution.

Common CI use cases include:

  • Running tests headlessly by default
  • Retrying flaky tests automatically
  • Producing machine-readable reports
  • Failing builds based on test outcomes

A typical CI command:

npx playwright test –reporter=html
Because the CLI abstracts browser setup, CI pipelines remain simpler and more portable.

Common Mistakes When Using Playwright CLI

Misuse of the CLI often leads to flaky or slow pipelines.

Common mistakes include:

  • Running all tests serially without understanding parallel defaults
  • Ignoring browser installation in CI environments
  • Overusing debug flags in production pipelines
  • Treating recorder-generated tests as final code
  • Hardcoding environment-specific values instead of using config

Most of these issues stem from underutilizing CLI features.

Best Practices for Playwright CLI Usage

Effective CLI usage requires discipline rather than more commands.

Recommended practices include:

  • Using CLI flags for experimentation, not permanent behavior
  • Keeping configuration in version-controlled files
  • Running the same CLI commands locally and in CI
  • Refactoring recorded tests before scaling
  • Reviewing generated artifacts for flaky patterns

The CLI works best when it enforces consistency rather than convenience shortcuts.

Extension tests passing locally but failing in CI?

Run Playwright Chrome extension tests on real browsers with BrowserStack to avoid CI-only issues.
Playwright Banner

Why Run Playwright CLI Tests on BrowserStack?

Running Playwright tests through the CLI ensures consistent execution, but local and CI environments still differ significantly from real user setups. This is where BrowserStack Automate adds critical value.

  • Validate CLI-driven tests on real browsers: Playwright CLI often runs tests on locally installed or containerized browsers. BrowserStack Automate executes the same CLI tests on real versions of Chrome, Firefox, Safari, and Edge, exposing issues that only appear in real browser environments.
  • Catch environment-specific failures early: CLI tests that pass locally can fail due to differences in OS rendering, browser updates, or graphics behavior. Running them on BrowserStack surfaces these failures before they reach production.
  • Reduce flakiness caused by infrastructure differences:Local machines and CI containers introduce timing and resource variability. BrowserStack provides stable, production-like environments that make CLI test results more consistent.
  • Scale CLI execution without maintaining browser grids: As test suites grow, managing browsers and parallel execution becomes expensive. BrowserStack Automate handles scaling and parallelization without requiring custom infrastructure.
  • Ensure consistent results across teams and pipelines: Tests executed via Playwright CLI behave the same regardless of who runs them or where they run, since BrowserStack standardizes the execution environment.
  • Improve debugging with rich test artifacts: When a CLI test fails on BrowserStack, teams get access to logs, screenshots, and video recordings from real browser sessions, making root cause analysis faster and more reliable.

Running Playwright CLI tests on BrowserStack Automate ensures that automation validated through the CLI reflects real-world browser behavior instead of idealized local conditions.

Conclusion

Playwright CLI is more than a convenience layer-it is the backbone of how Playwright automation is created, executed, and scaled. From installing browsers to debugging failures and running tests in CI, nearly every workflow depends on the CLI behaving consistently.

Teams that understand and use the CLI effectively gain predictable execution, faster feedback, and smoother scaling. When combined with real-browser platforms like BrowserStack Automate, Playwright CLI becomes a reliable foundation for production-grade test automation rather than just a local development tool.

Tags
Playwright

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord