Skip to content

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 .env via dotenv
  • 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/ui and tests/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 globalSetup clean and limited to session prep
  • Use tagged tests (@ui, @api) and filtered runs via -g
  • Archive video/screenshot artifacts in CI pipelines
  • Update timeout values for slow environments if necessary
Back to top