Skip to main content

⚙️ Module 8 – Test Configuration

The playwright.config.js file is the heart of your Playwright framework.

It controls:

  • Which browsers to run
  • Timeouts
  • Retries
  • Reports
  • Screenshots & Videos
  • Base URL
  • Workers & Parallelism

✅ Complete Configuration File

const { defineConfig, devices } = require('@playwright/test');

module.exports = defineConfig({
// Test directory
testDir: './tests',

// Run tests in parallel
fullyParallel: true,

// Fail build on CI if test.only is left
forbidOnly: !!process.env.CI,

// Retry failed tests on CI
retries: process.env.CI ? 2 : 1,

// Number of workers
workers: process.env.CI ? 1 : 2,

// Reporter
reporter: [
['html', { open: 'never' }],
['list']
],

use: {
// Base URL
baseURL: 'https://practicetestautomation.com',

// Browser options
headless: true,
viewport: { width: 1280, height: 720 },

// Timeouts
actionTimeout: 10000,
navigationTimeout: 30000,

// Screenshots & traces
screenshot: 'only-on-failure',
trace: 'on-first-retry',
video: 'on-first-retry',
},

// Global timeout per test
timeout: 30000,

// Global expect timeout
expect: {
timeout: 5000,
},

projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
],
});

✅ Configuration Deep Dive


1️⃣ testDir

testDir: './tests',

Tells Playwright where to look for test files.


2️⃣ fullyParallel

fullyParallel: true,

Runs all tests in parallel across all files.

ValueBehaviour
trueAll tests run in parallel
falseTests run sequentially

3️⃣ retries

retries: process.env.CI ? 2 : 1,

Automatically retries failed tests.

EnvironmentRetries
CI2 retries
Local1 retry

4️⃣ workers

workers: process.env.CI ? 1 : 2,

Number of parallel workers.

EnvironmentWorkers
CI1 (stable)
Local2 (faster)

5️⃣ reporter

reporter: [
['html', { open: 'never' }],
['list']
],

Multiple reporters at once:

ReporterDescription
htmlBeautiful HTML report
listTerminal output
dotMinimal dots output
jsonJSON output for CI

6️⃣ baseURL

baseURL: 'https://practicetestautomation.com',

Set once, use everywhere:

// Instead of full URL
await page.goto('https://practicetestautomation.com/practice-test-login/');

// You can use relative path
await page.goto('/practice-test-login/');

7️⃣ headless

headless: true,
ValueBehaviour
trueBrowser runs in background
falseBrowser window visible

8️⃣ viewport

viewport: { width: 1280, height: 720 },

Sets browser window size for all tests.


9️⃣ Timeouts

// Per action timeout (click, fill, etc)
actionTimeout: 10000,

// Navigation timeout (page.goto)
navigationTimeout: 30000,

// Global test timeout
timeout: 30000,

// Assertion timeout
expect: {
timeout: 5000,
},
TimeoutControls
actionTimeoutEach action like click, fill
navigationTimeoutPage navigation
timeoutEntire test duration
expect.timeoutEach assertion

🔟 Screenshots, Traces & Videos

screenshot: 'only-on-failure',
trace: 'on-first-retry',
video: 'on-first-retry',
OptionValues
screenshoton, off, only-on-failure
traceon, off, on-first-retry
videoon, off, on-first-retry

1️⃣1️⃣ projects

projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
],

Run same tests across multiple browsers simultaneously.


✅ Useful CLI Options

# Run specific browser
npx playwright test --project=chromium

# Run in headed mode
npx playwright test --headed

# Run specific file
npx playwright test tests/api-testing.spec.js

# Run specific test by name
npx playwright test -g "Login test"

# Run with retries
npx playwright test --retries=3

# Run with specific workers
npx playwright test --workers=4

# Debug mode
npx playwright test --debug

🎯 Summary

Config OptionPurpose
testDirWhere tests live
fullyParallelRun tests in parallel
retriesAuto retry failed tests
workersParallel execution threads
reporterHow results are reported
baseURLBase URL for all tests
headlessShow or hide browser
timeoutTest time limit
projectsMulti-browser setup

🚀 Keep Going

Apply what you learned in this module: