This repository demonstrates different ways to handle authentication in Playwright using UI login, API login, storage state, multiple accounts, user roles, and request interception.
The goal of this project is to show practical authentication strategies that can make Playwright tests faster, more reliable, and easier to scale.
Authentication is one of the most common challenges in test automation. Instead of logging in through the UI for every test, Playwright allows us to save and reuse authenticated browser state.
This project contains multiple authentication recipes, starting from simple UI-based login reuse to advanced API-based authentication with multiple roles and request interception.
- Playwright
- TypeScript
- Node.js
- Playwright Test Runner
- API Authentication
- Storage State
- Request Interception
git clone <your-repository-url>
cd <your-project-folder>npm installnpx playwright installnpx playwright testnpx playwright test tests/Recipe3.spec.tsnpx playwright test --headednpx playwright test --debugnpx playwright test --uiAfter test execution, generate and view the Playwright report:
npx playwright show-report.
├── tests
│ ├── Recipe1.spec.ts
│ ├── Recipe2.spec.ts
│ ├── Recipe3.spec.ts
│ ├── Recipe4.spec.ts
│ ├── Recipe5.spec.ts
│ ├── Recipe6.spec.ts
│ └── Recipe7.spec.ts
├── playwright.config.ts
├── package.json
└── README.mdApproach: Authenticate once through the UI and save the login session as a storage state file.
When to use: Use this when all tests can safely run with the same user account and do not affect each other.
Example scenario: Validating pages that only require a logged-in user, such as dashboard, profile, or order history.
Approach: Authenticate through the UI using multiple accounts and save separate storage state files for each account.
When to use: Use this when tests need different users running at the same time without session conflict.
Example scenario: Running tests in parallel where each test needs a separate customer account.
Approach: Authenticate multiple users with different roles through the UI and reuse their storage states.
When to use: Use this when the application has role-based access, such as admin, manager, and customer.
Example scenario: Validating that an admin can access user management, but a normal user cannot.
Approach: Authenticate directly through the API, create the browser storage state, and reuse it in tests.
When to use: Use this when you want faster and more stable authentication without depending on the UI login flow.
Example scenario: Bypassing the login screen and directly testing authenticated pages.
Approach: Authenticate multiple users through the API and save separate storage state files.
When to use: Use this when multiple test users are needed and UI login would slow down execution.
Example scenario: Running parallel tests where each worker gets a unique authenticated account.
Approach: Authenticate users with different roles through the API and reuse their storage states.
When to use: Use this when testing role-based access control in a faster and scalable way.
Example scenario: Testing admin, seller, and buyer workflows without logging in through the UI every time.
Approach: Authenticate through the API and use Playwright request interception to control or modify network behavior.
When to use: Use this when you need to mock, modify, block, or validate API requests during authenticated test execution.
Example scenario: Testing how the application behaves when a user API returns a specific role, permission, or error response.
| Test File | Description |
|---|---|
tests/Recipe1.spec.ts |
UI authentication using single storage state |
tests/Recipe2.spec.ts |
UI authentication with multiple accounts |
tests/Recipe3.spec.ts |
UI authentication with multiple accounts and roles |
tests/Recipe4.spec.ts |
API authentication using storage state |
tests/Recipe5.spec.ts |
API authentication with multiple accounts |
tests/Recipe6.spec.ts |
API authentication with multiple accounts and roles |
tests/Recipe7.spec.ts |
API authentication with request interception |
Reusing authentication state helps to:
- Reduce repeated login steps
- Make tests faster
- Avoid unnecessary dependency on the login UI
- Improve test stability
- Support parallel execution
- Support multiple users and roles
- Keep authentication setup separate from actual test logic
It is recommended to store credentials in a .env file.
Example:
BASE_URL=https://your-app-url.com
ADMIN_USERNAME=admin@example.com
ADMIN_PASSWORD=yourPassword
USER_USERNAME=user@example.com
USER_PASSWORD=yourPasswordMake sure .env is added to .gitignore.
.env
auth/
storage-state/Created as part of a Playwright authentication strategy learning project.
This repository is useful for testers, automation engineers, and quality engineers who want to understand practical authentication patterns in Playwright.