Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a session in Cypress, completely

Tags:

cypress

I have this test where it logs via cy.request() in and creates the session object in before each:

    cy.request({
          method: 'POST',
          url: `${URL}/sessions`,
          body: {
            userName: 'xxx',
            password: 'yyy',
          },
        });

Then during the first test it logs out via the interface:

 cy.page()
  .get('button[logout="UserMainMenu"]')
  .should('be.lengthOf', 1).click({ force: true })...

That works until now. But then the next test starts, doing the beforeEach() again.

Then it tries to restore the session object. But can't and after a time out recreates it.

enter image description here

I would like to avoid this timeout, and started to add the following commands:

cy.clearAllCookies();
cy.clearAllLocalStorage();
cy.clearAllSessionStorage();

How ever this does not make a difference. It still recreates:

enter image description here

Current version of cypress: 12.4.1

like image 581
Jonas Avatar asked Nov 02 '25 03:11

Jonas


1 Answers

OK. I found it: to clear the complete session, it needs this command:

Cypress.session.clearAllSavedSessions()

(a logout via interface and login again (with same user) seems to inactivate the session)

like image 143
Jonas Avatar answered Nov 04 '25 03:11

Jonas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!