Skip to content

Playwright Storage State Not Persisting Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Playwright Storage State Not Persisting Fix. We cover key concepts, practical examples, and best practices.

Your saved storage state doesn't work — tests that use storageState aren't authenticated, or the state file fails to load.

The Problem

// WRONG — storageState path is incorrect
import { test } from '@playwright/test';

test.use({ storageState: 'auth.json' });
// This looks for auth.json relative to the config file, not the test file
Error: ENOENT: no such file or directory, open 'auth.json'

The storage state file path is resolved relative to the working directory, not the test file. A relative path that works in one directory fails in another.

Step-by-Step Fix

1. Save storage state after login

// RIGHT — save state to a known location
import { test } from '@playwright/test';
import path from 'path';

const AUTH_FILE = path.join(__dirname, '../auth.json');

test('login and save state', async ({ page }) => {
  await page.goto('https://example.com/login');
  await page.fill('#email', 'user@example.com');
  await page.fill('#password', 'secret123');
  await page.click('button[type="submit"]');

  // Wait for navigation after login
  await page.waitForURL('**/dashboard');

  // Save cookies and localStorage
  await page.context().storageState({ path: AUTH_FILE });
});

2. Load storage state in tests

// RIGHT — use absolute path for reliability
import { test } from '@playwright/test';
import path from 'path';

const AUTH_FILE = path.join(__dirname, '../auth.json');

test.use({ storageState: AUTH_FILE });

test('dashboard is accessible', async ({ page }) => {
  await page.goto('https://example.com/dashboard');
  // Already authenticated from storage state
});

3. Configure in playwright.config.ts

// RIGHT — global storage state configuration
import { defineConfig } from '@playwright/test';

export default defineConfig({
  use: {
    // Relative to config file
    storageState: './e2e/auth.json',
  },
  projects: [
    {
      name: 'authenticated',
      use: { storageState: './e2e/auth.json' },
    },
    {
      name: 'anonymous',
      use: { storageState: undefined },
    },
  ],
});

4. Handle storage state expiration

// RIGHT — check if state exists, re-login if needed
import { test } from '@playwright/test';
import fs from 'fs';

test.beforeAll(async ({ browser }) => {
  if (!fs.existsSync(AUTH_FILE)) {
    const context = await browser.newContext();
    const page = await context.newPage();
    // Perform login flow
    await page.context().storageState({ path: AUTH_FILE });
    await context.close();
  }
});

Expected output:

✓ login and save state (2.1s)
✓ dashboard is accessible (1.0s)
✓ another authenticated test (0.9s)

Prevention Tips

  • Use absolute paths for storage state files
  • Set storageState in <a href="/testing-qa/playwright/">playwright</a>.config.ts for global use
  • Create separate projects for authenticated/anonymous scenarios
  • Generate storage state script as a setup step
  • Handle expired storage state gracefully

Common Mistakes with fixture storage

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What data does storageState save?

Cookies, localStorage, and sessionStorage for all origins. It does NOT save IndexedDB, WebSQL, or cache storage. Use the storageState option to load this data in new contexts.

How often should I regenerate storage state?

Regenerate whenever your authentication method changes or the auth token expires. Many teams regenerate storage state in CI on every run using a setup project.

Can I manually edit the storageState JSON file?

Yes. The file is a JSON array of cookies and origins with localStorage data. You can add or modify entries, but it's easier to regenerate the file by running the login flow.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro