cypress-io / github-action

GitHub Action for running Cypress end-to-end & component tests

Home Page:https://on.cypress.io/guides/continuous-integration/github-actions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Session / Cookies not cleared between tests

thibautvdu opened this issue · comments

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 ?

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

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

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

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

@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

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.

@thibautvdu