Bird
0
0

Identify the error in the following code using cy.session():

medium📝 Debug Q14 of 15
Cypress - Authentication and Sessions
Identify the error in the following code using 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')
})
AThe session key 'user' is reused, causing session cache conflict
BThe login callback does not return a promise or chain commands
CThe second session callback should include login steps again
DNo error; code works as expected
Step-by-Step Solution
Solution:
  1. Step 1: Check session key usage

    Reusing the same session key 'user' is correct to cache and reuse the session.
  2. Step 2: Verify callback structure

    The callback contains Cypress commands chained properly; no need to return a promise explicitly.
  3. Step 3: Confirm second session usage

    The second session call reuses the cached session and visits '/profile' without error.
  4. Final Answer:

    No error; code works as expected -> Option D
  5. Quick Check:

    Reusing session key with proper callback = No error [OK]
Quick Trick: Reusing session key with proper callback is valid [OK]
Common Mistakes:
  • Thinking session keys must be unique per call
  • Expecting explicit promise return in callback
  • Assuming login steps must repeat every time

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes