October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Reuse Authentication State in Playwright E2E Tests

Authenticate once, save Playwright browser state, and reuse it across tests. Learn when to share an account, when to isolate workers, and how to protect state files.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To avoid logging in through the UI before every Playwright test, authenticate in a setup project, save the resulting browser state, and load it with storageState in the tests that need it. Use one shared account only when tests can safely run against the same server-side data; for tests that change shared data, use a separate account and saved state for each worker.

Choose an authentication strategy that fits your tests

The key decision is whether tests can safely share an account. Reusing one login state reduces setup work, but it does not isolate server-side data. If parallel tests can change the same account’s data, account collisions can cause flaky results even when each browser context has its own cookies.

As an Amazon Associate I earn from qualifying purchases.

Test situation Recommended approach Why
Tests are independent and do not interfere through shared account data Authenticate once in a setup project and reuse its saved state Avoids repeating the login flow for each test.
Tests mutate shared server-side data Use a distinct account and saved state per parallel worker Reduces interference between concurrent tests.
The app provides a suitable, simpler or faster authentication API Authenticate through the API and save its storage state Skips UI login setup while keeping the tested features covered by browser E2E tests.

Playwright Test runs tests in worker processes. By default, test files can run in parallel, while tests in one file run in order in the same worker. Separate parallel tests do not share browser state or global variables, but they can still affect the same server-side account data. See Playwright TestConfig and Playwright Test.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reuse one account with a setup project

For independent tests, use a setup project to sign in once and save the resulting state. Make the browser projects depend on setup and configure them to load the saved file with storageState. Playwright’s authentication guide shows this pattern with Chromium and Firefox projects: Authentication.

Do not save state merely because the login button was clicked. Wait until the application has completed authentication—for example, until the expected final URL appears or a stable signed-in UI element is visible. This matters when cookies are established during a redirect; saving too early can produce a state file that looks valid but is not authenticated.

Project dependencies run before the projects that depend on them. After setup succeeds, dependent browser projects can run in parallel, subject to the configured worker limit. If setup fails, dependent projects do not run. Details are in Playwright projects.

Use a separate account for each worker when tests mutate data

If tests create, edit, or delete shared server-side records, a single account can make parallel tests race against one another. Playwright recommends one account per parallel worker for this case. The documented pattern overrides the storageState fixture with a worker-scoped fixture, uses test.info().parallelIndex to identify the worker, authenticates in a clean context, saves a worker-specific state file, and reuses it for that worker’s tests. See Playwright authentication.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Worker-specific accounts protect tests running within one run, but concurrent local and CI runs also need distinct accounts. Otherwise, two independent runs may still modify the same account’s data. Provision accounts or allocate them so simultaneous runs cannot collide.

Authenticate through an API when the app supports it

If the application offers an authentication API that is simpler or faster than its UI login flow, use an API request context to authenticate and save the resulting storage state. Browser tests can then load that state and exercise authenticated features without repeating the login UI. This approach depends on the application’s supported authentication flow; there is no universal endpoint or exchange to assume. The official pattern is documented in Playwright’s authentication guide.

Choose project dependencies or globalSetup

For most authentication setup, project dependencies integrate the work into Playwright Test’s normal runner lifecycle. They can use fixtures and browser management, and setup appears in the HTML report and can produce traces. Playwright recommends this approach for global setup actions. See Global setup and teardown.

globalSetup is still available and can authenticate once, write a state file, and pass data to tests. The trade-off is that it does not provide the same project-dependency features, including setup visibility in reports, traces, fixtures, and standard setup parallelism and retry behavior. Use it when its simpler lifecycle fits your needs rather than expecting those runner integrations.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle roles, browser state, and expiration

Different roles across tests

When tests use different roles and each role can reuse an account, create a state file per role and select the appropriate file with test.use({ storageState: ... }) for the relevant file or describe block. If a single test needs two signed-in roles at once, create two browser contexts with their respective states, use a separate page in each, and close both contexts when done.

Know what saved state includes

Playwright’s standard saved state covers cookies, local storage, IndexedDB, and passkey (WebAuthn)-based authentication. It does not persist sessionStorage. If the app relies on session storage, the authentication guide demonstrates saving it separately and injecting it with an init script for the target hostname: Authentication.

Protect and refresh state files

Saved browser state may contain cookies and headers that allow someone to impersonate a test account. Playwright recommends creating a playwright/.auth directory and adding it to .gitignore. Never commit state files. Delete or regenerate state when authentication expires. If the file only needs to exist for one run, save it under testProject.outputDir, which Playwright cleans before each run.

UI mode does not run the setup project by default, to improve speed. When stored credentials have expired, run the authentication setup manually as described in the authentication guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Implementation checklist

  1. Decide whether concurrent tests can safely share account data. Use one account for independent tests; isolate accounts for tests that mutate shared data.
  2. Choose UI or API authentication. Use an API flow only if the application supports one that produces suitable browser state.
  3. Authenticate in a setup project, or create worker-scoped authentication when each worker needs its own account.
  4. Wait for a final redirect or stable signed-in UI condition before saving state.
  5. Configure relevant browser projects or tests to load the state with storageState.
  6. Use a separate state file per reusable role, or separate contexts when a test needs multiple roles simultaneously.
  7. Keep state files out of source control, account for expiration, and handle sessionStorage separately if the app uses it.
  8. Use project dependencies when setup should participate in reports, traces, fixtures, and normal runner behavior; choose globalSetup only when its simpler lifecycle is a better fit.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.