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

Cypress does not clear cookies and localStorage on startup when testIsolation is false #28935

Closed
nbouvrette opened this issue Feb 13, 2024 · 5 comments
Labels
stage: needs information Not enough info to reproduce the issue stale no activity on this issue for a long period topic: cookies 🍪

Comments

@nbouvrette
Copy link

nbouvrette commented Feb 13, 2024

Current behavior

I am starting tests, and I would expect that when launching Chrome is starts without any cookies (and local storage) but it keeps the same infromation as the previous test run.

This issue only seems to happen when setting testIsolation to false since otherwise cookies and localStroage will be cleared before every test.

Desired behavior

I would expect that when launching Cypress, the environment starts without any state from previous test runs.

Test code to reproduce

Just install the latest Cypress version, run test on an application that has cookies. Close the test, re-open Cypress and inspect your cookies.

Cypress Version

latest

Node version

v20.5.0

Operating System

macOS 14.2.1

Debug Logs

No response

Other

No response

@jennifer-shehane
Copy link
Member

How are you closing the test in this case?

Close the test, re-open Cypress and inspect your cookies.

@jennifer-shehane jennifer-shehane added stage: needs information Not enough info to reproduce the issue topic: cookies 🍪 labels Feb 13, 2024
@nbouvrette
Copy link
Author

nbouvrette commented Feb 13, 2024

@jennifer-shehane I tried both closing Chrome and Cypress "normally" and also doing a CTRL-C in my console - both arrive at the same result in my case which is that session cookies are cleared but cookies with an expiry date from the previous tests are still there, including the localStorage

This is even worse for cookies that are set by API calls (httpOnly) since Cypress cannot clear those cookies as far as I am aware... so the only way to get Cypress back into a usable state is to manually clear cookie in the browser as far as I am aware.

@nbouvrette
Copy link
Author

Just to clarify, this issue only occurs when using testIsolation: false. Here are more details on how to replicate:

before(() => {
    console.log('getAllCookies')
    cy.getAllCookies()
      .then((cookies) => {
        console.log(`inside getAllCookies`)
        console.dir(cookies, { depth: null })
      })
      .then(() => {
        cy.clearAllCookies().then(() => {
          console.log(`after clearAllCookies`)
        })
      })
      .then(() => {
        cy.getAllCookies().then((cookies) => {
          console.log(`inside getAllCookies (after clearAllCookies)`)
          console.dir(cookies, { depth: null })
        })
      })
      .then(() => {
        cy.visit(`/`)
      })
      .then(() => {
        cy.getAllCookies().then((cookies) => {
          console.log(`inside getAllCookies (after visiting page)`)
          console.dir(cookies, { depth: null })
        })
      })
      .then(() => {
        throw new Error('stop')
      })
  })

Of course, in this example, visiting / will set cookies - here is what my console looks like when running this on the first run:

image

Now, if I stop Cypress, and restart it and run the same test again:

image

We can see that even at startup, before visiting any page, it still has a cookie.

I tried closing Cypress by quitting Chrome and Cypress, by using the "Close" button in the Cypress app, and using CTRL-C in the console, and they all ended up with the same result.

I think it would be expected that when Cypress starts, it clears all cookies and local storage without having to do it manually or without having to use sessions or test isolation.

Otherwise, this means that if any test fails and we restart Cypress, the previous browser state will be persisted, and it's impossible to guarantee reliable tests.

Right now, I am planning to use session as a workaround, but I still think that this behavior is probably not expected.

I'm also updating the issue with those new details for clarity.

@nbouvrette nbouvrette changed the title Cypress does not clear cookies on startup Cypress does not clear cookies and localStorage on startup when testIsolation is false Feb 17, 2024
@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Aug 16, 2024
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: needs information Not enough info to reproduce the issue stale no activity on this issue for a long period topic: cookies 🍪
Projects
None yet
Development

No branches or pull requests

3 participants