Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 6 tasks
gkatsanos opened this issue Sep 26, 2023 · 4 comments
Open
1 of 6 tasks

Comments

@gkatsanos
Copy link

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
  • 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 gkatsanos changed the title For the life of me I cant figure out how to preserve a Cookie across all test. For the life of me I cant figure out how to preserve a Cookie across all tests Sep 26, 2023
@jennifer-shehane
Copy link
Member

@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.

@gkatsanos
Copy link
Author

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?

@Gemini-0529
Copy link

Why is localStorage's data missing in the next it block, causing a redirection to the login page. @jennifer-shehane Help me with this
( There is no code in the code to intentionally clear localStorage )

it('Login', () => {
    ......
})
it('visit user page', () => {
  // Data in localStorage is deleted, causing the jump to fail
  cy.visit('/user')
})

@yashtibrewal
Copy link

Why is localStorage's data missing in the next it block, causing a redirection to the login page. @jennifer-shehane Help me with this ( There is no code in the code to intentionally clear localStorage )

it('Login', () => {
    ......
})
it('visit user page', () => {
  // Data in localStorage is deleted, causing the jump to fail
  cy.visit('/user')
})

This is because cypress automatically clears all cookies before each test (it block) to ensure a clean state, unless you configure it otherwise. This behavior is part of Cypress's automatic test isolation, which helps to avoid side effects between tests.

One workaround I found is, you can have the login functionality in beforeEach block (write the login code there) so that the it blocks gets hydrated with the expected cookies, or you can set the cookies manually in the beforeEach block.

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants