cy.session() helps save time by remembering login or setup steps so tests run faster and more reliably.
cy.session() for session caching in Cypress
cy.session(sessionId, setupCallback, options?)
sessionId: A unique name or key for the session.
setupCallback: A function that performs login or setup steps.
cy.session('user1', () => { cy.visit('/login') cy.get('#username').type('user1') cy.get('#password').type('password') cy.get('button[type=submit]').click() })
cy.session('admin', () => { cy.request('POST', '/api/login', { username: 'admin', password: 'adminpass' }) })
This test suite uses cy.session() to cache the login session for 'standardUser'. The login runs only once before all tests, making tests faster and stable.
describe('Test with session caching', () => { beforeEach(() => { cy.session('standardUser', () => { cy.visit('/login') cy.get('#username').type('standardUser') cy.get('#password').type('password123') cy.get('button[type=submit]').click() cy.url().should('include', '/dashboard') }) }) it('should show user dashboard', () => { cy.visit('/dashboard') cy.contains('Welcome, standardUser').should('be.visible') }) it('should allow user to logout', () => { cy.visit('/dashboard') cy.get('#logout').click() cy.url().should('include', '/login') }) })
Use unique session IDs to avoid conflicts between different users or setups.
Sessions are automatically restored between tests, so you don't need to repeat login steps.
cy.session() improves test speed and reduces flakiness by caching browser state.
cy.session() caches login or setup steps to speed up tests.
Use it when you want to reuse user sessions across tests.
It makes tests faster and more reliable by avoiding repeated logins.
Practice
cy.session() in Cypress tests?Solution
Step 1: Understand what
cy.session()doescy.session()caches login or setup steps so tests don't repeat them.Step 2: Identify the main benefit
This caching speeds up tests by reusing sessions instead of logging in every time.Final Answer:
To cache user sessions and avoid repeated login steps -> Option CQuick Check:
Session caching = Avoid repeated logins [OK]
cy.session() saves login to speed tests [OK]- Thinking it clears cookies instead of caching sessions
- Confusing it with parallel test execution
- Assuming it generates test data
cy.session()?Solution
Step 1: Recall
The second argument must be a callback function that runs login steps, e.g.,cy.session()syntax() => { cy.login() }.Step 2: Check each option
Onlycy.session('user', () => { cy.login() })correctly passes a callback function. The other options pass a function reference incorrectly, execute the function immediately, or use invalid syntax.Final Answer:
cy.session('user', () => { cy.login() }) -> Option AQuick Check:
Callback function syntax = cy.session('user', () => { cy.login() }) [OK]
- Passing function call instead of function
- Using invalid function syntax
- Omitting the callback wrapper
cy.session('admin', () => {
cy.visit('/login')
cy.get('#username').type('admin')
cy.get('#password').type('password')
cy.get('button[type=submit]').click()
})
cy.session('admin', () => {
cy.visit('/dashboard')
})Solution
Step 1: Understand session caching behavior
Sessions with the same key ('admin') cache the first login steps and reuse them in subsequent calls.Step 2: Analyze the second
The second call with the same key skips login steps and reuses the cached session, so only the visit to '/dashboard' runs.cy.session()callFinal Answer:
The login steps run only once; second session reuses cached login -> Option AQuick Check:
Same key session = cached login reused [OK]
- Assuming login runs every time
- Thinking different visits cause errors
- Believing duplicate keys cause test failure
cy.session():cy.session('user', () => {
cy.visit('/login')
cy.get('#user').type('test')
cy.get('#pass').type('1234')
cy.get('button').click()
})
cy.session('user', () => {
cy.visit('/profile')
})Solution
Step 1: Check session key usage
Reusing the same session key 'user' is correct to cache and reuse the session.Step 2: Verify callback structure
The callback contains Cypress commands chained properly; no need to return a promise explicitly.Step 3: Confirm second session usage
The second session call reuses the cached session and visits '/profile' without error.Final Answer:
No error; code works as expected -> Option DQuick Check:
Reusing session key with proper callback = No error [OK]
- Thinking session keys must be unique per call
- Expecting explicit promise return in callback
- Assuming login steps must repeat every time
cy.session() usage to fix this?Solution
Step 1: Understand stale session problem
Stale sessions cause intermittent failures because cached data may expire or become invalid.Step 2: Use
Thevalidatecallback incy.session()validatecallback runs before reusing a session to confirm it is still valid, forcing a fresh login if needed.Step 3: Avoid other options
Using different keys disables caching, removing benefits. Removingcy.session()slows tests. Clearing cookies manually is less efficient.Final Answer:
Add avalidatecallback to check session validity before reuse -> Option BQuick Check:
Validate callback fixes stale session issues [OK]
- Disabling caching by changing keys unnecessarily
- Ignoring session validation causing flaky tests
- Removing session caching and slowing tests
Ready to prove it?
Pass the test and this pattern moves unknown
