Playwright Execution Commands

After writing your Playwright tests, the next important step is learning how to run, debug, and control those tests efficiently. Playwright provides a powerful command-line interface (CLI) which allows to execute tests in different modes, filtering specific tests, debugging failures, running tests in parallel, and generating detailed reports.

In this section, we are going to see the most commonly used Playwright execution commands. These commands helps us to run all tests or specific tests, execute tests in different browsers, enable debugging features, manage retries and timeouts, and view test reports. Understanding these commands is essential for day-to-day test execution as well as for running tests in CI/CD pipelines.

1. Basic Test Execution

Run all tests: npx playwright test

Run tests in headed mode (browser UI visible): npx playwright test --headed

Run tests in headless mode (default): npx playwright test --headless

2. Run Specific Tests

Run a specific test file: npx playwright test tests/example.spec.js

Run tests inside a folder: npx playwright test tests/login/

Run a test by its title: npx playwright test -g "Login Test"

Run all tests except matching titles: npx playwright test -g "smoke" --invert

3. Debugging Commands

Debug mode with Inspector: npx playwright test --debug

Run with visible UI and debugging: npx playwright test --headed --debug

Enable slow motion (delay between steps – useful for demos): npx playwright test --slow-mo=2000

4. Parallel Execution

Disable parallel execution: npx playwright test --workers=1

Run with a specific number of workers: npx playwright test --workers=4

5. Run Tests by Browser (Project)

Projects must be defined in playwright.config.js

Run only Chromium: npx playwright test --project=chromium

Run only Firefox: npx playwright test --project=firefox

Run only WebKit: npx playwright test --project=webkit

6. Retries and Timeouts

Set retries temporarily: npx playwright test --retries=2

Set a custom test timeout: npx playwright test --timeout=60000

7. Report Commands

View the HTML report: npx playwright show-report

Open trace viewer: npx playwright show-trace trace.zip

Note: We will cover Playwright reports in detail in an upcoming section.

8. Tag / Filter Tests with Grep

Run tests with the @smoke tag: npx playwright test --grep @smoke

Exclude @regression tests: npx playwright test --grep-invert @regression

9. Code Generation (Record Tests)

Record actions and generate a test script: npx playwright codegen

Record a script for a specific site: npx playwright codegen https://google.com

Note: We will cover Playwright code generation in detail in an upcoming section.

playwright.config.js

Most of the options shown above, such as browser selection, parallel execution, retries, timeouts, reporters, and tracing, can also be configured centrally in the playwright.config.js file.

Using the configuration file allows you to define default behavior for all tests, instead of passing command-line options every time. 

 

Related Tutorials