Testing / Playwright Interview questions
How do you handle authentication state across tests in Playwright?
Rather than logging in through the UI at the start of every test - which is slow and repeated hundreds of times - Playwright supports saving authenticated browser state once and reusing it.
// auth.setup.ts - runs once await page.goto('/login'); await page.getByLabel('Email').fill('user@example.com'); await page.getByRole('button', { name: 'Log in' }).click(); await page.context().storageState({ path: 'state.json' });
// playwright.config.ts use: { storageState: 'state.json' }
storageState() captures cookies and local storage into a JSON file; pointing the config's use.storageState at that file makes every subsequent test start already logged in. Playwright's project dependencies feature is typically used to run this setup project once before the tests that depend on it.
More Related questions...