Sign In & Sessions
Sign in to see your account. The session is stored in the browser, so it survives a reload, and with "Remember me" it also reaches other tabs.
Why not sign in in every test?
Signing in through the UI is slow, and it tests the same screen again and again. Most suites sign in once, save the browser's state to a file, and start every other test from that file, already signed in.
Step 1: a setup test that signs in and saves the state
// tests/auth.setup.ts
import { expect, test as setup } from '@playwright/test';
setup('sign in', async ({ page }) => {
await page.goto('/auth/sign-in');
await page.getByLabel('Username').fill('TedBuckland');
await page.getByLabel('Password').fill('Alligator3');
await page.getByLabel('Remember me').check();
await page.getByRole('button', { name: 'Sign in' }).click();
// Signing in takes a moment; the assertion waits for it.
await expect(page.getByRole('status')).toHaveText('Signed in as TedBuckland');
await page.context().storageState({ path: 'playwright/.auth/user.json' });
});Step 2: tests that start from that file
// playwright.config.ts
projects: [
{ name: 'auth', testMatch: /auth\.setup\.ts/ },
{
name: 'signed-in',
use: { storageState: 'playwright/.auth/user.json' },
dependencies: ['auth'],
},
],Or for a single file: test.use({ storageState: 'playwright/.auth/user.json' }). A test that starts from the file opens this page and sees the account straight away.
The "Remember me" trap
storageState() saves cookies and localStorage, but not sessionStorage. On this page, an unchecked "Remember me" keeps the session in sessionStorage, so the saved file is empty and every "signed-in" test sees the sign-in form instead. Real apps do this too. Check what your app uses before you rely on storageState().
playwright/.auth to .gitignore. The file holds a real session token.Sign in to your account
Demo Credentials:
Login: TedBuckland
Password: Alligator3
Practising form validation messages instead? Use the Login Form exercise.
Related exercises
- FormsBeginnerLogin FormCover every path of a login form: required fields, wrong and right passwords, the password toggle and the lockout. Sessions are covered separately under Authentication.
- Mini ShopAdvancedPage Objects & FixturesTurn a long test into a maintainable suite with Page Objects and custom fixtures.
- Dialogs & WindowsAdvancedTabs & WindowsWork with several pages at once: new tabs, popups, a sign-in window and tabs that share storage.
Read the guide: Playwright authentication: sign in once with storageState