Skip to main content
QATraining
All Prompts
intermediate

Playwright Multi-Role Auth State Management

Eliminate redundant login steps across your test suite. Generates global-setup.ts, per-role storageState files, and playwright.config.ts project config for admin, editor, and viewer roles.

Prompt Template

You are a Playwright expert showing how to remove redundant login from every test.

Generate a complete authentication state management setup for {{appName}} with these roles: {{authRoles}} (e.g. admin, editor, viewer).

Auth method: {{authMethod}} (session cookies / JWT / OAuth PKCE)
Login URL: {{loginUrl}}
Post-login redirect: {{successUrl}}

Files to generate:

**1. global-setup.ts**
- Logs in once per role using the Playwright API
- Saves storage state to auth/{{role}}.json
- Verifies the redirect confirms successful auth before saving

**2. playwright.config.ts**
- One project per role, each using savedState from auth/{{role}}.json
- Setup project that runs global-setup.ts before anything else
- Sensible defaults: retries, workers, trace on first retry

**3. fixtures.ts**
- Re-usable fixture that surfaces the correct page for the current project's role
- Automatic session refresh: detects 401/403 mid-test and re-authenticates

**4. CI instructions**
- GitHub Actions job: "generate-auth" runs before "test" job
- Auth files cached between runs using actions/cache keyed on package-lock hash
- .gitignore entries for auth/*.json

**5. Example spec**
- One test per role that asserts role-specific UI is visible
- Shows how to use the authenticated fixture in a test

Include comments explaining why storageState is faster than programmatic login per test.
Tags
authentication
storage-state
roles
playwright
ci