Cypress Sessions
- Radek Stolarczyk
- Mar 21
- 2 min read
One of the most useful features in larger Cypress test suites is cy.session().
It helps solve a common problem: repeating the same setup steps, especially logging in before each test.
cy.session() lets Cypress save and reuse session data like cookies, localStorage, and sessionStorage. This means you don’t have to log in every time. Cypress can quickly restore the same browser state.
It also works with Cypress test isolation and can reuse sessions across different spec files when you enable cacheAcrossSpecs.
What is a Cypress session?
A Cypress session is a cached authenticated browser state.
Instead of logging in before every test, Cypress can:
perform login once
save the session
restore it in later tests
This usually means:
less repeated login code
faster test execution
less flakiness
cleaner, focused tests
A session can store:
cookies
local storage
session storage
Example: UI login with cy.session()
What this does
cy.session('user', ...) creates a cached session
the callback runs the real login flow
Cypress saves the authenticated state
future tests reuse it instead of logging in again
cacheAcrossSpecs: true allows reuse across spec files
Using sessions in tests
Before
After
The test becomes shorter and focused on the real behavior.
Using different users
When working with multiple roles (admin, user, etc.), each session should have a unique ID.
Example usage
Benefits of sessions
Use cy.session() when:
login is slow
login repeats across many tests
setup is identical
many specs use the same user
Main benefits
faster runs
less duplicated code
fewer login-related failures
better readability
Key rule
Use sessions when login is setup, not the thing you are testing.
When not to use sessions
Avoid sessions when login itself is under test:
login validation errors
MFA flows
password reset
auth redirects
“remember me” behavior
In these cases, the login flow should not be skipped.