What is `playwright.config.ts`?
What is playwright.config.ts?¶
It is the central configuration file that defines:
- Where your tests live
- How your tests run
- Which environment variables are loaded
- What reports are generated
- Runtime behavior like tracing, headless mode, and session reuse ๐ File Overview
import { defineConfig } from '@playwright/test';
import 'dotenv/config';
import * as path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const AUTOMATION_USER_STORAGE_STATE = path.join(
__dirname,
'utils/functions/loginSetup/loginData/automationUsertorageState.json'
);
- Loads environment variables from
.envviadotenv - Defines a path to the user's storage state JSON file (used for logged-in session reuse)
โ๏ธ defineConfig({ ... }) Options Explained¶
๐ globalSetup¶
globalSetup: './utils/functions/global.setup.ts',
- Script run once before all tests
- Used for setup tasks like logging in, saving tokens or storage state
๐ testDir testDir: './tests',
- Folder where all test specs reside
- UI and API specs should be organized in subfolders like
tests/uiandtests/api
โฑ๏ธ timeout & expect.timeout
timeout: 6000 * 1000, // 100 minutes max per test
expect: { timeout: 30000 }, // 30 seconds for individual expect() assertions
๐งช fullyParallel, forbidOnly, retries, workers | Option | Description | | --- | --- | | fullyParallel | Enables all tests to run independently and in parallel | | forbidOnly | Fails build if .only is left in test code (prevents skipping others) | | retries | Retries tests twice (even in non-CI environments) | | workers | Uses 1 worker in CI (serial run), unlimited locally |
๐ reporter reporter: process.env.CI ? [ ... ] : [ ... ] | Reporter | Description | | --- | --- | | html | Generates interactive HTML report | | list | Console-friendly test results | | playwright-ctrf-json-reporter | Outputs results in JSON (for CI pipelines or Sonar) | | monocart-reporter | Advanced interactive reports with timeline & filtering |
Reports are saved under ./reports/ folder
โ๏ธ use
use: {
trace: 'on',
headless: !!process.env.CI,
video: 'on',
screenshot: 'on'
}
| Setting | Description |
|---|---|
trace: 'on' | Captures trace for every test (viewable in HTML report) |
headless | Uses headless browser only in CI |
video: 'on' | Records test video for debugging |
screenshot: 'on' | Takes screenshots on failure |
๐ Best Practices¶
- Keep
globalSetupclean and limited to session prep - Use tagged tests (
@ui,@api) and filtered runs via-g - Archive video/screenshot artifacts in CI pipelines
- Update
timeoutvalues for slow environments if necessary