Code Reference

Auth Storage (JavaScript)

Playwright · Example / how-to

Playwright · Example / how-to


📋 Overview

Log in once via UI or API, save storageState, reuse across tests for authenticated browser sessions.

🔧 Core concepts

PieceRole
Setup projectRuns before dependent tests
storageState pathCookies + localStorage
Env secretsTest user credentials

💡 Examples

UI login setup:

// auth.setup.ts
import { test as setup, expect } from '@playwright/test';

const authFile = 'playwright/.auth/user.json';

setup('authenticate', async ({ page }) => {
  await page.goto('/login');
  await page.getByLabel('Email').fill(process.env.TEST_EMAIL!);
  await page.getByLabel('Password').fill(process.env.TEST_PASS!);
  await page.getByRole('button', { name: 'Sign in' }).click();
  await expect(page).toHaveURL(/dashboard/);
  await page.context().storageState({ path: authFile });
});

API login → inject into context:

setup('api auth', async ({ request }) => {
  const res = await request.post('/api/auth/login', {
    data: {
      email: process.env.TEST_EMAIL,
      password: process.env.TEST_PASS,
    },
  });
  expect(res.ok()).toBeTruthy();
  // If API sets cookies, persist them:
  await request.storageState({ path: 'playwright/.auth/user.json' });
});

Config:

// playwright.config.ts (sketch)
projects: [
  { name: 'setup', testMatch: /.*\.setup\.ts/ },
  {
    name: 'chromium',
    dependencies: ['setup'],
    use: { storageState: 'playwright/.auth/user.json' },
  },
]

⚠️ Pitfalls

  • Gitignore auth JSON files.
  • Refresh state when sessions expire in long CI jobs.

On this page