Bird
Raised Fist0
Cypresstesting~15 mins

Local storage management in Cypress - Build an Automation Script

Choose your learning style10 modes available
Verify local storage operations on a web page
Preconditions (4)
Step 1: Open the web page at 'http://localhost:3000/storage-test'
Step 2: Click the button with id 'save-btn' to save the key 'username' with value 'tester' to local storage
Step 3: Verify that the element with id 'storage-value' displays 'tester'
Step 4: Refresh the page
Step 5: Verify that the element with id 'storage-value' still displays 'tester'
Step 6: Click the button with id 'clear-btn' to clear local storage
Step 7: Verify that the element with id 'storage-value' is empty or displays 'No data'
✅ Expected Result: The local storage key 'username' is saved and displayed correctly, persists after refresh, and is cleared when requested.
Automation Requirements - Cypress
Assertions Needed:
Verify displayed value matches expected local storage value after save
Verify value persists after page reload
Verify local storage is cleared and display updates accordingly
Best Practices:
Use cy.visit() to open the page
Use cy.get() with id selectors for buttons and display elements
Use cy.reload() to refresh the page
Use Cypress commands to interact with local storage
Use clear and meaningful assertion messages
Automated Solution
Cypress
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.

Common Mistakes - 3 Pitfalls
Using hardcoded waits like cy.wait(500) instead of relying on Cypress commands
Using non-unique or complex selectors like XPath instead of simple id selectors
{'mistake': 'Not verifying local storage persistence after page reload', 'why_bad': "Missing this step means you don't confirm data actually stays saved.", 'correct_approach': 'Use cy.reload() and assert the displayed value again to confirm persistence.'}
Bonus Challenge

Now add data-driven testing with 3 different username inputs: 'tester', 'admin', 'guest'

Show Hint

Practice

(1/5)
1. What is the primary purpose of local storage in a web browser when testing with Cypress?
easy
A. To run JavaScript code on the server
B. To store small amounts of data persistently for a website
C. To style the webpage dynamically
D. To manage network requests

Solution

  1. Step 1: Understand local storage role

    Local storage is used by browsers to save small data like user preferences or tokens persistently.
  2. Step 2: Connect with Cypress testing

    Cypress can access this storage to verify or manipulate data during tests.
  3. Final Answer:

    To store small amounts of data persistently for a website -> Option B
  4. Quick Check:

    Local storage = persistent small data [OK]
Hint: Local storage saves small data for the site, not server tasks [OK]
Common Mistakes:
  • Confusing local storage with server-side storage
  • Thinking local storage runs code
  • Mixing local storage with styling or network
2. Which Cypress command is used to access the browser's window object to manage local storage?
easy
A. cy.document()
B. cy.getLocalStorage()
C. cy.localStorage()
D. cy.window()

Solution

  1. Step 1: Recall Cypress commands for browser objects

    To access the browser's window object, Cypress provides cy.window().
  2. Step 2: Understand local storage access

    Local storage is a property of the window object, so cy.window() is used to reach it.
  3. Final Answer:

    cy.window() -> Option D
  4. Quick Check:

    Access window with cy.window() [OK]
Hint: Use cy.window() to reach local storage via window object [OK]
Common Mistakes:
  • Using non-existent commands like cy.getLocalStorage()
  • Confusing cy.document() with window object
  • Assuming cy.localStorage() exists
3. 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');
});
medium
A. Test will fail due to syntax error in setItem usage
B. Test will fail because localStorage is not accessible
C. Test will pass because 'user' is set and retrieved correctly
D. Test will pass but 'user' will be undefined

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]
Hint: 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
4. Identify the error in this Cypress code that tries to clear local storage:
cy.window().then(win => {
  win.localStorage.clear;
});
medium
A. Missing parentheses to call clear method
B. localStorage.clear requires a key argument
C. cy.window() cannot access localStorage
D. clear is not a function of localStorage

Solution

  1. Step 1: Check localStorage clear usage

    localStorage.clear is a function and must be called with parentheses: clear()
  2. Step 2: Identify missing parentheses error

    The code uses clear without parentheses, so it does not execute the function.
  3. Final Answer:

    Missing parentheses to call clear method -> Option A
  4. 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

  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]
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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard