35. Master Class: playwright.config.ts Configuration

The Complete playwright.config.ts Template

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:
BASE_URL=https://prod.targetqa.com npx playwright test

Inside tests, navigate with relative paths:

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

Task

  • Implement buildConfigTimeoutSummary:
  • 1. Accept testTimeout and expectTimeout in milliseconds.
  • 2. Return formatted string: "Test: <seconds>s, Expect: <seconds>s".
πŸ’‘Common doubts
Did you like the problem?
Lesson 35 of 36
TypeScript
Loading code editor…
Next