Home > Software design >  cypress unable to clear localstorage object
cypress unable to clear localstorage object

Time:08-17

I am performing some cypress tests, I want to clear all of the localStorage, but this second object 'https://app.usercentrics.eu' used for saving "User consent choices" can't be cleared by using cy.clearLocalStorage() localStorage.clear() or localStorage.removeItem() localStorage screen capture

Has anyone encountered a similar issue and have an idea on how to deal with this kind of issue ?

CodePudding user response:

The mentioned functions I believe are specific to the domain you are currently in. In order to clear another domain's localStorage, you'd have to switch to that domain

cy.visit('/mySite').clearLocalStorage()
  .visit('/otherDomain').clearLocalStorage();

If you find yourself using this often, consider adding it as a Cypress custom command.

CodePudding user response:

I was trying the same thing on the previous question, i.e to get the consent button to show consistently for each test run.

This seems to work,

cy.origin('https://app.usercentrics.eu', () => {
  localStorage.clear();
})
cy.visit('https://www.deepskydata.com/')

cy.get('#usercentrics-root')
  .shadow()
  .find('[data-testid="uc-accept-all-button"]')
  .click()

I also start the test with cy.viewport(1000,4800) which makes it easier to see the button appearing at the bottom of the page.

  • Related