Playwright + TS · Lesson 35 of 36

0/36 done

Module 35: Playwright Configuration

35. Master Class: playwright.config.ts Configuration

20 minlesson

Try it yourself

Implement buildConfigTimeoutSummary:

  1. Accept testTimeout and expectTimeout in milliseconds.
  2. Return formatted string: "Test: <seconds>s, Expect: <seconds>s".

The editor has a worked example. Read it, tweak it, then press Mark as complete.

The Complete playwright.config.ts Template

typescript
import { defineConfig, devices } from "@playwright/test";
import dotenv from "dotenv";

dotenv.config();

export default defineConfig({
  testDir: "./tests",
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 2 : undefined,
  timeout: 30_000, // 30s per test

  expect: {
    timeout: 5_000, // 5s for retrying assertions
  },

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

  use: {
    baseURL: process.env.BASE_URL || "https://staging.targetqa.com",
    trace: "retain-on-failure",
    screenshot: "only-on-failure",
    video: "retain-on-failure",
    actionTimeout: 10_000,
    navigationTimeout: 15_000,
  },

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

Environment-Specific Configuration

Switch environments seamlessly using environment variables:

bash
BASE_URL=https://prod.targetqa.com npx playwright test

Inside tests, navigate with relative paths:

typescript
await page.goto("/dashboard"); // Automatically prepends baseURL!

Key Takeaways & Walkthrough Summary

The playwright.config.ts file is the central control hub of your entire automation project. In this masterclass, we will walk through every essential configuration: base URLs, cross-browser projects, viewport sizes, timeouts, retries, reporters, and environment variables. Let's configure our test framework like a pro!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

What is forbidOnly in playwright.config.ts?

It prevents committing accidental 'test.only()' into version control by crashing the CI build if any .only is detected.

Can projects have different base URLs?

Yes! You can specify 'use: { baseURL: ... }' inside specific project blocks.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip