</>

Technology

Playwright

Difficulty

Beginner

Interview Question

What is the playwright.config.ts file and what does it configure?

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

OptionDescription
testDirDirectory where tests live
fullyParallelRun all tests in parallel
workersNumber of parallel workers
retriesRetry count for failing tests
timeoutMax time per test
baseURLBase URL for page.goto('/')
screenshotWhen to capture screenshots
videoWhen to record video
traceWhen to capture trace
reporterOutput format (html, json, list)
projectsBrowser configurations

Follow AutomateQA

Related Topics