Local storage management in Cypress - Build an Automation Script
describe('Local Storage Management Test', () => { const url = 'http://localhost:3000/storage-test'; beforeEach(() => { cy.visit(url); }); it('should save, persist, and clear local storage data correctly', () => { // Click save button to store 'username' = 'tester' cy.get('#save-btn').click(); // Verify displayed value is 'tester' cy.get('#storage-value').should('have.text', 'tester'); // Reload the page cy.reload(); // Verify value persists after reload cy.get('#storage-value').should('have.text', 'tester'); // Click clear button to clear local storage cy.get('#clear-btn').click(); // Verify display shows no data cy.get('#storage-value').should(($el) => { const text = $el.text().trim(); expect(['', 'No data']).to.include(text); }); }); });
This Cypress test automates the manual test case step-by-step.
beforeEach: Visits the test page before each test to start fresh.
Click save button: Simulates user clicking the button that saves data to local storage.
Assertions: Checks the displayed value matches the expected 'tester' after saving and after page reload to confirm persistence.
Clear local storage: Clicks the clear button and verifies the display updates to show no data.
This approach uses clear selectors, Cypress commands, and assertions to ensure the local storage behavior is correct and visible to the user.
Now add data-driven testing with 3 different username inputs: 'tester', 'admin', 'guest'
Practice
Solution
Step 1: Understand local storage role
Local storage is used by browsers to save small data like user preferences or tokens persistently.Step 2: Connect with Cypress testing
Cypress can access this storage to verify or manipulate data during tests.Final Answer:
To store small amounts of data persistently for a website -> Option BQuick Check:
Local storage = persistent small data [OK]
- Confusing local storage with server-side storage
- Thinking local storage runs code
- Mixing local storage with styling or network
Solution
Step 1: Recall Cypress commands for browser objects
To access the browser's window object, Cypress providescy.window().Step 2: Understand local storage access
Local storage is a property of the window object, socy.window()is used to reach it.Final Answer:
cy.window() -> Option DQuick Check:
Access window with cy.window() [OK]
- Using non-existent commands like cy.getLocalStorage()
- Confusing cy.document() with window object
- Assuming cy.localStorage() exists
cy.window().then(win => {
win.localStorage.setItem('user', 'Alice');
const user = win.localStorage.getItem('user');
cy.wrap(user).should('equal', 'Alice');
});Solution
Step 1: Analyze localStorage set and get
The code sets 'user' key to 'Alice' in localStorage, then retrieves it correctly.Step 2: Check assertion correctness
The assertion checks if retrieved value equals 'Alice', which is true, so test passes.Final Answer:
Test will pass because 'user' is set and retrieved correctly -> Option CQuick Check:
Set and get localStorage works, test passes [OK]
- Assuming localStorage is inaccessible in Cypress
- Thinking setItem syntax is wrong
- Expecting undefined instead of stored value
cy.window().then(win => {
win.localStorage.clear;
});Solution
Step 1: Check localStorage clear usage
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 AQuick Check:
Call clear() with parentheses to clear storage [OK]
- Forgetting parentheses when calling functions
- Thinking clear needs a key argument
- Assuming cy.window() can't access localStorage
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 AQuick Check:
Set, reload, then assert with cy.window() and expect [OK]
- Using wrong localStorage property access
- Using non-existent Cypress commands
- Clearing storage instead of verifying token
Ready to prove it?
Pass the test and this pattern moves unknown
