Playwright Authentication
This chapter introduces how to handle authentication in Playwright, avoiding repeated login in every test while maintaining test isolation.
The core problem of authentication
Most web application tests require user login.
If every test performs the login flow from scratch, it significantly slows down testing.
Playwright's solution is: authenticate once, save the state, and reuse it across all tests.
Preparation: the .auth directory
Example
Create a directory to store the authentication state
mkdir
-p
playwright
/
.auth
Add to .gitignore (the authentication state contains confidential information)
echo
'playwright/.auth'
.gitignore
.authThe file contains sensitive cookies and authentication tokens,and must never be committed to the code repository。
Basic approach: shared account + Setup project
This is the authentication approach recommended by Playwright, suitable for scenarios where all tests share the same test account.
Step 1: Create the authentication Setup
Example
// File path: tests/auth.setup.ts
import
{
test as setup
}
from
'@playwright/test'
;
// Specify the path to save the authentication state
const
authFile
=
'playwright/.auth/user.json'
;
setup
(
'authenticate'
,
async
(
{
page
}
)
=>
{
// Perform the login operation
await page.
goto
(
'https://www.runoob.com/login'
)
;
await page.
getByLabel
(
'Username'
)
.
fill
(
'testuser'
)
;
await page.
getByLabel
(
'Password'
)
.
fill
(
'password123'
)
;
await page.
getByRole
(
'button'
,
{
name
:
'Login'
}
)
.
click
(
)
;
// Wait for login to complete (confirm page redirect or appearance of user info)
await page.
waitForURL
(
/dashboard/
)
;
// Save the current authentication state (cookies, localStorage) to a file
await page.
context
(
)
.
storageState
(
{
path
:
authFile
}
)
;
}
)
;
Step 2: Configure the Setup project
Example
// File path: playwright.config.ts
import
{
defineConfig
}
from
'@playwright/test'
;
export
default
defineConfig
(
{
projects
:
[
// Setup project - runs only once, generates the authentication state file
{
name
:
'setup'
,
testMatch
:
/auth.setup.ts/
,
}
,
// Actual test project - reuses the authentication state
{
name
:
'chromium'
,
use
:
{
storageState
:
'playwright/.auth/user.json'
,
}
,
dependencies
:
[
'setup'
]
,
// Depends on the setup project, ensuring setup runs first
}
,
]
,
}
)
;
Step 3: Tests directly use the authentication state
Example
// File path: tests/dashboard.spec.ts
import
{
test
,
expect
}
from
'@playwright/test'
;
test
(
'View dashboard'
,
async
(
{
page
}
)
=>
{
// Directly access a page that requires login; page already carries the authentication state
await page.
goto
(
'/dashboard'
)
;
await expect
(
page.
getByText
(
'Welcome back, testuser'
)
)
.
toBeVisible
(
)
;
}
)
;
Multi-account scenarios
If different tests need to use different accounts, you can create multiple authentication state files.
Example
// Admin test
test.
describe
(
'Admin features'
,
(
)
=>
{
test.
use
(
{
storageState
:
'playwright/.auth/admin.json'
}
)
;
test
(
'Manage users'
,
async
(
{
page
}
)
=>
{
// Operate as an administrator
}
)
;
}
)
;
// Regular user test
test.
describe
(
'Regular user features'
,
(
)
=>
{
test.
use
(
{
storageState
:
'playwright/.auth/user.json'
}
)
;
test
(
'Browse content'
,
async
(
{
page
}
)
=>
{
// Operate as a regular user
}
)
;
}
)
;
Authenticate via API requests (recommended)
Performing the login flow through the UI is slow; a more efficient approach is to call the login API directly.
Example
// File path: tests/auth.setup.ts
import
{
test as setup
}
from
'@playwright/test'
;
const
authFile
=
'playwright/.auth/user.json'
;
setup
(
'Authenticate via API'
,
async
(
{
request
}
)
=>
{
// Call the login API directly, without going through the browser UI
const
response
=
await request.
post
(
'https://www.runoob.com/api/login'
,
{
data
:
{
username
:
'testuser'
,
password
:
'password123'
,
}
,
}
)
;
// Confirm login succeeded
const
json
=
await response.
json
(
)
;
console.
log
(
'Login successful, Token:'
,
json.
token
)
;
// Save the token as a cookie (simulating browser authentication state)
// Note: storageState needs to be saved in a browser Context
}
)
;
API authentication is faster and more reliable than UI authentication; it is recommended to prioritize it in real projects.
Summary of applicable scenarios for authentication approaches
| Scenario | Recommended solution |
|---|---|
| All tests use the same test account | Setup project + storageState |
| Different types of tests need different accounts | Multiple storageState + test.use |
| Each test needs an independent account | Create account and login via API within each test |
| Account resources are limited | Setup project authenticates once, all reuse it |