Playwright Storage State Login
A festival wristband works like a pass. A guest shows an ID once at the gate, receives the band, and walks into every stage without further checks. Storage state brings that convenience to tests. A test logs in one time, saves the session to a file, and every other test loads the saved session instead of typing credentials again.
The Cost of Logging In Every Time
A login takes several seconds. A suite of two hundred tests that each log in wastes more than ten minutes. Login pages face rate limits and security checks, which makes repeated logins risky. A saved session removes both problems.
The Login Once Flow
Setup Step Tests
+--------------------+ +-------------------+
| Open login page | | Load user.json |
| Type credentials | | Start already |
| Click Sign in | saves --> | signed in |
| Save session file | user.json | Run the scenario |
+--------------------+ +-------------------+
runs once runs many times
Saving the Session
A setup file performs the login and calls storageState to write cookies and local storage into a JSON file.
import { test as setup, expect } from '@playwright/test';
const authFile = 'playwright/.auth/user.json';
setup('authenticate', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill(process.env.USER_EMAIL!);
await page.getByLabel('Password').fill(process.env.USER_PASSWORD!);
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page).toHaveURL(/dashboard/);
await page.context().storageState({ path: authFile });
});
Wiring the Setup Into the Config
A setup project runs before the browser projects. The dependencies field enforces the order.
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
projects: [
{ name: 'setup', testMatch: /auth\.setup\.ts/ },
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
storageState: 'playwright/.auth/user.json',
},
dependencies: ['setup'],
},
],
});
Writing Tests After Login
Tests need no login code at all. Each one begins on a signed-in page.
test('dashboard shows the user name', async ({ page }) => {
await page.goto('/dashboard');
await expect(page.getByTestId('welcome')).toContainText('Asha');
});
Several Roles
Teams often test an admin and a regular user. Each role gets a separate file, such as admin.json and user.json. A test file picks the role it needs.
test.use({ storageState: 'playwright/.auth/admin.json' });
Protecting Credentials
A saved session file works like a key to the account. Treat it with the same care as a password.
- Add
playwright/.authto the.gitignorefile. - Store real credentials in environment variables or CI secrets.
- Use dedicated test accounts, never personal ones.
A Real-World Scenario
A project management tool has 150 tests behind a login. Each login took six seconds, which added fifteen minutes to every run. The team moved the login into one setup step and loaded the saved session in all tests. The suite lost fourteen minutes and gained stability.
Quick Practice Task
Build a setup file for any practice site that offers a demo account. Save the session, write one test that visits a members-only page without logging in, and confirm the test passes.
Common Questions
What happens when the saved session expires?
Tests start on a login page and fail. Rerun the setup project to create a fresh file, and schedule it to run at the start of every CI job.
Can tests change the saved file?
They can, though shared files invite conflicts between parallel tests. Treat the file as read-only after setup.
Is storage state safe to share?
The file holds live session tokens. Keep it on the local machine or inside the CI job, and never upload it as a public artifact.
Key Takeaways
- Storage state saves cookies and local storage in a file.
- A setup project logs in once and every test reuses the result.
- Separate files serve separate user roles.
- Session files belong in
.gitignore, never in shared code.
