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.
Add the setup script as a project dependency in `playwright.config.ts` using the `dependencies` field so it runs before other test projects.
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.
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.
Add `playwright/.auth/` to `.gitignore` so credentials are never committed to source control.
Known gotchas
Storage state captures cookies and localStorage but not sessionStorage — if your app stores auth tokens only in sessionStorage, the saved state will not restore the session and tests will see a logged-out state.
JWTs and session cookies have expiry times. If CI caches the storage-state file across pipeline runs, it can become stale. Either regenerate on every run or add a step that checks for expiry.
If the login page sets cookies on a redirect chain across subdomains, ensure the setup browser context visits all relevant origins before calling `storageState`, otherwise some cookies may be missing from the saved file.
Give your agent this knowledge — and 200+ more routes
One MCP install gives any agent live access to the full route map, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp