Persist browser authentication state to disk and reuse it across Playwright test runs to skip repeated login flows

domain: playwright.dev · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Create a dedicated setup script (e.g., `auth.setup.ts`) that logs in once through the full UI, then calls `await page.context().storageState({ path: 'playwright/.auth/user.json' })` to save cookies and localStorage to a file.
  2. Add the setup script as a project dependency in `playwright.config.ts` using the `dependencies` field so it runs before other test projects.
  3. In the test project that should reuse auth, set `use: { storageState: 'playwright/.auth/user.json' }` — every browser context in that project will be initialised with the saved state.
  4. For multi-role setups (admin, regular user), create one setup script and one storage-state file per role, then define separate projects each pointing to the correct file.
  5. Add `playwright/.auth/` to `.gitignore` so credentials are never committed to source control.

Known gotchas

Related routes

log in once via playwright's api request context and reuse its storagestate in a browser context to skip ui login
playwright.dev · 5 steps · unrated
Decide between Playwright's launchPersistentContext(userDataDir) and storageState() for reusing authenticated sessions across test runs
playwright.dev · 5 steps · unrated
Launch a Playwright persistent browser context with launchPersistentContext and userDataDir to reuse a browser profile
playwright.dev · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans