Playwright Running Tests
This chapter comprehensively covers how to run Playwright tests, command-line arguments, and various filtering techniques.
Basic run commands
Example
Run all tests
npx playwright
test
By default, uses the project configured in package.json
npm
test
Filtering specific tests
By file path
# Run a single test file
npx playwright test tests/login.spec.ts
# Run all tests in the current directory
npx playwright test tests/user/
# Run multiple files (space separated)
npx playwright test tests/login.spec.ts tests/register.spec.ts
By filename keyword
# Run all tests whose filenames contain "login" or "user"
npx playwright test login user
Filter by test name (-g / --grep)
# Run tests whose names contain "login"
npx playwright test -g "登录"
# Filter using regular expressions
npx playwright test -g "登录|注册"
Filter by Tag
Example
// Define a tag in the test
test
(
'Quick smoke test @smoke'
,
async
(
{
page
}
)
=>
{
// ...
}
)
;
test
(
'Slow test @slow'
,
async
(
{
page
}
)
=>
{
// ...
}
)
;
# Run tests tagged with @smoke only
npx playwright test --grep "@smoke"
# Skip tests with @slow tag
npx playwright test --grep-invert "@slow"
Browser control
Specify browser (--project)
# Only runs on Chromium
npx playwright test --project=chromium
# Run on multiple browsers
npx playwright test --project=chromium --project=firefox
Headed mode (--headed)
# Show browser window (default headless)
npx playwright test --headed
Specify browser executable path
# Use locally installed Chrome instead of Playwright's Chromium
npx playwright test --project=chromium \
--channel=chrome
Execution mode
UI mode (--ui)
# Run in UI mode (recommended for development and debugging)
npx playwright test --ui
Debug mode (--debug)
# Step-by-step debugging test
npx playwright test --debug
Parallelism and repetition
Control the number of workers (--workers)
# Run with 4 workers in parallel
npx playwright test --workers=4
# Single-threaded execution (convenient for debugging)
npx playwright test --workers=1
Repeat runs (--repeat-each)
# Run each test 3 times (check stability)
npx playwright test --repeat-each=3
Sharded runs (--shard)
# Running in CI shards (split into 4 shards, currently shard 1)
npx playwright test --shard=1/4
# 第 2 片
npx playwright test --shard=2/4
Sharding is often used in CI to run tests in parallel across multiple machines, with each machine running only a portion, and the results are merged at the end.
Screenshot and Trace control
# Update baseline screenshots for visual regression
npx playwright test --update-snapshots
# Force generation of Trace file (even if not enabled in configuration)
npx playwright test --trace on
# View the last run Trace
npx playwright show-trace test-results/.../trace.zip
Common Command Quick Reference
| Command | Purpose |
|---|---|
npx playwright test |
Run all tests |
npx playwright test file.spec.ts |
Run a specified file |
npx playwright test -g "关键词" |
Filter by test name |
npx playwright test --project=chromium |
Specify browser |
npx playwright test --headed |
Headed mode (show browser window) |
npx playwright test --ui |
UI mode |
npx playwright test --debug |
Debug Mode |
npx playwright test --workers=4 |
Specify the number of workers |
npx playwright test --shard=1/4 |
Sharded run |
npx playwright test --repeat-each=3 |
Repeat run |
npx playwright test --update-snapshots |
Update visual baseline screenshots |
npx playwright show-report |
View the HTML report |
npx playwright show-trace trace.zip |
View Trace |
npx playwright codegen url |
Launch the code generator |