Bird
0
0

You want to cache a session but also clear cookies before each test. Which approach correctly combines cy.session() with cookie clearing?

hard📝 Application Q8 of 15
Cypress - Authentication and Sessions
You want to cache a session but also clear cookies before each test. Which approach correctly combines cy.session() with cookie clearing?
ACall <code>cy.session('user', loginFn)</code> inside <code>beforeEach</code> without clearing cookies
BUse <code>beforeEach(() => { cy.clearCookies(); cy.session('user', loginFn) })</code>
CClear cookies inside the <code>cy.session()</code> callback function
DClear cookies after <code>cy.session()</code> call in the test body
Step-by-Step Solution
Solution:
  1. Step 1: Understand cookie clearing timing

    Cookies must be cleared before session creation to avoid stale data.

  2. Step 2: Combine clearing and session in beforeEach

    Use beforeEach(() => { cy.clearCookies(); cy.session('user', loginFn) }) clears cookies then creates session before each test, ensuring fresh state.

  3. Final Answer:

    Use beforeEach(() => { cy.clearCookies(); cy.session('user', loginFn) }) -> Option B
  4. Quick Check:

    Clear then session in beforeEach = A [OK]
Quick Trick: Clear cookies before session to avoid stale data [OK]
Common Mistakes:
  • Clearing cookies after session creation
  • Not clearing cookies at all
  • Clearing cookies inside session callback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes