vendredi 19 mars 2021

How to set cookies in Cypress before visiting react app running on localhost

The steps I want to take are:

  1. Start the Cypress test-suite and use cy.setCookie to set the JSESSIONID cookie (already acquired and up-to-date)
  2. After the cookie is set, then use cy.visit to access the running app

The issue:

  • The cookie is not set before cy.visit runs and this causes the app redirect to an unauthorized page

What I have done so far:

Cypress.Cookies.defaults({
  preserve: 'JSESSIONID'
})

cy.setCookie('JSESSIONID', Cypress.env('JSESSIONID'), {
  path: '/',
  domain: '<domain.name>',
  httpOnly: true,
  secure: true,
  sameSite: 'no_restriction',
  log: true,
}).then(() => cy.visit('localhost:3000/<authenticated-route>')

It might be worth mentioning that <domain.name> is of the form www.staging.etc.com whereas is running locally: localhost:3000/

Any help would be greatly appreciated. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire