YouTip LogoYouTip

Playwright Running Tests

\\n\\n\\n\\nPlaywright Running Tests | Online Tutorial\\n\\n\\n\\n

Playwright Running Tests | Online Tutorial

\\n\\n

This chapter comprehensively introduces how to run Playwright tests, command line parameters, and various filtering techniques.

\\n\\n
\\n\\n

Basic Run Commands

\\n\\n

Run All Tests

\\n\\n
npx playwright test\\n
\\n\\n

Use the project configured in package.json by default

\\n\\n
npm test\\n
\\n\\n
\\n\\n

Filter Specific Tests

\\n\\n

By 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\\n

By File Name Keyword

\\n\\n
# Run all tests with "login" or "user" in the file name\\nnpx playwright test login user\\n
\\n\\n

Filter 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\\n

Filter 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\\n

Specify 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\\n

Headed Mode (--headed)

\\n\\n
# Show browser window (headless by default)\\nnpx playwright test --headed\\n
\\n\\n

Specify 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\\n

UI Mode (--ui)

\\n\\n
# Run in UI mode (recommended for development and debugging)\\nnpx playwright test --ui\\n
\\n\\n

Debug Mode (--debug)

\\n\\n
# Debug tests step by step\\nnpx playwright test --debug\\n
\\n\\n
\\n\\n

Parallel and Repeat

\\n\\n

Control 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\\n

Repeat 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\\n

Sharding 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\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n
CommandDescription
npx playwright testRun all tests
npx playwright test file.spec.tsRun specified file
npx playwright test -g "keyword"Filter by test name
npx playwright test --project=chromiumSpecify browser
npx playwright test --headedHeaded mode (show browser window)
npx playwright test --uiUI mode
npx playwright test --debugDebug mode
npx playwright test --workers=4Specify number of workers
npx playwright test --shard=1/4Run in shards
npx playwright test --repeat-each=3Repeat run
npx playwright test --update-snapshotsUpdate visual baseline screenshots
npx playwright show-reportView HTML report
npx playwright show-trace trace.zipView Trace
npx playwright codegen urlStart code generator
\\n\\n \\n
← Playwright CodegenPlaywright Config β†’