1

In latest version of Cypress Cookie preserve is deprecated so I wish to us cy.session. However I can't get this to work across multiple tests as cy.session needs to be inside a test 'it', example of how my tests are currently set up.

    beforeEach(() => {
    Cypress.Cookies.defaults({
    preserve: /somecookie|someOtherCookie|AnotherCookie/
    })
   it('Navigate to URL', () => {
        performance.NavigateToUrl(URL);
    });

    it('Login - Username/Pass', () => {
        performance.LoginUserPass();
    });        
    it('Navigate Order Page', () => {
        performance.Orderpage();
    });
     //And so on............

Any help and suggestions welcome as i don't really want to rewrite the test structure as i create a report on it current output/design.

for the session to be kept across all tests

2 Answers2

4

With recent changes, Cypress are trying to "enforce" test isolation more, and you are going against their best practice by having dependency across the different it() blocks.

A better approach would be to structure according to context

context('before logging in', () => {

   it('Can navigate to home page', () => {
     ...
   })

   it('Can log in', () => {
     ...
   })  
})

context('after logging in', () => {

  beforeEach(() => {
    cy.session('login', () => {
      // do login via cy.request()
      // preserve all browser login data such as cookies, localstorage
    })
  })

  it('Can use order page', () => {
    ...
  })
})

Test isolation flag

There is a note on the cy.session() page that indicates you can turn off test isolation, but the paragraphs are a bit vague IMO

The page is cleared before setup when testIsolation is enabled and is not cleared when testIsolation is disabled.

Cookies, local storage and session storage in all domains are always cleared before setup runs, regardless of the testIsolation configuration.

So, it's worth trying with setting testIsolation: false - but don't call cy.session() at all.

0

I think you could use the cy.session with the callback function in a before hook with both the visit and login methods:

const sessionId = 'Login with valid credentials'

before(() => {
  cy.session(sessionId, () => {
    performance.NavigateToUrl(URL)
    performance.LoginUserPass()
  })
})

Then restore the session and visit the page again in a beforeEach hook.

beforeEach(() => {
  cy.session(sessionId)
  performance.NavigateToUrl(URL)
})

it('Navigate Order Page', () => {
  performance.Orderpage();
})
//And so on............
Leomelzer
  • 135
  • 7