Local storage is a browser feature that stores data on the user's computer. It keeps data even after the browser is closed, allowing tests to check saved information between sessions.
Click to reveal answer
beginner
How do you clear local storage in Cypress?
Use cy.clearLocalStorage() to remove all data from local storage during a test. This helps start tests with a clean state.
Click to reveal answer
intermediate
How to set a local storage item in Cypress before a test?
Use cy.window().then((win) => { win.localStorage.setItem('key', 'value') }) to add data to local storage. This simulates saved data before the app loads.
Click to reveal answer
intermediate
How can you verify a local storage value in Cypress?
Use cy.getLocalStorage('key').should('eq', 'value') to check if the stored value matches what you expect.
Click to reveal answer
beginner
Why is managing local storage important in automated tests?
Managing local storage ensures tests run with predictable data. It avoids leftover data causing false test results and helps simulate user scenarios accurately.
Click to reveal answer
Which Cypress command clears all local storage data?
Acy.clearLocalStorage()
Bcy.clearCookies()
Ccy.resetStorage()
Dcy.clearSessionStorage()
✗ Incorrect
cy.clearLocalStorage() clears all local storage data. Other options clear cookies or session storage, not local storage.
How do you set a local storage item with key 'token' and value 'abc123' in Cypress?
localStorage.clear is a function and must be called with parentheses: clear()
Step 2: Identify missing parentheses error
The code uses clear without parentheses, so it does not execute the function.
Final Answer:
Missing parentheses to call clear method -> Option A
Quick Check:
Call clear() with parentheses to clear storage [OK]
Hint: Always add () to call localStorage.clear() method [OK]
Common Mistakes:
Forgetting parentheses when calling functions
Thinking clear needs a key argument
Assuming cy.window() can't access localStorage
5. 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
A. cy.window().then(win => win.localStorage.setItem('token', 'abc123')); cy.reload(); cy.window().then(win => expect(win.localStorage.getItem('token')).to.equal('abc123'));
B. cy.setLocalStorage('token', 'abc123'); cy.reload(); cy.getLocalStorage('token').should('equal', 'abc123');
C. cy.window().then(win => win.localStorage.setItem('token', 'abc123')); cy.reload(); cy.window().its('localStorage.token').should('equal', 'abc123');
D. cy.window().then(win => win.localStorage.clear()); cy.reload(); cy.window().its('localStorage.token').should('be.null');
Solution
Step 1: Save token in localStorage correctly
Using cy.window().then(win => win.localStorage.setItem('token', 'abc123')) correctly sets the token.
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'.
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.
Final Answer:
Use cy.window() to set, reload, then check with expect -> Option A
Quick Check:
Set, reload, then assert with cy.window() and expect [OK]
Hint: 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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.