cypress-documentation icon indicating copy to clipboard operation
cypress-documentation copied to clipboard

For the life of me I cant figure out how to preserve a Cookie across all tests

Open gkatsanos opened this issue 2 years ago • 4 comments

Description

confusing documentation

URL of Issue(s)

none

Steps to replicate

I read and re-read the documentation on cy.session, the migration guide the changes of the Cypress.Cookies API and all. and I cant figure out what the syntax is - there's no example that shows how to simply persist a cookie across all test. Could you modify the docs, add an example or just comment here? (I'm happy to make a PR)

Browser

Chrome latest

Device

  • [ ] PC
  • [X] Mac
  • [ ] iPhone
  • [ ] iPad
  • [ ] Android Phone
  • [ ] Android Tablet

Additional Information

I read and re-read the documentation on cy.session, the migration guide the changes of the Cypress.Cookies API and all. and I cant figure out what the syntax is - there's no example that shows how to simply persist a cookie across all test. Could you modify the docs, add an example or just comment here? (I'm happy to make a PR)

gkatsanos avatar Sep 26 '23 14:09 gkatsanos

@gkatsanos cy.session restores cookies from a session that was captured and restores them as if those actions that set the cookie happened in the new test. There's no direct action to 'save this exact cookie to persist'.

  • Use cy.session to perform the actions in your application that set the cookie.
  • Use cy.session in your next test where you would expect the cookie to be present, so it restores all cookies, localStorage, etc that were present previously.

jennifer-shehane avatar Sep 27 '23 13:09 jennifer-shehane

Hm, interesting, I added this:

beforeEach(() => {
  cy.setCookie(
    "CookieConsent",
    "{stamp:%27vjpVJCpe9QeDb7Ba7mmn6GE5TtAmzBnDEVA8csu2OzT/ZGu6jV7YGw==%27%2Cnecessary:true%2Cpreferences:true%2Cstatistics:true%:%27gr%27}"
  )
})

in my e2e.js file and it worked. Should this be in documentation?

gkatsanos avatar Sep 28 '23 15:09 gkatsanos