Stored Authentication State: Playwright saves browser cookies and session data after initial login, allowing tests to reuse authentication without repeated login flows.
- Speed: Avoids 10+ seconds of GOV.UK One Login flow per test
- Parallel execution: Each worker uses separate authenticated session
- Reliability: Reduces authentication-related test failures
- Performance: 34-38% speed improvement, up to 71% with parallelization
- Setup project authenticates each test account and saves session cookies to
playwright/auth-states/user-N.json - Worker correlation system creates
worker-email-map.jsonmapping workers to authenticated user emails - Test workers load their assigned session file (user-0.json, user-1.json, etc.) with context-stored email info
- Tests run immediately with authenticated sessions - no login required
- Authentication recovery automatically detects and fixes broken sessions when LandingPage methods call
reAuthenticate()utility - Sessions expire after ~30 minutes of inactivity
- Flexible: Run setup once, reuse authentication for multiple test runs
- Automatic recovery: Authentication issues are handled transparently
- Efficient debugging: No re-authentication required between test runs
- Automatic isolation: Each test project runs fresh setup
- Workflow: setup → functional tests → setup → non-functional tests
- No conflicts: Authentication recovery prevents state conflicts between test suites
Each test account must:
- Be a complete GOV.UK One Login account
- Have MFA setup completed (SMS or authenticator app)
- Be registered in the target application
- Have logged into the application at least once manually
File: tests/config/test-accounts.json
{
"accounts": [
{
"email": "TEST_ACCOUNT_1_EMAIL",
"password": "TEST_ACCOUNT_1_PASSWORD",
"accountName": "TEST_ACCOUNT_1_NAME",
"description": "Primary test account - Worker 0"
},
{
"email": "TEST_ACCOUNT_2_EMAIL",
"password": "TEST_ACCOUNT_2_PASSWORD",
"accountName": "TEST_ACCOUNT_2_NAME",
"description": "Secondary test account - Worker 1"
}
]
}It is expected that each account has access to the following councils or test 'Verify councils list' needs to be updated with configured councils for the user accounts.
- LONDON BOROUGH OF BARNET
- LONDON BOROUGH OF BEXLEY Council configuration for LA users can be done in Salesforce.
File: .env (create in project root)
TEST_ACCOUNT_1_NAME=test user 1
TEST_ACCOUNT_1_EMAIL=user1@example.com
TEST_ACCOUNT_1_PASSWORD=Password123!
TEST_ACCOUNT_2_NAME=test user 2
TEST_ACCOUNT_2_EMAIL=user2@example.com
TEST_ACCOUNT_2_PASSWORD=Password456!# Creates auth files: playwright/auth-states/user-0.json, user-1.json, etc.
npx playwright test --project=setup# Delete auth files manually if needed (authentication recovery handles most issues)
Remove-Item "playwright\auth-states\user-*.json"When manual cleanup might be needed:
- When switching between test environments
- When debugging authentication setup issues
- When test account credentials change
Setup fails:
- Verify test accounts have completed MFA setup
- Manually log into each account once to confirm they work
Session expired:
- Auto-recovery handles this - Authentication recovery will automatically re-authenticate
- Auth files are valid for ~30 minutes of inactivity
- Manual refresh if needed:
npx playwright test --project=setup
Missing auth files:
- Ensure worker count matches number of test accounts
- Run setup to create all required auth files
Parallel execution issues:
- Each worker needs its own test account
- Accounts cannot be shared between parallel workers
"description": "Additional test account - Worker 2"
}
-
Add the credentials to
.envlocally:TEST_ACCOUNT_3_NAME=test user3 TEST_ACCOUNT_3_EMAIL=test3@example.com TEST_ACCOUNT_3_PASSWORD=YourActualPassword3!
-
For GitHub Actions / CI/CD, see the CI/CD Configuration Guide for adding secrets
-
Update
playwright.config.tsto match the number of accounts:workers: 3, // Match number of test accounts
Naming Convention: Use TEST_ACCOUNT_N_EMAIL and TEST_ACCOUNT_N_PASSWORD where N is the account number (1, 2, 3...).
When you run tests, the setup project creates separate authentication files for each worker:
- Setup runs in parallel (with
workersmatching number of accounts) for faster authentication - Multiple test instances are created - one for each account in
test-accounts.json - Each test authenticates with its assigned account and saves to:
- Worker 0 →
playwright/auth-states/user-0.json - Worker 1 →
playwright/auth-states/user-1.json - Worker N →
playwright/auth-states/user-N.json
- Worker 0 →
- Test execution then runs in parallel, with each worker loading its own auth file
Example console output:
[setup] › authentication setup - user 0
[Auth Setup] Saved authentication state to: playwright/auth-states/user-0.json
[setup] › authentication setup - user 1
[Auth Setup] Saved authentication state to: playwright/auth-states/user-1.json
- Setup project runs with multiple workers for faster authentication
- For each account in
test-accounts.json:- Navigates to the Landing page
- Clicks "Start now" → GOV.UK One Login
- Enters credentials and authenticates
- Checks for incomplete account setup (MFA not configured)
- Saves cookies/state to
playwright/auth-states/user-{N}.json
- Validation: Verifies all accounts reach the application's main page
- Each worker loads its specific auth file:
- Worker 0 → loads
user-0.json - Worker 1 → loads
user-1.json - Worker N → loads
user-N.json
- Worker 0 → loads
- Auth state is loaded into a worker-scoped browser context with stored email and worker info
- Context-based email retrieval uses
getCurrentUserEmail(page)to get authenticated user from browser context andgetCurrentUserDisplayName(page)for display name validation - All tests in that worker reuse the authenticated session
- Each test gets a new page but maintains authentication
What it is: Real-time authentication recovery system that detects and fixes broken sessions exactly when needed during test execution.
Why it's needed: When tests fail, Playwright sometimes closes the shared browser context, breaking authentication for retry attempts and subsequent tests.
How it works:
- Real-time detection - LandingPage methods (like
clickSignIn_AuthenticatedUser) automatically detect authentication loss via URL inspection - Immediate recovery - Calls
AuthUtils.reAuthenticate(page)utility function to handle complete re-authentication flow - Context-based credentials - Uses worker info stored in browser context to identify correct account for re-authentication
- Shared state management - Uses separate
saveWorkerIndexAndUserMapping()andsaveAuthState()functions for clean separation of concerns - Streamlined fixtures - Simple fixtures delegate authentication recovery to LandingPage methods and AuthUtils functions
- Recovery is transparent - Tests continue normally without manual intervention
When it activates:
- During test retries after failures
- When browser contexts are unexpectedly closed
- When authentication sessions become invalid
- At exact point of authentication loss (not preemptively)
For CI/CD configuration details, see the CI/CD Configuration Guide.
Authentication fails in tests:
- LandingPage methods detect issues and call
AuthUtils.reAuthenticate()automatically - Dedicated utility function with context-based worker identification for reliable recovery - Context-based email retrieval -
getCurrentUserEmail(page)provides efficient access to authenticated user info andgetCurrentUserDisplayName(page)for UI display name validation - Separated authentication functions -
saveWorkerIndexAndUserMapping()andsaveAuthState()ensure clean state management - Zero manual intervention needed - Streamlined architecture automatically fixes session issues during test execution
- Run
npx playwright test --project=setuponly if switching test environments or updating credentials - Verify all accounts in
tests/config/test-accounts.jsonare registered and valid - Check if
playwright/auth-states/user-*.jsonfiles exist for all workers
"Finish creating your GOV.UK One Login" error:
- This means a test account hasn't completed MFA setup
- Log into the account manually at the application URL
- Complete the MFA setup (text message or authenticator app)
- Run setup again:
npx playwright test --project=setup
Missing authentication file (user-N.json not found):
- Ensure you have enough test accounts in
tests/config/test-accounts.json(one per worker) - Reduce
workersinplaywright.config.tsto match available accounts - Run setup to create all auth files:
npx playwright test --project=setup
Parallel execution issues:
- Each account must be a separate, fully registered GOV.UK One Login account
- Match
workerscount to the number of available test accounts - Ensure all accounts have completed MFA setup before running tests
- Verify all auth files were created:
ls playwright/auth-states/
Session expired:
- Re-run setup project to capture fresh authentication
- Authentication state expires after ~30 minutes of inactivity
- Check for expired sessions:
npx playwright test --project=setup
Setup taking too long:
- Setup runs in parallel with multiple workers
- Expected time: ~8-10 seconds per account (running concurrently)
- For 2 accounts with 2 workers: ~8-10 seconds total for setup
- Check network connectivity if authentication is slower than expected