Answer
playwright.config.ts — The Configuration File
playwright.config.ts (or .js) is the central configuration file for Playwright Test. It sits at the project root and controls every aspect of how tests run.
Complete Config Example
TypeScript
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
// ── Test Location ─────────────────────────────────
testDir: './tests',
testMatch: '**/*.spec.ts', // Test file pattern
// ── Execution ─────────────────────────────────────
fullyParallel: true, // All tests run in parallel
workers: process.env.CI ? 2 : 4, // Worker count
retries: process.env.CI ? 2 : 0, // Retry failed tests on CI
// ── Timeouts ──────────────────────────────────────
timeout: 30_000, // Per-test timeout (30s)
expect: { timeout: 5_000 }, // Assertion timeout (5s)
// ── Global Setup ──────────────────────────────────
globalSetup: './global-setup.ts',
globalTeardown: './global-teardown.ts',
// ── Reporters ─────────────────────────────────────
reporter: [
['html'], // HTML report
['list'], // Console output
['json', { outputFile: 'results.json' }],
],
// ── Shared Options for All Tests ──────────────────
use: {
baseURL: 'https://myapp.com',
headless: true,
screenshot: 'only-on-failure',
video: 'retain-on-failure',
trace: 'retain-on-failure',
actionTimeout: 10_000,
navigationTimeout: 30_000,
viewport: { width: 1280, height: 720 },
ignoreHTTPSErrors: true,
locale: 'en-US',
timezoneId: 'America/New_York',
},
// ── Browser Projects ──────────────────────────────
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
// Mobile emulation
{
name: 'Mobile Chrome',
use: { ...devices['Pixel 5'] },
},
// Setup project (runs before tests)
{
name: 'setup',
testMatch: /.*\.setup\.ts/,
},
],
});
Key Configuration Options
| Option | Description |
|---|---|
testDir | Directory where tests live |
fullyParallel | Run all tests in parallel |
workers | Number of parallel workers |
retries | Retry count for failing tests |
timeout | Max time per test |
baseURL | Base URL for page.goto('/') |
screenshot | When to capture screenshots |
video | When to record video |
trace | When to capture trace |
reporter | Output format (html, json, list) |
projects | Browser configurations |
