Skip to main content
QA Training
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 senior Playwright automation engineer 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

How to use this prompt

  1. 1Copy the prompt template using the Copy button above.
  2. 2Open your preferred AI assistant (ChatGPT, Claude, Gemini, Copilot, etc.).
  3. 3Paste the prompt and replace the {{placeholder}} variables with your project-specific values.
  4. 4Review the output - treat it as a professional first draft, not a final deliverable.
  5. 5Iterate: refine the output by asking follow-up questions or providing more context.

Frequently asked questions

Which AI models work best with this prompt?v

This prompt works with any capable language model including ChatGPT (GPT-4o), Claude (3.5+), Gemini (1.5 Pro+), and GitHub Copilot Chat. More capable models produce more complete and accurate outputs.

How do I adapt the prompt for a different framework?v

Replace the framework placeholder with your target framework and update any framework-specific syntax references in the output. The structure and logic of the generated tests will transfer across frameworks.

Can I use this prompt commercially?v

Yes. All prompts on QATraining.uk are free to use for personal and commercial QA work. Attribution is appreciated but not required.