SA
SDET QA

Installing Playwright

Learn how to install Playwright and set up your testing environment.

Prerequisites

  • Node.js version 16 or higher
  • npm or yarn package manager

Installation Steps

1. Initialize a New Project

Create a new directory and initialize npm:

bash
mkdir my-playwright-tests
cd my-playwright-tests
npm init -y

2. Install Playwright

Install Playwright with test runner:

bash
npm init playwright@latest

This command will:

  • Install Playwright and its dependencies
  • Set up browser binaries (Chromium, Firefox, WebKit)
  • Create a playwright.config.ts configuration file
  • Add example test files

3. Verify Installation

Run the example tests to verify everything is working:

bash
npx playwright test

4. Project Structure Explained

After installation, your project structure should look like this:

Project Structure
playwright-automation/
│
ā”œā”€ā”€ tests/
│   └── example.spec.ts
│
ā”œā”€ā”€ playwright.config.ts
ā”œā”€ā”€ package.json
ā”œā”€ā”€ package-lock.json
ā”œā”€ā”€ node_modules/
└── test-results/

Explanation:

  • tests/: Directory for all test files
  • playwright.config.ts: Configuration file for Playwright settings or Global Configuration
  • package.json: Project metadata and dependencies
  • node_modules/: Installed dependencies
  • test-results/: Directory for test results, screenshots, videos and traces
  • example.spec.ts: Sample test file to get you started. (*.spec.ts is the test naming convention)

Configuration

The playwright.config.ts file contains all your test configuration:

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

export default defineConfig({
  testDir: './tests',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: 'html',
  use: {
    baseURL: 'http://127.0.0.1:3000',
    trace: 'on-first-retry',
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },
    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },
  ],
});