Authentication in Playwright Tests

Almost every meaningful end-to-end test needs a logged-in user. Logging in through the UI in every test is slow (seconds per test, multiplied across hundreds of tests) and fragile (one login-page change breaks everything). Playwright solves this by authenticating once in a setup step, saving the browser's storage state (cookies and localStorage) to a file, and starting every test with that state already loaded.

Real applications add complications: several roles (admin, member, viewer), tests that modify user settings, SSO providers, MFA, and short-lived tokens. The patterns below scale from a simple app to complex multi-role suites, while keeping tests isolated and credentials secure.

TL;DR

Quick Example

Core Concepts

Storage State

context.storageState({ path }) serializes cookies and localStorage (and optionally IndexedDB) for all origins to JSON. Loading it via use: { storageState: path } makes new contexts start with the same session, so the app sees an authenticated user immediately. Session storage isn't included; apps that keep tokens only in sessionStorage need an init script to restore them.

Setup Projects and Dependencies

A setup project is a normal Playwright project (matching *.setup.ts) that other projects list in dependencies. Playwright runs it first, and shows it in reports and traces. It's preferred over the older globalSetup because it supports fixtures, tracing, retries, and the HTML report.

Multiple Roles

Common approaches:

Accounts and Isolation

Sharing one account across parallel tests is fine for read-only tests. Tests that change account state (settings, cart, subscriptions) conflict when run in parallel. Options:

API-Based Login

If your app supports it, authenticating via an HTTP call (request.post('/api/login')) and saving request.storageState() is faster and doesn't depend on the login UI. Keep one UI login test that covers the actual login page.

SSO, OAuth, and MFA

Third-party identity providers (SSO, OAuth) complicate automated login:

Security Considerations

Best Practices

Authenticate Once per Run, Not per Test

Setup projects plus storage state typically cut suite time dramatically, and remove the login page as a single point of failure for every test.

Keep a Dedicated Login Test

Because most tests bypass the login UI, have explicit tests for the login page itself: success, wrong password, lockout, and password reset.

Match Account Strategy to Test Behavior

Read-only tests can share an account, and mutating tests need isolated accounts. Design fixtures so tests declare which they need.

Verify Login Succeeded in Setup

Assert a post-login indicator (an account menu or a user name) before saving state. Saving state after a failed login makes every test fail confusingly.

Common Mistakes

Committing Storage State Files

playwright/.auth/*.json contains valid session cookies. Committing them leaks access to test environments. Gitignore them.

Parallel Tests Mutating a Shared Account

Two workers changing the same user's preferences or cart produce intermittent failures that look like application bugs. Use per-worker or per-test accounts.

Logging In Through the UI in beforeEach

It multiplies login time across every test, and makes the whole suite fail whenever the login page changes. Use setup projects and storage state.

FAQ

How do I avoid logging in before every Playwright test?

Log in once in a setup project, save storageState to a file, and configure dependent projects to load it with use: { storageState: 'path' }. Every test then starts authenticated, in its own isolated context.

How do I test with multiple user roles?

Save a storage state file per role in setup, and select the right one per project, test file, or test.use block. For scenarios involving several users at once, create multiple browser contexts in one test, each with its own storage state.

Can Playwright handle SSO or MFA logins?

It can automate them in test environments (with test IdP accounts, known TOTP secrets, or test-only login endpoints), but you should avoid automating production MFA or CAPTCHAs. Many teams configure identity providers with dedicated test users and relaxed MFA in staging.

Is storageState secure?

The files contain live session cookies and tokens, so treat them as secrets: gitignore them, avoid uploading them as artifacts, and use test-environment accounts with limited privileges.

Related Topics

References