← Back to library
Playwrightauthadvancedv1.20+
Save auth state to file after login
Persist cookies and localStorage to a JSON file after login so other tests can skip the UI flow.
1test('logs in and saves session state to disk', async ({ page }) => {
2 await page.goto('/login');
3 await page.getByLabel('Email').fill('user@test.com');
4 await page.getByLabel('Password').fill('secret');
5 await page.getByRole('button', { name: 'Sign in' }).click();
6 await page.waitForURL('/dashboard');
7
8 // Saves cookies + localStorage to a JSON file.
9 // Other test files can load this file and skip the login UI entirely.
10 await page.context().storageState({ path: 'playwright/.auth/user.json' });
11});#storageState#save auth#login#persist session