Speed Up Your Tests with storageState()
Little Known Tip to Get Your Test Running Faster
Tired of logging in before every test? Playwright’s storageState()
lets you save your login state and reuse it across multiple tests - boosting performance and reliability.
Why storageState()
Matters
Every time your test logs in, you're spending time and creating potential points of failure. Also it adds additional overhead on your tests - that may impact the overall test time. Instead, you can log in once, save the state (cookies + local storage), and load that state in future tests.
Step 1: Save the Auth State
Create a setup script that logs in and saves the storage state.
// setup-auth.ts
import { chromium } from '@playwright/test';
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com/login');
await page.fill('#username', 'your-username');
await page.fill('#password', 'your-password');
await page.click('button[type="submit"]');
// Wait for redirect or element that confirms login
await page.waitForSelector('#dashboard');
// Save storage state to file
await context.storageState({ path: 'auth.json' });
await browser.close();
})();
Step 2: Reuse the Auth State
In your test file, load the previously saved auth.json
file to reuse the session.
// example.spec.ts
import { test, expect } from '@playwright/test';
test.use({
storageState: 'auth.json'
});
test('Dashboard loads for authenticated user', async ({ page }) => {
await page.goto('https://example.com/dashboard');
await expect(page.locator('#dashboard')).toBeVisible();
});
When Should You Use This?
- Tests that require an authenticated user
- Reducing login redundancy in CI/CD pipelines
- Speeding up test suites with shared session state
Start using storageState()
today and take the fast lane through your E2E tests!