← Back to library
Playwrightauthadvancedv1.30+

Login once for all tests with auth.setup.ts

Use a global setup project to authenticate once before the entire suite — the standard pattern for large test suites.

1// ── playwright.config.ts ─────────────────────────────────────
2import { defineConfig } from '@playwright/test';
3
4export default defineConfig({
5  projects: [
6    {
7      name: 'setup',
8      testMatch: /auth\.setup\.ts/, // runs this file before all others
9    },
10    {
11      name: 'chromium',
12      use: { storageState: 'playwright/.auth/user.json' },
13      dependencies: ['setup'], // waits for setup to finish first
14    },
15  ],
16});
17
18// ── auth.setup.ts ─────────────────────────────────────────────
19import { test as setup } from '@playwright/test';
20
21setup('authenticate once for the whole suite', async ({ page }) => {
22  await page.goto('/login');
23  await page.getByLabel('Email').fill('user@test.com');
24  await page.getByLabel('Password').fill('secret');
25  await page.getByRole('button', { name: 'Sign in' }).click();
26  await page.waitForURL('/dashboard');
27
28  // Write auth state — all chromium tests will load this automatically
29  await page.context().storageState({ path: 'playwright/.auth/user.json' });
30});
#auth.setup#global setup#dependencies#storageState#project