Challenge - 5 Problems
Local Storage Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Cypress test snippet?
Consider the following Cypress test code that interacts with local storage. What will be the value of
storedValue logged in the test?Cypress
cy.window().then(win => { win.localStorage.setItem('token', 'abc123'); }); cy.window().then(win => { const storedValue = win.localStorage.getItem('token'); cy.log(storedValue); });
Attempts:
2 left
💡 Hint
Remember that localStorage stores string values and persists within the same window context.
✗ Incorrect
The code sets the localStorage item 'token' to 'abc123' and then retrieves it. The value logged is 'abc123'.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies a localStorage key value in Cypress?
You want to assert that the localStorage key 'user' has the value 'admin'. Which Cypress assertion is correct?
Attempts:
2 left
💡 Hint
Use
win.localStorage.getItem() to access localStorage values.✗ Incorrect
Option C correctly uses
win.localStorage.getItem('user') and asserts its value with expect(...).to.equal(...).🔧 Debug
advanced2:00remaining
Why does this Cypress test fail to clear localStorage?
This test tries to clear localStorage but fails. Identify the reason.
Cypress
cy.window().then(win => { win.localStorage.clear(); }); cy.window().then(win => { const length = win.localStorage.length; cy.log(length); });
Attempts:
2 left
💡 Hint
Check how methods are invoked in JavaScript.
✗ Incorrect
The code references
win.localStorage.clear without parentheses, so the method is not executed.❓ framework
advanced2:00remaining
Which Cypress command best resets localStorage before each test?
You want to ensure localStorage is empty before each test runs. Which approach is best?
Attempts:
2 left
💡 Hint
Resetting before each test ensures clean state.
✗ Incorrect
Option A uses the built-in Cypress command
cy.clearLocalStorage() inside beforeEach to clear localStorage before every test.🧠 Conceptual
expert2:00remaining
What is a key limitation of testing localStorage in Cypress across multiple test files?
When running Cypress tests split across multiple spec files, what is a common limitation related to localStorage?
Attempts:
2 left
💡 Hint
Think about test isolation and browser context resets.
✗ Incorrect
Cypress clears localStorage between spec files to isolate tests, so data does not persist across them.