Q5 of 42 · Playwright

How do you install and configure Playwright in a TypeScript project?

PlaywrightJuniorplaywrightsetuptypescriptfundamentalsjunior

Short answer

Short answer: Run `npm init playwright@latest` for an interactive setup, or manually `npm i -D @playwright/test` followed by `npx playwright install` to download browsers. Configure via `playwright.config.ts` at the root. The init command scaffolds a sample test, GitHub Actions workflow, and a tsconfig if needed.

Detail

The fastest path is npm init playwright@latest (or yarn create playwright). The interactive prompt asks:

  • TypeScript or JavaScript.
  • Test directory (default tests/).
  • Whether to add a GitHub Actions workflow.
  • Whether to install all browsers (default yes).

It generates playwright.config.ts, an example test, and .github/workflows/playwright.yml. Browsers are downloaded via npx playwright install.

Manual install (when integrating into an existing project):

npm install -D @playwright/test
npx playwright install
npx playwright install-deps   # Linux only — system libraries for Chromium/Firefox

Minimal playwright.config.ts:

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  timeout: 30_000,
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
  },
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
  ],
});

Useful first-run commands:

npx playwright test                       # all tests
npx playwright test --ui                  # interactive UI mode
npx playwright test tests/login.spec.ts   # one file
npx playwright test --headed              # see the browser
npx playwright show-report                # open the last HTML report
npx playwright codegen https://example.com # record interactions

Useful config keys worth knowing on day one: baseURL, trace, video, screenshot, retries, workers, reporter, projects. Each is documented inline; defineConfig gives you autocomplete.

// WHAT INTERVIEWERS LOOK FOR

Knowing both the init wizard and the manual path, naming `npx playwright install`, and the most useful first-day commands (`--ui`, `codegen`, `show-report`).

// COMMON PITFALL

Forgetting `npx playwright install` after `npm install` and getting 'browser not found' errors.