Adam
Adam

Reputation: 4780

Cypress resets to cy.visit() after running each describe section

I have a spec file in Cypress below and every time it runs the spec, the "Analyze Section" succeeds but the "Other Section" fails due to it returning to the login page even though the before() hook at the root should just run once based on the level of nesting. I'm trying to make it so the login happens one time whenever any tests in this suite are run. Likewise, when any test in the "Analyze Section" are run we click the #HyperLinkAnalyze link one time to ensure we are on the proper page for any test. I was trying to make them cascade down but the beforeEach() call in each section ends up popping the page back out to the login page that happened in before().

context('Admin - Analyze Tab', { tags: ['@admin'] }, () => {
  let user;

  before(() => {
    cy.visit(Cypress.env('admin_url'));
    user = Cypress.env('admin_user');
    cy.login(user.email, user.password);
  });

  describe('Analyze Section', ()=>{
    beforeEach(() => {
      cy.get('#HyperLinkAnalyze').click();
      cy.get('#HyperLinkCampaignStats').click();
    });

    it('TEST 1', {}, () => {
      cy.contains('#analytics-row1', 'Response Rate').should('be.visible');
    });

    it('TEST 2', {}, () => {
      cy.contains('#analytics-row1', 'Response Rate').should('be.visible');
    });
  });

  describe('Other Section', ()=>{
    beforeEach(() => {
      cy.get('#HyperLinkAnalyze').click();
      cy.get('#HyperLinkXSellStats').click();
    });

    it('TEST 1', {}, () => {
      cy.contains('#analytics-row1', 'Response Rate').should('be.visible');
    });

    it('TEST 2', {}, () => {
      cy.contains('#analytics-row1', 'Response Rate').should('be.visible');
    });
  });
});

Upvotes: 0

Views: 1277

Answers (1)

Rahul L
Rahul L

Reputation: 4349

You can try Cypress.session.

The new cy.session() command solves problem by caching and restoring cookies, localStorage and sessionStorage after a successful login. The steps that your login code takes to create the session will only be performed once when it's called the first time in any given spec file. Subsequent calls will restore the session from cache.

Set experimentalSessionSupport flag to true in the Cypress config or by using Cypress.config() at the top of a spec file.

Check below example -

const loginWithSession = () => {
      cy.session(() => {
         cy.visit(Cypress.env('admin_url'));
         let user = Cypress.env('admin_user');
         cy.login(user.email, user.password);// Can be parameterize 
       });
      cy.visit(Cypress.env('admin_url'));//Or home page url
    })
    }
    


context('Admin - Analyze Tab', { tags: ['@admin'] }, () => {
      
      before(() => {
        loginWithSession();
      });

  
      describe('Analyze Section', ()=>{
        beforeEach(() => {
          loginWithSession();
          cy.get('#HyperLinkAnalyze').click();
          cy.get('#HyperLinkCampaignStats').click();
        });
    
        it('TEST 1', {}, () => {
          cy.contains('#analytics-row1', 'Response Rate').should('be.visible');
        });
    
        it('TEST 2', {}, () => {
          cy.contains('#analytics-row1', 'Response Rate').should('be.visible');
        });
      });
    
      describe('Other Section', ()=>{
        beforeEach(() => {
          loginWithSession();
          cy.get('#HyperLinkAnalyze').click();
          cy.get('#HyperLinkXSellStats').click();
        });
    
        it('TEST 1', {}, () => {
          cy.contains('#analytics-row1', 'Response Rate').should('be.visible');
        });
    
        it('TEST 2', {}, () => {
          cy.contains('#analytics-row1', 'Response Rate').should('be.visible');
        });
      });
    });

In older version of Cypress you can use https://docs.cypress.io/api/cypress-api/cookies#Preserve-Once to preserve the cookies and Cypress will not clear it .

beforeEach(() => {
   Cypress.Cookies.preserveOnce('session_id', 'remember_token')
})

Upvotes: 0

Related Questions