Reusing a signed-in session across tests

Why not sign in in every test?

Signing in through the UI is slow, and it tests the same screen again and again. Most suites sign in once, save the browser's state to a file, and start every other test from that file, already signed in.

Step 1: a setup test that signs in and saves the state

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

setup('sign in', async ({ page }) => {
  await page.goto('/auth/sign-in');
  await page.getByLabel('Username').fill('TedBuckland');
  await page.getByLabel('Password').fill('Alligator3');
  await page.getByLabel('Remember me').check();
  await page.getByRole('button', { name: 'Sign in' }).click();

  // Signing in takes a moment; the assertion waits for it.
  await expect(page.getByRole('status')).toHaveText('Signed in as TedBuckland');
  await page.context().storageState({ path: 'playwright/.auth/user.json' });
});

Step 2: tests that start from that file

// playwright.config.ts
projects: [
  { name: 'auth', testMatch: /auth\.setup\.ts/ },
  {
    name: 'signed-in',
    use: { storageState: 'playwright/.auth/user.json' },
    dependencies: ['auth'],
  },
],

Or for a single file: test.use({ storageState: 'playwright/.auth/user.json' }). A test that starts from the file opens this page and sees the account straight away.

The "Remember me" trap

storageState() saves cookies and localStorage, but not sessionStorage. On this page, an unchecked "Remember me" keeps the session in sessionStorage, so the saved file is empty and every "signed-in" test sees the sign-in form instead. Real apps do this too. Check what your app uses before you rely on storageState().

Add playwright/.auth to .gitignore. The file holds a real session token.
QA Playground

Sign in to your account

Demo Credentials:

Login: TedBuckland

Password: Alligator3