-
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
Clear sessionStorage in between tests #413
Comments
I am having the same issue. I need one method to clear sessionStorage. Thanks @brian-mann for the workaround. cy.window().then((win) => {
win.sessionStorage.clear()
}); |
I'm hitting this, but the workaround isn't helping. Can you provide more detail? The problem is that if I place this before cy.visit(), it seems to operate on the wrong window object, and if I place it after cy.visit(), the application code has already read sessionStorage by the time the cy.window.then() callback fires. To get a repeatable test, sessionStorage needs to be managed before the app executes. |
Browsers partition In fact the Because Cypress test code is always on the same origin as your application, modifying sessionStorage from any window will work. Another option is using the There's no reason though for this not to be working. Just place My guess is that you're not putting this code in a |
Using beforeEach doesn't help. Setting breakpoints, the issue seems to be that beforeEach runs before the previous page is unloaded, and so the app's beforeunload handler will then save to sessionStorage after it's been cleared, and before the next test starts. It's pretty confusing trying to follow how the window is being manipulated. beforeunload never fires if there's only one test. If there are two tests, beforeunload fires between the two tests. I guess this means the window is destroyed between test runs, but is reused between test cases. To get a consistent environment, then, we need to destroy the window between each test, or navigate away from the app in afterEach to trigger the beforeunload handler, then clear sessionStorage in beforeEach. Any suggestions? Update: Visiting a static page (no javascript) in afterEach does resolve the problem by coercing the beforeunload. |
This sounds like a classic anti pattern of building up state between tests. Each test should start from a clean slate and visit before each test. As I also suggested, you could use the |
Okay taking a second look I understand a bit better and can answer your questions.
The window is not re-used (that's impossible), it is nuked as per what happens whenever there is a page navigation caused by We have an open issue explaining lifecycle events and the reason they need to be refactored here. #686 It goes into a lot of detail and is related to your problem. Using afterEach hooks like that is also an anti pattern ;-) https://docs.cypress.io/guides/references/best-practices.html#Using-after-or-afterEach-hooks You really should just use the |
Ah, cool, I'll try that. afterEach had another problem, which is that if there were any pending ajax requests, they would be canceled on page navigation, which would return an ajax error 0, causing the afterEach to error out & abort the rest of the suite. The same thing happens (canceled requests) while running tests, but in that case doesn't abort the suite. Not sure why the difference, but I'll try onBeforeLoad. Update: onBeforeLoad works perfectly. |
any reasons why not add this clearing to the onBeforeLoad in the cypress itself? I think we should either have all cookies/localStorage/sessionStorage clean automatically, or none of them + deal with them manually :) Thanks for the solution though! |
Yup it's part of a much larger story here: #686 |
Would it be possible to provide an example of how to use the onBeforeLoad handler in this scenario? I'm encountering the same issues and can't figure out how to proceed. |
clearing sessionStorage are just synchronous methods you call on the cy.visit('...', {
onBeforeLoad: (win) => {
win.sessionStorage.clear()
}
}) Read https://docs.cypress.io/api/events/catalog-of-events.html as well. |
I am using the onBeforeLoad workaround to clear the session storage. When I initially open my login test in the test runner the sessionStorage does not get cleared. If I use the refresh in the test runner, the sessionStorage is cleared and works as expected. Why would it be that upon initial start up of the test runner my session storage is not being cleared? |
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.
I am still facing this and the provided solution is not helping : / Just started automating a new app, this wasn't the issue with the other app I work on. Anything else I can do to clear sessions between tests?
to index.js and cy.clearCookies(); |
This solution works for me at the start of each test |
Tried that one as well : / |
I managed to remove session Storage this way (Cypress 4.7.0, Win10 x64) (cy.clearCookies() and cy.clearLocalStorage() do not affect session storage).
https://developer.mozilla.org//docs/Web/API/Window/sessionStorage |
It worked for me, thank you @jpaquit |
When will None of the workarounds have worked here. I am even clearing session store beforeEach and onBeforeLoad. The result is tests randomly passing, definitely a race condition. One time the entire suite passes. |
We've outlined some work to be done to address some of the concerns of 'session storage' in this issue: #8301 It outlines a proposal for a 'session' API, to quickly summarize:
The session related data would include I recommend reading the entire proposal in #8301 and following there for any updates. Please feel free to express any concerns or questions in that issue concerning the API and add a 👍 for general support. |
describe('Required Object e2e test', () => {
let startingObjectsCount = 0;
before(() => {
cy.window().then(win => {
win.sessionStorage.clear();
});
cy.clearCookies();
cy.intercept('GET', '/api/objects*').as('entitiesRequest');
cy.visit(''); // is configured in cypress.json
cy.wait('@entitiesRequest').then(({ request, response }) => (startingObjectsCount = response.body.length));
cy.visit('/');
});
}) |
We released If you have any feedback about |
@jennifer-shehane |
This has not yet been fixed on the latest cypress version (9.5.2) |
This behavior will be enabled by default via test isolation when the In v12.0.0 (soon-to-be-released), we are introducing the concept of Test Isolation. There will be two modes of test isolation,
Closing this issue as done since will enable this behavior by default. Please following #24277 for the |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Currently we clear
cookies
andlocalStorage
but somehow we missed automatically clearingsessionStorage
as well.This needs to be added, and we likely need to add a new
cy.clearSessionStorage
API command.Thanks to @abadstubner for finding this one.
Currently as a workaround you can manually clear
sessionStorage
like this:The text was updated successfully, but these errors were encountered: