Bird
0
0

You want to write a Cypress test that saves a token in local storage, reloads the page, and then verifies the token is still present. Which sequence of commands correctly achieves this?

hard📝 Application Q15 of 15
Cypress - Authentication and Sessions
You want to write a Cypress test that saves a token in local storage, reloads the page, and then verifies the token is still present. Which sequence of commands correctly achieves this?
Acy.window().then(win => win.localStorage.setItem('token', 'abc123')); cy.reload(); cy.window().then(win => expect(win.localStorage.getItem('token')).to.equal('abc123'));
Bcy.setLocalStorage('token', 'abc123'); cy.reload(); cy.getLocalStorage('token').should('equal', 'abc123');
Ccy.window().then(win => win.localStorage.setItem('token', 'abc123')); cy.reload(); cy.window().its('localStorage.token').should('equal', 'abc123');
Dcy.window().then(win => win.localStorage.clear()); cy.reload(); cy.window().its('localStorage.token').should('be.null');
Step-by-Step Solution
Solution:
  1. Step 1: Save token in localStorage correctly

    Using cy.window().then(win => win.localStorage.setItem('token', 'abc123')) correctly sets the token.
  2. Step 2: Reload and verify token presence

    After cy.reload(), accessing localStorage via cy.window().then and asserting with expect ensures token is still 'abc123'.
  3. Step 3: Evaluate other options

    cy.window().then(win => win.localStorage.setItem('token', 'abc123')); cy.reload(); cy.window().its('localStorage.token').should('equal', 'abc123'); uses incorrect property access for localStorage token; cy.setLocalStorage('token', 'abc123'); cy.reload(); cy.getLocalStorage('token').should('equal', 'abc123'); uses non-existent commands; cy.window().then(win => win.localStorage.clear()); cy.reload(); cy.window().its('localStorage.token').should('be.null'); clears storage and checks for null, which is not the goal.
  4. Final Answer:

    Use cy.window() to set, reload, then check with expect -> Option A
  5. Quick Check:

    Set, reload, then assert with cy.window() and expect [OK]
Quick Trick: Set token, reload, then check with cy.window() and expect [OK]
Common Mistakes:
  • Using wrong localStorage property access
  • Using non-existent Cypress commands
  • Clearing storage instead of verifying token

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes