ElVincitore
ElVincitore

Reputation: 375

cypress unable to clear localstorage object

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 ?

Upvotes: 2

Views: 497

Answers (2)

Fody
Fody

Reputation: 31872

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.

Upvotes: 3

agoff
agoff

Reputation: 7125

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.

Upvotes: 0

Related Questions