THE REPLACEQA BLOG
Playwright auth state: cookies, sessionStorage and parallel workers
Diagnose missing authenticated state, restore sessionStorage by origin when needed, and keep account sharing from corrupting parallel tests.
If reusing Playwright authentication state returns you to an organization picker or login page, first identify where the application stores that state. Saving cookies and local storage does not automatically save sessionStorage, and a saved browser state does not create an independent backend account for each worker.
A Reddit question about sessionStorage across workers and subdomains describes that exact gap. The question is useful because it separates being authenticated from having selected the right tenant. Technical sources here were checked on November 18, 2026; the code is an adaptation pattern, not a login integration for an unspecified application.
Inventory the state before writing a workaround
After a successful login in a controlled test account, record which storage mechanism contains each prerequisite. Avoid copying values into logs or tickets.
| State | Example purpose | Check before reuse |
|---|---|---|
| Cookie | Server session identity | Correct origin, expiry and server validity |
| Local storage | Client preference or token | Correct origin and expected key |
| IndexedDB | Client-managed authentication data | Whether capture includes it in your Playwright version |
| Session storage | Selected organization or tab-scoped token | Capture and restore separately if required |
| Backend account | Permissions, workspace membership | Worker isolation and current authorization |
The authentication guide documents reusable state and a session-storage workaround. The storageState API documents available capture options, including indexedDB: true when that storage contains the authentication information you need. Check the API for your installed version rather than assuming all state is included by default.
Wait for the final login state before saving. Seeing the initial redirect is weaker than reaching the authenticated page with the expected tenant. Keep at least one separate test that actually exercises login; preloaded state intentionally skips that journey.
Capture only the origin you intend to restore
In a hypothetical app, perform its normal login and organization selection first. Then save ordinary state and a separate session-storage record:
import { mkdir, writeFile } from 'node:fs/promises';
await mkdir('playwright/.auth', { recursive: true });
await page.context().storageState({
path: 'playwright/.auth/user.json',
indexedDB: true,
});
const session = await page.evaluate(() => ({
origin: location.origin,
values: Object.fromEntries(
Object.keys(sessionStorage).map(key => [key, sessionStorage.getItem(key)!]),
),
}));
await writeFile('playwright/.auth/session.json', JSON.stringify(session));
This is a fragment for an authenticated setup routine. Treat both files as credentials: exclude the directory from Git and from ordinary test-report uploads. Generate fresh state when its account, permissions or validity changes.
MDN's sessionStorage reference explains its origin and tab scoping. In particular, session storage survives reloads within a page session; it is not simply erased on every navigation. That matters when reasoning about a restore script.
Restore before application code reads it
Create a fresh browser context from the saved ordinary state. Before navigating its first page, register an initialization script for the exact origin:
import { readFile } from 'node:fs/promises';
const saved = JSON.parse(await readFile('playwright/.auth/session.json', 'utf8'));
const context = await browser.newContext({ storageState: 'playwright/.auth/user.json' });
await context.addInitScript(({ origin, values }) => {
if (location.origin !== origin) return;
for (const [key, value] of Object.entries(values)) {
sessionStorage.setItem(key, String(value));
}
}, saved);
const page = await context.newPage();
try {
await page.goto(saved.origin + '/workspace');
await expect(page.getByRole('heading', { name: 'Test workspace', exact: true }))
.toBeVisible();
} finally {
await context.close();
}
Replace the workspace route and heading with your contract. For multiple origins, capture each required origin separately and restore only the matching entry. Do not inject one origin's token or tenant selection into every frame or subdomain.
There is an important limit: an initialization script can run again on later document navigations. This unconditional restoration pattern is therefore suitable only for tests whose starting authenticated state is intentionally fixed. It can reintroduce state after logout or tenant clearing. Use a separate context without the restore script for those transitions, and test them through the real UI. A guard that restores when storage is empty is not a complete fix: empty storage may be the intended result of logout.
Separate browser isolation from account isolation
Two fresh contexts can still authenticate as the same user and edit the same server records. If one test changes that account's organization or permissions, another test may fail despite perfect browser isolation.
Use dedicated accounts or owned records for mutating tests. A worker account pool needs a namespace across concurrent CI jobs too; worker number zero in one job is unrelated to worker number zero in another. Read-only tests may share an account when their operations truly do not change server state.
Verify the setup with three checks: a fresh context reaches the intended workspace, another origin receives none of the restored keys, and the independent logout test stays logged out after reload. Preserve the reason for each prerequisite in the manual-to-Playwright test contract.
These are standalone authenticated-testing techniques. ReplaceQA's current closed beta focuses on public journeys; the small-team testing strategy helps assign authenticated coverage separately.