Running tests the way you need
Why This Matters
You will run tests dozens of times a day: one test while you write it, one browser while you debug, everything before you push, the same test twenty times when you suspect it is flaky. Knowing the handful of command-line options that do this saves you minutes every hour.
The commands
| You want | Command |
|---|---|
| Everything | npx playwright test |
| One file | npx playwright test login.spec.ts |
| One browser | npx playwright test --project=chromium |
| Tests whose title matches | npx playwright test -g "cart" |
| Watch the browser | npx playwright test --headed |
| One worker, in order | npx playwright test --workers=1 |
| Check for flakiness | npx playwright test --repeat-each=20 |
| Only the tests that failed last time | npx playwright test --last-failed |
| UI mode | npx playwright test --ui |
Options combine. One test, in Firefox, visible, five times:
npx playwright test -g "cart" --project=firefox --headed --repeat-each=5
The repository also has npm scripts for the common ones: npm test, npm run test:chromium, npm run test:ui and npm run report.
Three browsers, one test
Running all projects runs every browser test in Chromium, Firefox and WebKit, the engine behind Safari. A real run of the example repository:
Running 40 tests using 7 workers
...
40 passed (25.7s)
The same 12 browser tests passed in all three engines without a single change. That is the payoff of role-based locators and web-first assertions: nothing in the tests depends on how one browser happens to render or time things.
In the code: only, skip and describe
test.only("the one I'm working on", async ({ page }) => { /* ... */ }); // run only this
test.skip("not ready yet", async ({ page }) => { /* ... */ }); // skip this
test.describe("checkout", () => { // group tests
test("shows the total", async ({ page }) => { /* ... */ });
});
test.only is handy while you write a test and dangerous if you commit it: the whole suite would silently shrink to one test. That is why the config sets forbidOnly: !!process.env.CI, which turns a committed test.only into a failed CI run.
Tests from data
The login tests in the repository are one loop over a list of cases:
const cases = [
{ name: "wrong password", username: "practice", password: "wrong", error: "Invalid username or password." },
{ name: "locked account", username: "locked_user", password: PASSWORD, error: "This account is locked. Please contact support." },
// ...
];
for (const c of cases) {
test(`login fails: ${c.name}`, async ({ page }) => {
// ...
await expect(page.getByRole("alert").filter({ hasText: c.error })).toBeVisible();
});
}
Each case becomes its own test with its own name, so -g "locked" runs just one, and a failure tells you exactly which case broke.
Exercise
- Run only the shop tests on WebKit, headed.
- Run the login tests twenty times in Chromium. Do any fail?
- Add
test.onlyto one test, runCI=1 npx playwright test, and read the error. Then remove it.
Show suggested answer
npx playwright test shop.spec.ts --project=webkit --headednpx playwright test login.spec.ts --project=chromium --repeat-each=20runs 140 tests (7 × 20). They should all pass: every assertion waits for the page instead of sleeping. If one fails only now and then, you have found a flaky test, and module 3 shows how to find out why.- With
CIset, Playwright stops with an error that the run contains a focused test (test.only) and runs nothing. WithoutCI, it would have quietly run just that one test.