Protected routes should reject unauthenticated users and enforce roles. Playwright verifies guards redirect correctly and authorized users reach content.
Guard Test Scenarios
Logged-out user visiting /admin should redirect to login or see 403. Authenticated user with wrong role should see unauthorized message. Authorized user sees content.
Use empty storageState for unauthenticated cases; role-specific storageState for authorization matrix.
Browser tests assert redirect to login. API tests with request.get('/api/admin') assert 401/403 without involving the UI — faster contract verification.
Common Mistakes
Only testing navigation path, not direct URL entry.
Admin storageState on tests that should start logged out.
Not testing client-side route guards after SPA load.
Ignoring API authorization — only checking UI hide/show.
Key Takeaways
Test unauthenticated, wrong-role, and authorized access.
Use storageState projects for role matrices.
Deep link to protected URLs every time.
Align UI guards with API status expectations.
Pro Tip
Run the same protected-route spec under admin and user projects — doubles coverage with zero duplicate code.
Continue with Browsers to build on what you learned here.