Playwright Running Tests
Running Playwright Tests
You run Playwright tests with the command npx playwright test.
Options after the command choose which tests to run, and how.
The examples in this chapter use a project with two test files:
tests/todo.spec.js: the two tests from the First Test chaptertests/login.spec.js: two tests for the Login practice page
Example
tests/login.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('can log in', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/login.html');
await page.getByLabel('Username').fill('student');
await page.getByLabel('Password').fill('w3schools');
await page.getByRole('button', { name: 'Log in' }).click();
await expect(page.getByTestId('welcome')).toHaveText('Welcome, student!');
});
test('shows an error for a wrong password', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/login.html');
await page.getByLabel('Username').fill('student');
await page.getByLabel('Password').fill('wrong');
await page.getByRole('button', { name: 'Log in' }).click();
await expect(page.getByRole('alert')).toHaveText('Wrong username or password.');
});
Run All Tests
Run every test in the project:
Example
npx playwright test
Output:
Running 4 tests using 4 workers
[1/4] [chromium] › tests/login.spec.js:4:1 › can log in
[2/4] [chromium] › tests/todo.spec.js:4:1 › has title
[3/4] [chromium] › tests/todo.spec.js:9:1 › can add a todo
[4/4] [chromium] › tests/login.spec.js:12:1 › shows an error for a wrong password
4 passed (3.5s)
Each line shows the project (chromium), the file, the line where the test starts, and the test name.
The tests run at the same time, so the order can change from run to run.
Run Some of the Tests
Run one test file by writing its name after the command:
Example
npx playwright test tests/login.spec.js
Output:
Running 2 tests using 2 workers
[1/2] [chromium] › tests/login.spec.js:12:1 › shows an error for a wrong password
[2/2] [chromium] › tests/login.spec.js:4:1 › can log in
2 passed (3.6s)
Part of the name is enough: npx playwright test login runs every test file with "login" in its name.
Run one test by adding the line number where it starts, after a colon:
Example
npx playwright test tests/login.spec.js:12
Output:
Running 1 test using 1 worker
[1/1] [chromium] › tests/login.spec.js:12:1 › shows an error for a wrong password
1 passed (3.5s)
Run the tests whose title matches a text, with -g (short for --grep):
Example
npx playwright test -g "log in"
Output:
Running 1 test using 1 worker
[1/1] [chromium] › tests/login.spec.js:4:1 › can log in
1 passed (3.4s)
The text is a regular expression.
It is matched against the project name, the file name and the test title together.
So -g "todo" also runs the "has title" test, because that test is in todo.spec.js.
See which tests would run, without running them, with --list:
Example
npx playwright test --list
Output:
Listing tests:
[chromium] › login.spec.js:4:1 › can log in
[chromium] › login.spec.js:12:1 › shows an error for a wrong password
[chromium] › todo.spec.js:4:1 › has title
[chromium] › todo.spec.js:9:1 › can add a todo
Total: 4 tests in 2 files
Browsers and Workers
Tests run headless by default: you do not see the browser.
Add --headed to open a browser window for each test, so you can watch what happens:
Example
npx playwright test --headed
Run the tests in one project only, with --project and the project name from your config file:
Example
npx playwright test --project=chromium
Output:
Running 4 tests using 4 workers
[1/4] [chromium] › tests/todo.spec.js:4:1 › has title
[2/4] [chromium] › tests/todo.spec.js:9:1 › can add a todo
[3/4] [chromium] › tests/login.spec.js:12:1 › shows an error for a wrong password
[4/4] [chromium] › tests/login.spec.js:4:1 › can log in
4 passed (3.0s)
Our config has only one project, so the result is the same as before.
It is useful when your config file lists several browsers.
A worker is a process that runs tests.
By default, Playwright starts as many workers as half the CPU cores of your computer.
Use --workers to choose the number, for example 1 to run one test at a time:
Example
npx playwright test --workers=1
Output:
Running 4 tests using 1 worker
[1/4] [chromium] › tests/login.spec.js:4:1 › can log in
[2/4] [chromium] › tests/login.spec.js:12:1 › shows an error for a wrong password
[3/4] [chromium] › tests/todo.spec.js:4:1 › has title
[4/4] [chromium] › tests/todo.spec.js:9:1 › can add a todo
4 passed (4.6s)
With one worker, the tests run in order, and the run takes longer.
Show the Report
The config in this tutorial uses the HTML reporter.
After a run, open the report in your browser:
Example
npx playwright show-report
Output:
Serving HTML report at http://localhost:9323. Press Ctrl+C to quit.
The report shows every test, how long it took, and the error for every failed test.
Press Ctrl+C in the terminal to stop the report server.
Tip: When a test fails, Playwright opens the report automatically.
To run and watch tests in a window, or rerun only the tests that failed, see the next chapter: UI Mode.