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

Session / Cookies not cleared between tests #1023

Closed
thibautvdu opened this issue Sep 7, 2023 · 7 comments
Closed

Session / Cookies not cleared between tests #1023

thibautvdu opened this issue Sep 7, 2023 · 7 comments

Comments

@thibautvdu
Copy link

thibautvdu commented Sep 7, 2023

I use a custom login command, as follow :

Cypress.Commands.add("login", (email: string) => {
  cy.session(`login ${email}`, () => {
    const now = Date.now();

    cy.visit("/login");
    cy.get('[data-test="login-email"]').type(email);
    cy.get('[data-test="login-submit"]').click();
   ...
  });
});

When running locally, with cypress:run, everything is working perfectly fine and the tests start with a signed out user regardless of the previous ones. However when running it through github actions, the signed in session persists between my tests and thus create chaos all around. Is there any additional configuration necessary to ensure test isolation with this github action ?

@MikeMcC399
Copy link
Collaborator

@thibautvdu

github-action should not affect Test Isolation unless you have accidently changed the configuration in your workflow.

Can you post your workflow? Otherwise it would probably need a full reproducible example in order to investigate.

@thibautvdu
Copy link
Author

thibautvdu commented Sep 7, 2023

Thanks for your answer @MikeMcC399
Here is my workflow :

name: ci preview
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows
on: [deployment_status]
jobs:
  e2e:
    # only runs this job on successful deploy
    if: github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success' && github.event.deployment_status.environment == 'Preview'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 🛎
        uses: actions/checkout@v3
      - name: Install Vercel CLI
        run: npm i -g vercel@latest
      - name: Link preview alias url to new deployment
        run: vercel alias --token=${{ secrets.VERCEL_TOKEN }} --scope=${{secrets.VERCEL_ORG_ID}} set ${{ github.event.deployment_status.target_url }} ${{ secrets.PREVIEW_URL }}
      - name: Run Cypress 🌲
        uses: cypress-io/github-action@v6
        with:
          record: true
        env:
          CYPRESS_BASE_URL: https://${{ secrets.PREVIEW_URL }}
          CYPRESS_IMAP_EMAIL: ${{ secrets.CYPRESS_IMAP_EMAIL }}
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
      - name: Upload Cucumber Report
        run: npm run cypress:upload

It is worth mentioning I'm using https://github.com/badeball/cypress-cucumber-preprocessor ; however since test isolation is working perfectly fine with it locally I do not suspect it to be at cause. Login is performed through firebase authentification

@MikeMcC399
Copy link
Collaborator

@thibautvdu

I don't see any problem with your workflow which could cause this issue.
(Just one unrelated comment, that you probably don't need -g when you install vercel.)

Which version of Cypress are you using?

@thibautvdu
Copy link
Author

thibautvdu commented Sep 7, 2023

@MikeMcC399

I am using the latest version ( 13.1.0 ).
I made some progress ; it seems to be linked to the following issue :
#cypress-io/cypress#1208

I could check that the session local storage was cleared correctly in both cases, but somehow, the indexed databases are only cleared when I run cypress locally, but not through github actions.
I applied the fix mentioned in #cypress-io/cypress#1208 (comment)

Cypress.Commands.add("clearIndexedDB", async () => {
  const databases = await window.indexedDB.databases();

  await Promise.all(
    databases.map(
      ({ name }) =>
        new Promise((resolve, reject) => {
          const request = window.indexedDB.deleteDatabase(name as string);

          request.addEventListener("success", resolve);
          // Note: we need to also listen to the "blocked" event
          // (and resolve the promise) due to https://stackoverflow.com/a/35141818
          request.addEventListener("blocked", resolve);
          request.addEventListener("error", reject);
        }),
    ),
  );
});

Now I'm left with another issue : restoring the session works locally but not on the github action ; my guess is that in the same way that the indexed dbs are not deleted between tests, they are not restored either on github. Any help with that last issue, or a cleaner way to deal with the first, are welcome !

@MikeMcC399
Copy link
Collaborator

@thibautvdu

The Cypress documentation does not cover the use of IndexedDB at all. It does however refer to the related @this-dot/cypress-indexeddb plugin in the Plugins > Custom Commands section. You might find that plugin useful.

You may find users with related experience in the Cypress technical community on Discord who could help with your "how-to" questions:

Discord chat
(click on button)

It does not look like your issue is caused by github-action itself as the action's calls corresponding to your workflow are basically just equivalent to:

npm ci
npx cypress run --record

@thibautvdu
Copy link
Author

Thanks for your answers @MikeMcC399

I will close this issue since it is now a duplicate of cypress-io/cypress#18350 , which mention the exact same need of having indexed dbs handled by cypress sessions and offer a workaround.

Strangely I still confirm that the indexed dbs get cleared upon each test run locally on my machine and not on github, with both electron and chrome, and that they also get restored along with cy.session. That last point doesn't need further investigation on my side since I have to implement a workaround for github anyway.

@MikeMcC399
Copy link
Collaborator

@thibautvdu

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

No branches or pull requests

2 participants