Cypress: cy.clearLocalStorage should clear all localStorage of all domains

Created on 8 Oct 2018  路  18Comments  路  Source: cypress-io/cypress

Summary of this thread so far

cy.clearLocalStorage doesn't behave the way I assumed it would. It doesn't clear _all_ local storage. The docs have since been updated to reflect this.


Current behavior:

I was trying to write a simple test for some to do list website when I noticed that certain state wasn't being cleared correctly between tests. So I added some sanity checks like cy.clearCookies() and cy.clearLocalStorage() - which in theory should be redundant - and then I observed this: https://youtu.be/x6Yeh6WM5fM

Oddly enough, it looks like local state is magically reappearing for reasons I don't understand. Any idea what's going on? Here's the code snippet I executed:

describe('Hello world', () => {
    before(function () {
        cy.viewport(1200, 660)
        cy.clearCookies()
        cy.clearLocalStorage()
        cy.wait(20000)
        cy.visit('https://www.any.do/').contains('Login').click()
        cy.contains('ALREADY A MEMBER?', {
            'timeout': 10000
        }).click()
        cy.get('#emailSignin #email').type('[email protected]')
        cy.get('#emailSignin #password').type('badpassword')
        cy.contains('SIGN IN').click()
    })

    it('Is a test', () => {
        cy.log('Look!')
    })
})

Even going into the developer tools, Application tab, clicking 'Clear storage' and then clearing everything doesn't reset it. The only workaround I have found is selecting the local storage data for https://web.any.do and clicking 'Clear All'.

Desired behavior:

The old local storage data should not reappear when the test reaches the domain https://web.any.do.

Steps to reproduce:

  1. Run this script with valid credentials. I've changed my example account's password, but you can use [email protected] with password 'anydone'. The script should work even without the cy.wait; that was just to give me time to inspect cookie/local storage beforehand.
  2. The test will pass.
  3. Rerun the test.
  4. The test will fail because it suddenly remembers the login.

Versions

Cypress v3.1.0, Electron 59

documentation ready for work feature user experience

Most helpful comment

I am also having similar issue - Cypress not clearing the localStorage using cy.clearLocalStorage().

Any update- when are we going to fix it?

All 18 comments

I believe what is happening is that when Cypress clears localStorage, it only clears local storage for the domain and subdomain that was used in cy.visit(), so you will see localStorage cleared for https://www.any.do/ but not for https://web.any.do.

@brian-mann if you can confirm this is the current behavior -

  1. I would like to specify this in our documentation
  2. Is it possible for us to clear localStorage on other subdomains/domains?

Interesting... if this is correct it'd definitely be good to add to the documentation. But ideally I'd like _all_ local storage to be cleared by this command (or a similar one).

Yes, I did a test to verify this is the behavior - that cy.clearLocalStorage() only clears for the current domain (including subdomain).

cy.clearCookies() has a similar issue open here: https://github.com/cypress-io/cypress/issues/408

it('opens the page', function () {
  cy.visit('https://www.cypress.io').then((win) => {
    win.localStorage.setItem('foo', 'bar')
    expect(win.localStorage.getItem('foo')).to.eq('bar')
  })
  cy.visit('https://www.cypress.io/dashboard/').then((win) => {
    expect(win.localStorage.getItem('foo')).to.eq('bar')
  })
  cy.visit('https://docs.cypress.io').then((win) => {
    expect(win.localStorage.getItem('foo')).to.eq('bar') // fails
  })
})

I created a new issue in our docs to document cy.clearLocalStorage only clearing it for the current domain here: https://github.com/cypress-io/cypress-documentation/issues/1034. Our documentation is open source and contributions are welcome. 馃槃

Thank you for your analysis!

I see that a documentation update has already been released. Assuming I'm able to figure out how to alter the behavior of cy.clearLocalStorage so that it clears everything, would you and the other maintainers be open to that? Or is there a specific reason for the current behavior?

Oh, yes, we agree it should clear localStorage for all domains, or at least be controllable in some way to choose which domains they're cleared for.

I'm having lots of trouble setting up a development environment for the cypress repo (the npm install command fails), so I think I'll leave this code change up to you guys.

In the meantime, is there some sort of workaround you'd recommend? I'm assuming it's best to call cy.visit and then run cy.clearLocalStorage or win.localStorage.clear for every website I'd like to clear the storage of?

I am also having similar issue - Cypress not clearing the localStorage using cy.clearLocalStorage().

Any update- when are we going to fix it?

I'm also having this issue, is there an update ??

any update or workaround ?

This is still an issue,

So I have cy.visit() on a beforeEach and the cy.clearLocalStorage() on the actual test. In this case.
It clearly does not reflect, what it says and I'm having a little staging/production miss-match:
I wonder, if there is a way to clear cookies and local storage when ending the browser as one might need a clean browser to start the tests.

I really need this feature as well. I was looking at the codebase of the function, but I'm not an expert of coffee script and it seems like it is the same as adding a custom command. So I rather tried to implement it in my project. My idea was to use the window.frames or to select all the iframes in the page to execute the localStorage.clear() on every window. But it doesn't seems to work, I don't know why.

Maybe you can tell me why this approach is apparently wrong and how should I approach it so everyone can have this feature, which is by the way, very needed in case of OAuth.

Thank you in advance the Cypress team 馃憤

I was getting burned by this as well. We run on a different domain during development. You could try:

cy.on('window:before:load', window => {
    window.localStorage.clear();
});

Not sure if it's the right approach, but I ended up just writing an overwrite so that cy.clearLocalStorage it had the desired effect

Cypress.Commands.overwrite('clearLocalStorage', () => {
  cy.on('window:before:load', window => {
    window.localStorage.clear();
  });
});

Hi,

I came across with another issue where this needs to be addressed: Cookie Consent Management.
Would be glad if there is any solution around this?
Otherwise I've the feeling this is a dealbreaker :S

Cheers and ty for the nice testing environment.

Any update on this?

I'm curious how this interacts with the documentation on cy.clearLocalStorage:

Cypress automatically runs this command before each test to prevent state from being shared across tests. You shouldn鈥檛 need to use this command unless you鈥檙e using it to clear localStorage inside a single test.

I'm seeing behavior where adding a global:

beforeEach(() => {
  cy.clearLocalStorage();
});

is changing a test from flaky to not flaky. Specifically, it looks like there are stale login tokens hanging around in localStorage between tests. When I run the test in isolation (it.only() for only that test), it passes 100% of the time. When I run the test alongside one other test that logs in to the app (put both in a describe.only() block together), the same test fails >50% of the time.

I also tried adding a:

beforeEach(() => {
  cy.visit('/');
});

based on this issue. In some tests, my team was setting local storage before visiting a URL, which I realized might be confusing given that local storage operates on a per-domain basis. In conjunction with this bug, it seemed like it was possible that this meant the local storage wasn't getting correctly cleared between tests because the local storage we were setting wasn't attached to a domain. In practice, though, that didn't matter and the test was still flaky even with that.

(In retrospec, it makes sense that this would have no effect: clearly Cypress has to have _some_ concept of what domain to attach the local storage entries to even before a cy.visit() call, or else the entries that we were setting (e.g. login tokens) wouldn't have had the intended effects in our tests.)

I'm also blocked with this issue. Anyone has found a reliable workaround?
@zeptonaut the issue description you gave matches exactly what I'm seeing.

Was this page helpful?
0 / 5 - 0 ratings