user-interfacetestingcypresscypress-custom-commands

Cypress sessionStorage session


I am using Cypress in my application, it was working in previous versions of Cypress, but now that I am using 13.15v the user session is not kept.

My test setup is as follows:

describe("Organization Membership Management", () => {
    before(() => {
        cy.session("userSession", () => {
            cy.login();
        });
    });

    beforeEach(() => {
        // More code...
    });

    it("should add user", () => {});

    it("should remove user", () => {});
});

The first it() the test works because the login works in the before(), but when it goes to the second it() the user is logged off, but I would like to use the same session in both tests if possible, because the user logged in to the application is created and deleted for each test.

I would appreciate any help. Thanks

I tried adding session for the login, but it did not work. I wanted the user session to be used in both tests.


Solution

  • If you are using cy.session() to maintain logged-in status, it must be used in beforeEach() not before().

    The session command is a cache or memoised callback where the first call runs the callback and next call restores values cached from the first call.

    But if you only ever call it once (i.e in before()) then the cache feature is not used.

    beforeEach(() => {
      // this is run before each test
      cy.session("userSession", () => {
        cy.login();
      });
    });
    
    it('calls actual cy.login() and logs in', () => 
    
    it('calls cy.session() and restores cached credentials from first call', () =>