-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cy.clearLocalStorage should clear all localStorage of all domains #2573
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 @brian-mann if you can confirm this is the current behavior -
|
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.clearCookies() has a similar issue open here: #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: cypress-io/cypress-documentation#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 |
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 In the meantime, is there some sort of workaround you'd recommend? I'm assuming it's best to call |
I am also having similar issue - Cypress not clearing the localStorage using cy.clearLocalStorage(). Any update- when are we going to fix it? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
So I have cy.visit() on a beforeEach and the cy.clearLocalStorage() on the actual test. In this case. |
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 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 👍 |
Hi, I came across with another issue where this needs to be addressed: Cookie Consent Management. Cheers and ty for the nice testing environment. |
Any update on this? |
I'm curious how this interacts with the documentation on
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 |
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 |
I'm also blocked with this issue. Anyone has found a reliable workaround? |
This comment has been minimized.
This comment has been minimized.
If you want to clear the localstorage of an iframe that is not on your domain: cy.get('#iframe-id', { log: false })
.its('0.contentWindow.localStorage', { log: false })
.then((localStorage) => localStorage.clear()); Of course this will work only with "chromeWebSecurity": false, see https://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/#application-with-iframe |
This workaround works for me: beforeEach(() => {
cy.window().then((window) => {
window.sessionStorage.clear();
window.localStorage.clear();
});
}); |
Awesome, @dominik-fielmann ! Your workaround worked for me, too, though I needed to "point" Cypress to the correct domain via cy.visit(...) upfront... |
So, after a lot of time, we realized the problem. cy.visit('subdomain.another.domain', {
onBeforeLoad(win) {
win.localStorage.clear();
},
}); I hope, this workaround helps. |
Been trying this workaround but no luck yet... trying to clear a particular local storage value of a Typeform survey. The local storage items are set against Tried your workaround above @ronhks but in my scenario Any ideas? |
@jacktomlinson: in this case, can you tried to delete exactly the spcified local storage? |
Tried that one I believe, but it doesn't clear it as the item of local storage is not on the same domain as where I am running the test from 😞. |
Hi! @jennifer-shehane . Are you planning to add the possibility to clear all storages but not the main only? Is it in the roadmap? As you can see it clears only main domain. The additional one has data even if I close the runner. It is hardly cached. |
this issue still present. I am working with multiple logins and cleaning the local storage and cookies is not working. I couldnt find a workaround but basically I have to execute case by case instead of a complete run, has someone found a workaround? |
This was the only workaround I have thought so far... is that if you can visit url you want to clear and wait for the page to load in a separate test before hand... // A workaround here as typeform stores previous results on this domain in local storage.
// Read more here: https://github.com/cypress-io/cypress/issues/2573
it('should clear typeform session storage', () => {
cy.visit('https://companyname.typeform.com/to/EP0Zx2'); // an example typeform survey
cy.wait(4000);
cy.clearLocalStorage();
}); Then run what you would like to in a second test case. |
Hello, i do see this issue is open still . Any plans for a fix of this issue? |
Facing with the same issue too |
I have the same issue too! |
Hi am facing the same issue, any helpful workaround? |
is there any update on this, please? this is causing huge issues as when we talk about E2E testing this is common stuff and common use case :( facing huge issue due to this :( |
The new |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
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()
andcy.clearLocalStorage()
- which in theory should be redundant - and then I observed this: https://youtu.be/x6Yeh6WM5fMOddly 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:
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:
cy.wait
; that was just to give me time to inspect cookie/local storage beforehand.Versions
Cypress v3.1.0, Electron 59
The text was updated successfully, but these errors were encountered: