Bird
0
0

What will be the output of the following Cypress code snippet?

medium📝 Predict Output Q13 of 15
Cypress - Authentication and Sessions
What will be the output of the following Cypress code snippet?
cy.window().then(win => {
  win.localStorage.setItem('user', 'Alice');
  const user = win.localStorage.getItem('user');
  cy.wrap(user).should('equal', 'Alice');
});
ATest will fail due to syntax error in setItem usage
BTest will fail because localStorage is not accessible
CTest will pass because 'user' is set and retrieved correctly
DTest will pass but 'user' will be undefined
Step-by-Step Solution
Solution:
  1. Step 1: Analyze localStorage set and get

    The code sets 'user' key to 'Alice' in localStorage, then retrieves it correctly.
  2. Step 2: Check assertion correctness

    The assertion checks if retrieved value equals 'Alice', which is true, so test passes.
  3. Final Answer:

    Test will pass because 'user' is set and retrieved correctly -> Option C
  4. Quick Check:

    Set and get localStorage works, test passes [OK]
Quick Trick: Setting then getting localStorage key returns correct value [OK]
Common Mistakes:
  • Assuming localStorage is inaccessible in Cypress
  • Thinking setItem syntax is wrong
  • Expecting undefined instead of stored value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes