0
0
CypressHow-ToBeginner ยท 4 min read

How to Preserve Cookies in Cypress for Reliable Tests

To preserve cookies in Cypress, use Cypress.Cookies.preserveOnce() or Cypress.Cookies.defaults() inside your test or support/index.js. This keeps specified cookies from being cleared between tests, allowing session data to persist.
๐Ÿ“

Syntax

Cypress.Cookies.preserveOnce(...cookieNames) preserves specific cookies by name for the current test run.
Cypress.Cookies.defaults({ preserve: ... }) sets global cookie preservation rules for all tests.

  • cookieNames: One or more cookie names as strings.
  • preserve: A string, array of strings, or a function returning true to preserve cookies.
javascript
Cypress.Cookies.preserveOnce('session_id', 'auth_token')

// or globally in support file
Cypress.Cookies.defaults({
  preserve: ['session_id', 'auth_token']
})
๐Ÿ’ป

Example

This example shows how to preserve the session_id cookie between tests to keep the user logged in.

javascript
describe('Preserve Cookies Example', () => {
  beforeEach(() => {
    Cypress.Cookies.preserveOnce('session_id')
  })

  it('logs in user', () => {
    cy.visit('/login')
    cy.get('#username').type('user')
    cy.get('#password').type('pass')
    cy.get('button[type=submit]').click()
    cy.url().should('include', '/dashboard')
  })

  it('stays logged in on next test', () => {
    cy.visit('/dashboard')
    cy.contains('Welcome, user').should('be.visible')
  })
})
Output
Test 1: passes (user logs in and session_id cookie set) Test 2: passes (session_id cookie preserved, user stays logged in)
โš ๏ธ

Common Pitfalls

  • Not calling Cypress.Cookies.preserveOnce() inside beforeEach() causes cookies to clear before each test.
  • Using Cypress.Cookies.defaults() incorrectly can preserve unwanted cookies.
  • Preserving cookies that expire or are deleted by the server won't keep the session alive.
javascript
/* Wrong: preserveOnce called only once outside beforeEach */
Cypress.Cookies.preserveOnce('session_id')

describe('Test Suite', () => {
  it('test 1', () => { /* ... */ })
  it('test 2', () => { /* session cookie cleared here */ })
})

/* Right: preserveOnce called inside beforeEach */
describe('Test Suite', () => {
  beforeEach(() => {
    Cypress.Cookies.preserveOnce('session_id')
  })
  it('test 1', () => { /* ... */ })
  it('test 2', () => { /* session cookie preserved */ })
})
๐Ÿ“Š

Quick Reference

MethodPurposeUsage Location
Cypress.Cookies.preserveOnce(cookieNames)Preserves specific cookies for current test runInside beforeEach() or test
Cypress.Cookies.defaults({ preserve })Sets global cookie preservation rulesIn support/index.js or plugins
cy.clearCookies()Clears all cookies (overrides preservation)Inside tests when needed
โœ…

Key Takeaways

Use Cypress.Cookies.preserveOnce inside beforeEach to keep cookies between tests.
Set global cookie preservation with Cypress.Cookies.defaults in support files for convenience.
Preserved cookies must exist and not expire to maintain session state.
Calling preserveOnce outside beforeEach will not preserve cookies correctly.
Clearing cookies manually overrides preservation settings.