Log in once with storageState
advanced Authentication Challenge 14 of 22
Every test in the suite logs in through the UI in a beforeEach, which adds several seconds per test and hard-codes a password. Rework it to log in once and reuse the session.
The editor holds three files, separated by // ---- file ---- comments.
Requirements
- Create
tests/auth.setup.ts, which logs in and saves the session withpage.context().storageState({ path }). - In
playwright.config.ts, add asetupproject (matched withtestMatch) and make thechromiumproject depend on it withdependencies. - Load the saved state with
use: { storageState: ... }on thechromiumproject. - Remove the
beforeEachlogin from the spec. - Read the email and password from
process.env.
Expected output
A `setup` project logs in once and saves cookies and local storage with `storageState({ path })`. The `chromium` project depends on `setup` and loads that file through `use.storageState`, so every test starts already signed in. Credentials come from environment variables.
Checks (6)
- ○Saves the session with storageState({ path })
- ○Reuses the saved state through use.storageState
- ○The browser project depends on the setup project
- ○Defines a setup project with testMatch
- ○Reads credentials from environment variables
- ○No per-test UI login in beforeEach
example.spec.tsValidated · not executed
Ctrl/⌘ + Enter to validate · your code is saved in this browser
Results
Validate your code against the checks.