API …
Playwright lab

Auth & storage state

Log in once, save storageState and reuse it. Tokens in localStorage, role cookies and expiring sessions.

Practice tasks

  • Log in through the UI with practice@testyard.dev / SecretPass1! and save context.storageState({ path }).
  • Create a second context with storageState and assert you land logged in without touching the form.
  • Skip the UI: call request.post('/api/auth/login') and inject ty.token and ty.user with addInitScript or storageState origins.
  • Switch the role to admin and assert the admin panel appears only for admin + logged-in users.
  • Read cookies with context.cookies() and add the ty_role cookie with context.addCookies().
  • Press “Expire session in 3 s” and assert the app logs the user out (toHaveText on the status).
  • Build an auth.setup.ts project dependency that writes .auth/user.json for every other project.

More Playwright lab practice