Sometimes tests need to remember things like login info so you don't have to do the same steps again and again.
Preserving state between tests in Cypress
Cypress.Cookies.preserveOnce('cookieName1', 'cookieName2') // or Cypress.Cookies.defaults({ preserve: ['cookieName'] })
Use preserveOnce inside a beforeEach hook to keep cookies for that test run only.
Use defaults to keep cookies for all tests automatically.
beforeEach(() => {
Cypress.Cookies.preserveOnce('session_id', 'auth_token')
})Cypress.Cookies.defaults({
preserve: ['session_id']
})before(() => {
cy.login() // custom command to log in once
})
beforeEach(() => {
Cypress.Cookies.preserveOnce('session_id')
})This test suite logs in once before all tests. Then it keeps the 'session_id' cookie between tests so the user stays logged in. Test 1 checks the dashboard greeting. Test 2 visits the profile page without logging in again.
describe('Preserve login state between tests', () => { before(() => { cy.visit('/login') cy.get('#username').type('user1') cy.get('#password').type('password123') cy.get('button[type=submit]').click() cy.url().should('include', '/dashboard') }) beforeEach(() => { Cypress.Cookies.preserveOnce('session_id') }) it('Test 1: Check dashboard greeting', () => { cy.visit('/dashboard') cy.contains('Welcome, user1').should('be.visible') }) it('Test 2: Visit profile page without logging in again', () => { cy.visit('/profile') cy.contains('user1').should('be.visible') }) })
Preserving cookies is the most common way to keep login state.
You can also preserve local storage or session storage using custom commands.
Be careful: preserving too much state can make tests depend on each other, which is not ideal.
Preserving state helps tests run faster and avoid repeating steps.
Use Cypress.Cookies.preserveOnce or Cypress.Cookies.defaults to keep cookies.
Keep tests independent by preserving only what is necessary.
Practice
Solution
Step 1: Understand test state preservation
Preserving state means keeping some data like cookies so tests don't repeat setup steps.Step 2: Identify the benefit
Keeping state avoids repeating login or setup, making tests faster and more efficient.Final Answer:
To avoid repeating login steps and speed up test execution -> Option CQuick Check:
Preserving state = faster tests [OK]
- Thinking tests should be dependent on each other
- Believing preserving state clears cookies
- Assuming preserving state disables assertions
Solution
Step 1: Recall Cypress cookie preservation method
The correct method to preserve cookies once isCypress.Cookies.preserveOnce.Step 2: Match method and argument
The method takes the cookie name as a string argument, e.g., 'session_id'.Final Answer:
Cypress.Cookies.preserveOnce('session_id') -> Option AQuick Check:
Preserve once = Cypress.Cookies.preserveOnce [OK]
- Using cy.preserveCookie which does not exist
- Mixing method names like preserveCookieOnce
- Calling preserve on cy.Cookies which is invalid
describe('Test Suite', () => {
beforeEach(() => {
Cypress.Cookies.preserveOnce('token')
})
it('Test 1: Set cookie', () => {
cy.setCookie('token', 'abc123')
})
it('Test 2: Check cookie', () => {
cy.getCookie('token').should('have.property', 'value', 'abc123')
})
})Solution
Step 1: Analyze cookie preservation in beforeEach
ThebeforeEachhook preserves the 'token' cookie between tests usingCypress.Cookies.preserveOnce('token').Step 2: Understand cookie setting and checking
Test 1 sets the cookie 'token' to 'abc123'. Test 2 checks the cookie value. Since the cookie is preserved, the check passes.Final Answer:
Test 2 fails because cookies are cleared between tests -> Option BQuick Check:
PreserveOnce keeps cookie for next test [OK]
- Assuming cookies clear automatically between tests
- Thinking preserveOnce clears cookies
- Confusing syntax causing errors
beforeEach(() => {
Cypress.Cookies.preserveOnce('auth_token')
})
it('Login test', () => {
cy.setCookie('auth_token', 'xyz789')
})
it('Dashboard test', () => {
cy.getCookie('auth_token').should('exist')
})What is the likely cause of the failure?
Solution
Step 1: Understand when preserveOnce runs
ThebeforeEachhook runs before each test, so it preserves cookies existing before the test starts.Step 2: Analyze cookie setting timing
The cookie is set inside the first test, afterbeforeEachruns, so it is not preserved for the next test.Final Answer:
The cookie is set after the beforeEach hook runs, so it is not preserved -> Option AQuick Check:
PreserveOnce must run after cookie set [OK]
- Thinking preserveOnce preserves cookies set during the test
- Assuming Cypress can't preserve cookies
- Blaming assertion syntax instead of timing
Solution
Step 1: Understand preserving multiple cookies globally
UsingCypress.Cookies.defaultswith a preserve array keeps specified cookies across all tests automatically.Step 2: Compare with other options
Calling preserveOnce in every test is repetitive. Manually setting cookies each time is inefficient. Disabling cookie clearing globally is not recommended.Final Answer:
Use Cypress.Cookies.defaults({ whitelist: ['token', 'user_id'] }) in support file -> Option DQuick Check:
Defaults preserve multiple cookies globally [OK]
- Calling preserveOnce repeatedly instead of defaults
- Manually resetting cookies each test
- Trying to disable cookie clearing globally
Ready to prove it?
Pass the test and this pattern moves unknown
