Bird
0
0

You want to maintain both a cookie named 'auth_token' and local storage data across multiple Cypress tests. Which approach correctly achieves this?

hard📝 Application Q8 of 15
Cypress - Authentication and Sessions
You want to maintain both a cookie named 'auth_token' and local storage data across multiple Cypress tests. Which approach correctly achieves this?
ACall Cypress.Cookies.preserveOnce('auth_token') only once in before and clear local storage manually in each test
BUse Cypress.Cookies.preserveOnce('auth_token') in beforeEach and implement custom commands to save and restore local storage in beforeEach and afterEach hooks
CSet cookies and local storage in the first test only and rely on Cypress default behavior to keep them
DUse cy.preserveCookie('auth_token') and cy.preserveLocalStorage() commands in beforeEach
Step-by-Step Solution
Solution:
  1. Step 1: Preserve cookies properly

    Use Cypress.Cookies.preserveOnce('auth_token') inside beforeEach to keep the cookie between tests.
  2. Step 2: Handle local storage manually

    Cypress does not preserve local storage by default; custom commands to save and restore local storage in beforeEach and afterEach are needed.
  3. Step 3: Evaluate options

    Use Cypress.Cookies.preserveOnce('auth_token') in beforeEach and implement custom commands to save and restore local storage in beforeEach and afterEach hooks correctly combines cookie preservation and local storage management.
  4. Final Answer:

    Use Cypress.Cookies.preserveOnce('auth_token') in beforeEach and implement custom commands to save and restore local storage in beforeEach and afterEach hooks -> Option B
  5. Quick Check:

    Cookies preserved with preserveOnce; local storage requires manual save/restore [OK]
Quick Trick: Preserve cookies with preserveOnce; save/restore local storage manually [OK]
Common Mistakes:
  • Assuming Cypress preserves local storage automatically
  • Calling preserveOnce only once in before hook
  • Using non-existent cy.preserveCookie or cy.preserveLocalStorage commands

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes