Playwright Global Setup

A shop owner switches on the lights and loads cash into the register before the first customer walks in. At closing time, the owner counts the cash and locks the door. Global setup and teardown perform the same duties for a test suite. Setup prepares the environment once before any test starts, and teardown cleans up once after the last test ends.

The Suite Timeline

Global Setup          (runs once)
     |
     v
Test 1  Test 2  Test 3 ... Test N   (many workers in parallel)
     |
     v
Global Teardown       (runs once)

Registering the Files

The config file points to the setup and teardown scripts.

import { defineConfig } from '@playwright/test';

export default defineConfig({
  globalSetup: './global-setup.ts',
  globalTeardown: './global-teardown.ts',
});

Writing the Setup Script

The script exports one asynchronous function. Playwright runs it before the first test.

import { FullConfig } from '@playwright/test';

async function globalSetup(config: FullConfig) {
  console.log('Preparing test data...');
  await fetch('https://staging.example.com/api/reset', { method: 'POST' });
}

export default globalSetup;

Writing the Teardown Script

async function globalTeardown() {
  console.log('Cleaning up...');
  await fetch('https://staging.example.com/api/cleanup', { method: 'POST' });
}

export default globalTeardown;

Typical Setup Jobs

  • Reset a test database to a known state.
  • Create user accounts that tests share.
  • Start a local server or mock service.
  • Delete old files from earlier runs.

Global Setup Compared With Hooks

A beforeAll hook runs once for each worker process and each file. Global setup runs exactly once for the entire run. Pick global setup for expensive jobs that must never repeat, and pick hooks for work that belongs to a group of tests.

globalSetup  -> once per full run
beforeAll    -> once per file per worker
beforeEach   -> once before every test

Sharing Values With Tests

Setup can store a value in an environment variable, and tests read it later.

process.env.SEED_USER = 'asha@example.com';

A Real-World Scenario

A booking site needs ten sample hotels in its staging database. The setup script wipes old data and inserts the ten hotels through an API call. Every test searches a predictable catalog. The teardown script removes any bookings created during the run, so the next morning starts clean.

Cautions

Global setup does not appear in the HTML report or the trace viewer, so failures there show only in the console. Keep the script short and log each step. A failure in setup stops the whole run before any test begins.

Quick Practice Task

Create a global-setup.ts file that prints a message. Register it in the config and run any test. Confirm the message appears once at the very start of the output, even when the suite holds many tests.

Common Questions

Can setup use a browser?

Yes. The setup function can launch a browser, perform a login, and save the session. Many teams now prefer a setup project because it appears in reports and traces.

What if teardown fails?

Playwright prints the error and marks the run as failed. Write teardown steps so they succeed even when setup stopped half-way.

Do tests share variables with setup directly?

They do not share memory. Pass values through environment variables, files, or a database that both sides read.

Key Takeaways

  • Global setup runs once before all tests, and teardown runs once after.
  • The config file registers both scripts.
  • Setup suits database resets, shared accounts, and service startup.
  • Hooks serve smaller groups of tests, and global setup serves the whole run.