Playwright CLI (Command-Line Interface) is a command-line utility provided by Microsoft Playwright that helps developers and QA engineers efficiently create, execute, debug, and maintain automated tests. It acts as the primary interface for managing Playwright projects directly from the terminal.
Whether you’re building a fresh automation framework, recording tests, debugging failures, generating reports, or integrating with CI/CD pipelines, Playwright CLI provides powerful commands to streamline the automation lifecycle.
- Installing Playwright Create a New Project Shell
npm init playwright@latest
This command:
Installs Playwright
Downloads browser binaries
Creates sample tests
Generates configuration files
Sample folder structure:
project
├── tests/
├── playwright.config.js
├── package.json
└── node_modules/
- Install Browsers Install all supported browsers:
Shell
npx playwright install
Install specific browsers:
Shell
npx playwright install chromium
npx playwright install firefox
npx playwright install webkit
Install browsers with system dependencies (Linux):
Shell
npx playwright install — with-deps
- Running Tests Execute All Tests Shell npx playwright test
Execute Specific Test File
Shell
npx playwright test login.spec.js
Example:
Shell
npx playwright test tests/login.spec.ts
Execute Specific Test by Name
Shell
npx playwright test -g “Verify Successful Login”
The -g option filters tests using a regex pattern.
- Running Tests on Specific Browsers Chromium Shell
npx playwright test — project=chromium
Firefox
Shell
npx playwright test — project=firefox
WebKit
Shell
npx playwright test — project=webkit
- Headed vs Headless Execution Headless (Default) Shell
npx playwright test
Headed Mode
Shell
npx playwright test — headed
Useful for debugging UI issues.
- Parallel Test Execution Run tests in parallel:
Shell
npx playwright test — workers=4
Example:
Shell
npx playwright test — workers=8
Benefits:
Faster execution
Better CI/CD performance
Efficient resource utilization
Inspector Mode
Shell
npx playwright test — debug
Features:
Step-by-step execution
Element inspection
Action highlighting
Network monitoring
Pause Execution
JavaScript
await page.pause();
Execution stops until user resumes.
- Record New Tests One of Playwright’s most popular CLI features.
Shell
npx playwright codegen https://example.com
Playwright automatically:
Opens browser
Records interactions
Generates test code
Suggests reliable locators
Example output:
JavaScript
await page.getByRole(‘textbox’).fill(‘admin’);
await page.getByRole(‘button’, { name: ‘Login’ }).click();
- Generate Locators Use Codegen to identify robust locators:
Shell
npx playwright codegen
Preferred locator priorities:
getByRole()
getByLabel()
getByPlaceholder()
getByText()
CSS selectors
- UI Mode Launch Playwright Test Runner UI.
Shell
npx playwright test — ui
Features:
Visual test explorer
Run individual tests
View traces
Analyze failures
Watch mode
Ideal for local development.
- Trace Viewer Trace Viewer helps identify test failures.
Enable tracing:
JavaScript
use: {
trace: ‘on-first-retry’
}
Open trace:
Shell
npx playwright show-trace trace.zip
You can inspect:
Screenshots
Network requests
DOM snapshots
Test actions
Console logs
- View HTML Reports Generate report:
Shell
npx playwright test
Open report:
Shell
npx playwright show-report
Sample report contains:
Pass/Fail status
Duration
Screenshots
Traces
Error details
- Taking Screenshots Capture screenshot via CLI:
Shell
npx playwright screenshot https://example.com screenshot.png
Useful for quick validation.
- Generate PDF Chromium-based PDF generation:
Shell
npx playwright pdf https://example.com output.pdf
Useful for:
Invoice validation
Reporting
Web page snapshots
- Run Tests with Different Configurations Custom configuration:
Shell
npx playwright test — config=playwright.staging.config.ts
Example environments:
Shell
playwright.dev.config.ts
playwright.qa.config.ts
playwright.prod.config.ts
- Retries Execute failed tests automatically.
Shell
npx playwright test — retries=2
Configuration:
JavaScript
retries: 2
Benefits:
Reduces flaky failures
Improves CI stability
- Repeat Test Execution Run the same test multiple times:
Shell
npx playwright test — repeat-each=10
Useful for:
Detecting flaky tests
Stability testing
- List Tests Display all available tests.
Shell
npx playwright test — list
Example output:
tests/login.spec.ts
tests/cart.spec.ts
tests/order.spec.ts
- Update Visual Snapshots For Visual Testing:
Shell
npx playwright test — update-snapshots
or
Shell
npx playwright test -u
Used when UI intentionally changes.
- Run Tests by Tag Example test:
JavaScript
test(‘@smoke Login Test’, async ({ page }) => {
});
Run:
Shell
npx playwright test -g @smoke
Other examples:
Shell
npx playwright test -g @regression
npx playwright test -g @sanity
- Environment Variables Windows:
BAT
set ENV=QA
npx playwright test
Linux/Mac:
Shell
ENV=QA npx playwright test
Usage:
JavaScript
const env = process.env.ENV;
- CI/CD Integration Azure DevOps YAML
script: npm ci
script: npx playwright install — with-deps
script: npx playwright test
GitHub Actions
YAML
- name: Install Dependencies
run: npm ci
- name: Install Browsers
run: npx playwright install — with-deps
- name: Execute Tests
run: npx playwright test
- Useful Daily Commands PurposeCommandCreate Projectnpm init playwright@latestRun All Tests npx playwright testRun in Headed Mode npx playwright test — headedDebug Modenpx playwright test — debug Open UI Mode npx playwright test — uiRecord Testsnpx playwright codegenShow Reportnpx playwright show-reportShow Tracenpx playwright show-trace trace.zipRun Smoke Testsnpx playwright test -g @smokeRetry Failuresnpx playwright test — retries=2Update Snapshotsnpx playwright test -u
Best Practices for QA Engineers
✅ Prefer getByRole() and accessibility-based locators.
✅ Use — debug and Trace Viewer for troubleshooting.
✅ Configure retries only for flaky environments.
✅ Run smoke tests via tags in CI pipelines.
✅ Enable traces on failure.
✅ Use UI Mode during framework development.
✅ Generate reports after every execution.
✅ Execute tests in parallel to reduce runtime.
Conclusion
Playwright CLI is much more than a simple test runner. It provides a complete toolkit for test creation, execution, debugging, reporting, tracing, visual testing, and CI/CD integration. Mastering commands such as test, codegen, show-report, show-trace, — debug, and — ui can significantly improve the productivity of QA Automation Engineers and SDETs.
For professionals transitioning from Selenium to Playwright, proficiency with the Playwright CLI is one of the fastest ways to accelerate framework development, debugging, and large-scale test execution.
Top comments (0)