Playwright Running Tests | Online Tutorial
\\n\\nThis chapter comprehensively introduces how to run Playwright tests, command line parameters, and various filtering techniques.
\\n\\n\\n\\n
Basic Run Commands
\\n\\nRun All Tests
\\n\\nnpx playwright test\\n\\n\\nUse the project configured in package.json by default
\\n\\nnpm test\\n\\n\\n\\n\\n
Filter Specific Tests
\\n\\nBy File Path
\\n\\n# Run a single test file\\nnpx playwright test tests/login.spec.ts\\n\\n# Run all tests in a directory\\nnpx playwright test tests/user/\\n\\n# Run multiple files (space separated)\\nnpx playwright test tests/login.spec.ts tests/register.spec.ts\\n\\n\\nBy File Name Keyword
\\n\\n# Run all tests with "login" or "user" in the file name\\nnpx playwright test login user\\n\\n\\nFilter by Test Name (-g / --grep)
\\n\\n# Run tests with "Login" in the test name\\nnpx playwright test -g "Login"\\n\\n# Filter by regular expression\\nnpx playwright test -g "Login|Register"\\n\\n\\nFilter by Tag
\\n\\n// Define tag in test\\n\\ntest('Quick smoke test @smoke', async ({ page }) => {\\n\\n// ...\\n\\n});\\n\\ntest('Slow test @slow', async ({ page }) => {\\n\\n// ...\\n\\n});\\n\\n# Run only tests with @smoke tag\\nnpx playwright test --grep "@smoke"\\n\\n# Skip tests with @slow tag\\nnpx playwright test --grep-invert "@slow"\\n\\n\\n\\n\\n
Browser Control
\\n\\nSpecify Browser (--project)
\\n\\n# Run only on Chromium\\nnpx playwright test --project=chromium\\n\\n# Run on multiple browsers\\nnpx playwright test --project=chromium --project=firefox\\n\\n\\nHeaded Mode (--headed)
\\n\\n# Show browser window (headless by default)\\nnpx playwright test --headed\\n\\n\\nSpecify Browser Executable Path
\\n\\n# Use locally installed Chrome instead of Playwright's Chromium\\nnpx playwright test --project=chromium \\\\\\n --channel=chrome\\n\\n\\n\\n\\n
Execution Mode
\\n\\nUI Mode (--ui)
\\n\\n# Run in UI mode (recommended for development and debugging)\\nnpx playwright test --ui\\n\\n\\nDebug Mode (--debug)
\\n\\n# Debug tests step by step\\nnpx playwright test --debug\\n\\n\\n\\n\\n
Parallel and Repeat
\\n\\nControl Parallelism (--workers)
\\n\\n# Run with 4 workers in parallel\\nnpx playwright test --workers=4\\n\\n# Run single-threaded (convenient for debugging)\\nnpx playwright test --workers=1\\n\\n\\nRepeat Run (--repeat-each)
\\n\\n# Repeat each test 3 times (check stability)\\nnpx playwright test --repeat-each=3\\n\\n\\n\\n\\n
Shard Run (--shard)
\\n\\n# Run in shards on CI (split into 4 shards, current is shard 1)\\nnpx playwright test --shard=1/4\\n\\n# Shard 2\\nnpx playwright test --shard=2/4\\n\\n\\nSharding is commonly used in CI to run tests in parallel on multiple machines, with each machine running only a portion, and finally merging the results.
\\n\\n\\n\\n
Screenshot and Trace Control
\\n\\n# Update baseline screenshots for visual regression\\nnpx playwright test --update-snapshots\\n\\n# Force generate Trace file (even if not enabled in config)\\nnpx playwright test --trace on\\n\\n# View trace from last run\\nnpx playwright show-trace test-results/.../trace.zip\\n\\n\\n\\n\\n
Quick Command Reference
\\n\\n| Command | Description |
|---|---|
npx playwright test | Run all tests |
npx playwright test file.spec.ts | Run specified file |
npx playwright test -g "keyword" | 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 number of workers |
npx playwright test --shard=1/4 | Run in shards |
npx playwright test --repeat-each=3 | Repeat run |
npx playwright test --update-snapshots | Update visual baseline screenshots |
npx playwright show-report | View HTML report |
npx playwright show-trace trace.zip | View Trace |
npx playwright codegen url | Start code generator |
YouTip