Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Cypress: Add option to allow LocalStorage

The following facts:

  1. page 'X' Open -> cy.visit('/x');
  2. check checkbox 'A'-> cy.checkCheckbox('A');
  3. close browser or reopen page ??????
  4. check if checkbox 'A' is still checked cy.VerifyCheckBox('A', 'checked');

To step 3: If I only use the command 'cy.visit('X);' then Cypress clears the cache and my customisations are lost.

I know that there is a command 'Cypress.LocalStorage', but I don't know how exactly to use it for my case.

Thanks for your help!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I believe you can visit your page, set the local storage value, and then validate the checkbox. Roughly, it would look like:

cy.visit('/projects');
cy.window().then((window) => {
  window.localStorage.setItem('myLocalStorageItem', value);
})
cy.VerifyCheckBox('A', 'checked');

It doesn't mimic an exact user's workflow, but it works the same overall -- the checkbox is checked when a localStorage item is set to some value.

about 4 years ago · Juan Pablo Isaza Report

0

I would follow the pattern that @agoff uses, but use window:before:load event.

That way will catch any regression that clears the value during page load.

// verify that CheckboxA sets local storage
cy.visit('/projects')
cy.checkCheckbox('A')
cy.window().then((window) => {
  expect(window.localStorage.getItem('myLocalStorageItem')).to.eq(value)
})

// verify that reload retains localStorage value
cy.on('window:before:load', (win) => {      
  // should fire after Cypress clears localStorage
  window.localStorage.setItem('myLocalStorageItem', value)  
}) 
cy.reload()
cy.VerifyCheckBox('A', 'checked')
about 4 years ago · Juan Pablo Isaza Report

0

I have found a second solution for my problem. I added at the beginning of my test case (in describe, bevor it) this:

beforeEach(() => {
        cy.restoreLocalStorage().then(() => {
            cy.log('Storage restored');
        })
    });

    afterEach(() => {
        cy.saveLocalStorage().then(() => {
            cy.log('Storage saved');
        })
    });

I hope I can help you with it! Let me know it!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!