Skip to content
SDETRole
SDETRole

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 with page.context().storageState({ path }).
  • In playwright.config.ts, add a setup project (matched with testMatch) and make the chromium project depend on it with dependencies.
  • Load the saved state with use: { storageState: ... } on the chromium project.
  • Remove the beforeEach login 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.

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →