Playwright Configuration Explained

This chapter provides a detailed introduction toplaywright.config.tsthe various configurations in it, allowing you to flexibly adjust test behavior based on project requirements.


Configuration File Overview

playwright.config.tsis Playwright's central configuration file, where all test runtime parameters are defined.

Example

// File path: playwright.config.ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  // ===== Basic Configuration =====
  testDir: './tests',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: 'html',
  timeout: 30000,

  // ===== Global Shared Configuration =====
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
  },

  // ===== Multi-browser Projects =====
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
    { name: 'firefox',  use: { ...devices['Desktop Firefox'] } },
    { name: 'webkit',   use: { ...devices['Desktop Safari'] } },
  ],

  // ===== Web Server =====
  webServer: {
    command: 'npm run start',
    url: 'http://localhost:3000',
    reuseExistingServer: !process.env.CI,
  },
});

Basic Configuration Options

Configuration ItemTypeDefault ValueDescription
testDirstringtestsDirectory where test files are located
fullyParallelbooleanfalseWhether to run all tests fully in parallel (including tests within the same file)
workersnumberCPU cores / 2Number of workers executing in parallel
retriesnumber0Number of retries for failed tests
timeoutnumber30000Total timeout per test (milliseconds)
reporterstring/arraylistTest reporter type
forbidOnlybooleanfalseFail if test.only() is detected on CI
testMatchstring/RegExp*.spec.(ts\|js)Test file matching pattern
testIgnorestring/RegExp-Ignored test file patterns

Configuration Example

Example

export default defineConfig({
  // Find tests in the e2e directory
  testDir: './e2e',

  // All tests fully parallel
  fullyParallel: true,

  // Automatically allocate worker count
  workers: undefined,

  // No retry on failure (local) / retry 2 times (CI)
  retries: process.env.CI ? 2 : 0,

  // Use both command line and HTML reports
  reporter: [
    ['list'],
    ['html', { open: 'never' }],
  ],
});

use Common Configuration

useThe configuration in it applies to all tests and projects:

Configuration ItemTypeDescription
baseURLstringBase URL, relative paths can be used in tests (e.g.await page.goto('/login'))
viewport{width, height}Browser viewport size, default{ width: 1280, height: 720 }
localestringBrowser language, e.g.'zh-CN'、'en-US'
timezonestringTimezone, e.g.'Asia/Shanghai'
colorSchemestringColor scheme:'light'|'dark'|'no-preference'
geolocation{latitude, longitude}Simulated geolocation
permissionsstring[]Granted browser permissions (e.g.['geolocation'])
userAgentstringCustom User-Agent string
tracestringTrace collection strategy:'on'|'off'|'on-first-retry'
screenshotstringScreenshot strategy:'on'|'off'|'only-on-failure'
videostringVideo recording strategy:'on'|'off'|'retain-on-failure'
storageStatestringAuthentication state file path
testIdAttributestringCustom testId attribute name, default'data-testid'
actionTimeoutnumberTimeout for each operation (click, fill)
navigationTimeoutnumberTimeout for each navigation

webServer Configuration

webServerThe configuration allows Playwright to automatically start your development server before running tests:

Example

export default defineConfig({
  webServer: {
    // Startup command
    command: 'npm run dev',

    // Wait until this URL is accessible before running tests
    url: 'http://localhost:5173',

    // Timeout wait duration (milliseconds)
    timeout: 120000,

    // Whether to reuse an already running server (can reuse locally, restart on CI)
    reuseExistingServer: !process.env.CI,
  },
});

globalSetup / globalTeardown Global Hooks

Executed once before/after all tests run, suitable for operations such as database initialization.

Example

export default defineConfig({
  globalSetup: './global-setup.ts',
  globalTeardown: './global-teardown.ts',
});

Example

// File path: global-setup.ts
import { FullConfig } from '@playwright/test';

async function globalSetup(config: FullConfig) {
  // Executed before all tests run
  console.log('Starting global setup...');
  // For example: initialize test database, create test users, etc.
}

export default globalSetup;
Other Extensions