Skip to main content

Install Playwright and tour the project

Why This Matters

The first five minutes with a project decide how the next five months go. A config you understand means you know where reports end up, why a test was retried and which browser it ran in. A config you copied blindly means guessing, every time CI turns red.

Two ways to start

A new project starts with the Playwright initialiser:

npm init playwright@latest

It asks a few questions (TypeScript, the test folder, a GitHub Actions workflow), installs @playwright/test and downloads the browsers.

This course uses the example repository, which was created the same way and then tidied up:

git clone https://github.com/learn-automated-testing/playwright-practice.git
cd playwright-practice
npm ci                     # exact versions from package-lock.json
npx playwright install     # Chromium, Firefox and WebKit

npm ci instead of npm install: it installs exactly what the lock file says, so your setup matches CI.

The tour

playwright-practice/
├── playwright.config.ts       how, where and in which browsers tests run
├── src/                       types and helpers (module 1)
├── tests/
│   ├── module-1/              tests without a browser
│   └── module-2/              shop and login tests
├── eslint.config.mjs          catches a forgotten await
├── tsconfig.json              TypeScript settings for the type check
└── .github/workflows/         the CI pipeline (module 8)

playwright.config.ts, line by line

export default defineConfig({
  testDir: "./tests",
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  reporter: [
    ["list"],
    ["html", { open: "never" }],
    ["json", { outputFile: "test-results/results.json" }],
    ["junit", { outputFile: "test-results/junit.xml" }],
  ],
  use: {
    baseURL: process.env.BASE_URL ?? "https://practiceautomatedtesting.com",
    trace: "on-first-retry",
    screenshot: "only-on-failure",
  },
  projects: [
    { name: "unit", testMatch: /module-1\/.*\.spec\.ts/ },
    { name: "chromium", use: { ...devices["Desktop Chrome"] }, testIgnore: /module-1\// },
    { name: "firefox", use: { ...devices["Desktop Firefox"] }, testIgnore: /module-1\// },
    { name: "webkit", use: { ...devices["Desktop Safari"] }, testIgnore: /module-1\// },
  ],
});
  • baseURL lets tests write page.goto("/shopping") instead of the full address. Set BASE_URL to point the same tests at another environment.
  • fullyParallel runs tests side by side. That only works because tests do not share state; on practiceautomatedtesting.com every new browser context gets its own shop sandbox, so one test’s cart never shows up in another.
  • forbidOnly fails CI if someone commits a test.only by accident.
  • retries: none locally (you want to see a failure), two in CI. Module 7 explains why retries can also hide problems.
  • reporter: a list in the terminal, an HTML report for people, and JSON and JUnit for tools.
  • trace: "on-first-retry" records a full trace when a test is retried: the evidence you need in module 3.
  • projects run the browser tests in three browsers. The module 1 tests need no browser, so they run once, as the unit project.

First run

npm run test:chromium

This is a real run against the live site:

Running 12 tests using 7 workers

  ✓   1 …/module-2/login.spec.ts:10:1 › the practice user logs in and out (6.8s)
  ✓   2 … tests/module-2/login.spec.ts:32:3 › login fails: locked account (6.8s)
  ✓   3 … tests/module-2/login.spec.ts:32:3 › login fails: empty password (4.4s)
  ✓   4 … tests/module-2/login.spec.ts:32:3 › login fails: wrong password (5.2s)
  ✓   5 … › tests/module-2/login.spec.ts:32:3 › login fails: unknown user (5.3s)
  ✓   6 … › tests/module-2/login.spec.ts:41:1 › Show reveals the password (5.5s)
  ✓   7 … tests/module-2/login.spec.ts:32:3 › login fails: empty username (5.6s)
  ✓   8 …pec.ts:11:1 › the shop shows products with an Add to Cart button (3.4s)
  ✓   9 …tests/module-2/shop.spec.ts:17:1 › searching narrows the results (3.7s)
  ✓  10 …/shop.spec.ts:25:1 › adding two products updates the cart button (3.1s)
  ✓  11 …le-2/shop.spec.ts:39:1 › an out-of-stock product cannot be added (2.7s)
  ✓  12 …op.spec.ts:45:1 › the checkout shows subtotal, 21% tax and total (3.0s)

  12 passed (9.8s)

Twelve tests that take three to seven seconds each finished in under ten seconds: that is fullyParallel with seven workers.

Exercise

  1. Run npm test to run all projects. How many tests run, and why that number?
  2. Open the HTML report with npm run report and find the slowest test.
  3. Where would you look for the JSON report, and who is it for?
Show suggested answer
  1. 40 tests: the 4 module 1 tests once (the unit project) plus 12 browser tests in each of Chromium, Firefox and WebKit (36). On our machine the whole run took 26 seconds.
  2. The login tests are the slowest, because the login page waits about 0.8 seconds before it answers, on purpose, to practise waiting without sleeps.
  3. In test-results/results.json. It is for tools: dashboards, other pipelines, or the TestPilot plugin that triages failures with AI in module 8.