Clear session storage workaround is not consistently functioning
See original GitHub issueCurrent behavior:
While logging in targeting the User Interface I have to clear session storage because we store route history in there. The first time the test is run and the initial test runner browser is opened, but the session storage is not cleared. Refreshing the test without closing the browser lets the test succeed the second time and any time after that as long as you don’t close the test runner browser window.
I am using the clear session storage workaround by using
cy.visit(activeEndpoint, { onBeforeLoad: (win) => { win.sessionStorage.clear(); } });
Desired behavior:
The expected behavior is that session storage is cleared using win.sessionStorage.clear() on the initial firing of my login test allowing me to visit the login page and be able to log. It’s bothersome to have to run the test a few times to get it to succeed.
Steps to reproduce:
describe('Logs In Using the UI', () => {
beforeEach('Clear session storage', () => {
cy.visit(url, {
onBeforeLoad: (win) => {
win.sessionStorage.clear();
}
});
});
it('Logs in to url using the UI', () => {
cy.get(loginSelectors.usernameInput).type('username');
cy.get(loginSelectors.passwordInput).type('password');
cy.get(loginSelectors.loginButton).click();
waitOnLoginXhr();
});
});
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:17 (13 by maintainers)
Top GitHub Comments
I was under the impression that Cypress clears local storage and cookies (ie session state) before each test. However, I have noticed that since switching to 3.0 we occasionally see session state “leak” between tests, which causes flakiness.
I can provide a link to our (private) dashboard if it helps.
We released
cy.session()
as an experimental command (setexperimentalSessionSupport
totrue
in config) in Cypress 8.2.0. This command can be used to cache and restore cookies,localStorage
, andsessionStorage
. We especially see this command as being useful for testing Auth flows and recommend giving it a try to replace your existing tests around authentication and login.If you have any feedback about
cy.session()
for general UX improvements, new features, or unexpected behavior, please leave your feedback in our Cypress Session Experimental Feedback Discussion.