Course contentsShow
← React
Lesson 2162 of 2,39648. End-to-End TestingFree lesson

Installing and Configuring Playwright

Set up Playwright in a React project with npx playwright install and configure playwright.config.ts for your needs.

Installing and Configuring Playwright

What you'll learn: How to add Playwright to your React project and configure it to match your testing requirements.

The Installation Process

Playwright is remarkably straightforward to set up. Unlike many testing tools that require multiple packages and complex configuration, Playwright provides a single command that handles everything:

npx playwright install

This command downloads Playwright itself and the browser binaries (Chromium, Firefox, WebKit) needed for testing. Think of it like installing not just a testing framework, but also automated browsers that Playwright will control.

For a React project, you'll typically run this in your project root after adding Playwright as a dev dependency:

npm install -D @playwright/test
npx playwright install

Configuration Basics

Playwright uses a playwright.config.ts file at your project root. This is your control center for test behavior:

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

export default defineConfig({
  testDir: './e2e',
  timeout: 30000,
  use: {
    baseURL: 'http://localhost:5173',
    screenshot: 'only-on-failure',
  },
  webServer: {
    command: 'npm run dev',
    port: 5173,
    reuseExistingServer: true,
  },
});

The testDir points to where your E2E tests live. The webServer configuration is particularly valuable for React apps—Playwright will automatically start your dev server before tests and shut it down after.

The baseURL lets you write tests using relative paths instead of full URLs, and screenshot captures visual evidence when tests fail.

Key Takeaway: Install Playwright with npx playwright install, then configure playwright.config.ts to specify your test directory, dev server settings, and browser behavior—Playwright handles the rest, including starting your React app automatically.