Search code examples
javascripttypescriptlocal-storagecypressbrowser-cache

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!


Solution

  • 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!