skip to Main Content

I am testing adding and removing resource in my app. Currently it clears the cookies and does login and customer selection before each test case. My app checks if the cookies are gone then it redirects you back to login page.

describe("Resource Add / Remove", () => {
  let resourceName;
  beforeEach(() => {
    cy.visit('http://localhost/myapp')
    
    const user = {
      email: EMAIL,
      password: PASSWORD,
    };
    cy.login(user);
    cy.selectCustomer('MyCustomer')
  });

  it("should create a new resource", () => {
    resourceName = "MyResource";
    // create new resource and check if it exist
  });

  it("it should delete last created resource", () => {
    // find resource using resourceName in the list and click on delete button
    // check if it is removed from the list
  });
});

But this is really time consuming… After creating a new resource I just want to jump to next test case and skip login and customer selection from the beginning.

How to do this?

In Cypress preserveOnce is now removed.

2

Answers


  1. cy.session() should accomplish your goal of caching cookies. From the documentation:

    Cache and restore cookies, localStorage, and sessionStorage (i.e. session data) in order to recreate a consistent browser context between tests.

    In your case, it could look something like the following:

    describe("Resource Add / Remove", () => {
      let resourceName;
      beforeEach(() => {
        cy.session(EMAIL, () => {
          cy.visit('http://localhost/myapp')
        
          const user = {
            email: EMAIL,
            password: PASSWORD,
          };
          cy.login(user);
          cy.selectCustomer('MyCustomer')
        });
      });
    
      it("should create a new resource", () => {
        resourceName = "MyResource";
        // create new resource and check if it exist
      });
    
      it("it should delete last created resource", () => {
        // find resource using resourceName in the list and click on delete button
        // check if it is removed from the list
      });
    });
    

    However, Cypress recommends using cy.session() inside a custom login command.

    Login or Signup to reply.
  2. If all you want to do is preserve a cookie, I recommend cypress-v10-preserve-cookie.

    It has a very simple usage, just use it the same as the old preserveOnce().

    import 'cypress-v10-preserve-cookie'
    
    before(() => {
      // logging in here
      cy.setCookie('token', '123')
    })
    
    beforeEach(() => {
      // equivalent to cy.preserveCookieOnce('token')
      cy.preserveCookieOnce('token')
    })
    
    it('is logged in', () => {
      cy.getCookie('token')
        .should('have.property', 'value', '123')
    })
    
    it('is still logged in', () => {
      cy.getCookie('token')
        .should('have.property', 'value', '123')
    })
    
    it('is still logged in', () => {
      cy.getCookie('token')
        .should('have.property', 'value', '123')
    })
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search