Cypress keeps old session logged in even after clearLocalStorage() and clearCookies() and window.SessionsStorage().clear()
See original GitHub issueHi Team,
Current behavior
I have 3 spec files and in each one I have a before hook to clear localstorage, session and cookies then login to the app
before(function () {
cy.clearCookies()
cy.clearLocalStorage()
cy.visit('/people/users', {
onBeforeLoad: (win) => {
win.sessionStorage.clear()
},
})
})
When I run the tests Cypress keeps the old session logged in and redirects to the users page as it finds the token somewhere in cache every time and login to the old session.
So what happens :
1 - First attempt : run all specs, all fails as when cypress launches the app, it’s already signed in
2 - Clear App Data manually from Cypress then restart Cypress
3 - Second attempt : run all specs, first spec and the login in the before hook pass, but second and third specs fails on the login step as cypress kept the session of the first spec executed
Closing and starting cypress did not help too
The only thing it helped is clearing the app data manually in Cypress (deleting the production folder under cy).
the tested App is running locally It uses SSO for login (an outh url redirection to enter the username and password and then it sets the oauth token in the localstorage…)
Desired behavior
Cypress should clear the app data between specs execution.
Versions
Machine : Mac with Catalina 10.15.7 Cypress latest version happens in all browsers, Chrome, Firefox for devs, Edge and Electron
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Right now there doesn’t seem to be enough information to reproduce the problem on our end. We’ll have to close this issue until we can reproduce it. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.
Please comment in this issue with a reproducible example and we will consider reopening the issue. Here are some tips for providing a Short, Self Contained, Correct, Example and our own Troubleshooting Cypress guide.
I have the same issue. I’m running
cypress run
via an npm script (which is itself launched from my NUnit C# tests, but that shouldn’t matter I think) and my tests sometimes fail, sometime pass.I look at the recorded video and it’s because the first
cy.visit('/')
is loading the “already logged in page” instead of the login page itself.My test file structure is pretty simple (see below) as I’m still new to Cypress. I couldn’t deduce a pattern to which tests fail. It’s not always the same one.