skip to Main Content

Situation is:

I have a bunch of cypress test spec that are depending on each other (the specification of tested project makes it impossibly to make them independent).

In the first spec I’m creating an array of objects and making it an environmental variable so I can operate on that array in other specs.

In head-full mode with the cypress UI dashboard it works pretty smoothly, but in headless mode the value of environmental variable created in one spec is ‘undefined’ in the next spec.

So

spec1.cy.ts

const arrafOfObjects = pageObject.functionThatReturnsTheArray();

Cypress.env("arrafOfObjects", arrafOfObjects);
spec2.cy.ts

const arrafOfObjects = Cypress.env("arrafOfObjects");

const length = arrafOfObjects.length //TypeError: Cannot read properties of undefined (reading 'length')
cypress.config.js

const { defineConfig } = require("cypress");
const { verifyDownloadTasks } = require('cy-verify-downloads');
const { removeDirectory } = require('cypress-delete-downloads-folder');

const env = 'staging' 

module.exports = defineConfig({

  env:{
    godUser: '[email protected]',
    passwordChargeGodUser: 'secretPassword'
    securityconfigHash: process.env.secretConfigHash
  },

  e2e: {
    baseUrl: 'https://url.' + env + '.domain.cloud',
    excludeSpecPattern : ["cypress/e2e/2-advanced-examples","cypress/e2e/1-getting-started", "cypress/e2e/0-financeportal"],
    downloadsFolder: "cypress/downloads",
    testIsolation: false,
    trashAssetsBeforeRuns : true,
    experimentalRunAllSpecs : true,
    chromeWebSecurity: false,
    experimentalModifyObstructiveThirdPartyCode: true,
    experimentalOriginDependencies: false,
    
    defaultCommandTimeout: 10000,
    numTestsKeptInMemory : 0,
    requestTimeout : 30000,
    responseTimeout : 50000,
    pageLoadTimeout: 1000000, 
    setupNodeEvents(on, config) {
      on('task', verifyDownloadTasks);
      on('task', { removeDirectory })
    },
  },

});

EDIT: I did some more investigation and it seems that headless mode is ignoring testIsolation: false configuration and is wiping out memory… any idea how it’s possible?

2

Answers


  1. Cypress.env variables are reset in each spec file. Headed mode may be doing something wonky or different than I’d expect, but it makes sense that the environment variable is undefined in the new spec file. My recommendation is to re-structure your setup so that your spec files aren’t dependent on one another – there are no assurances that your files will always run in a specific order, and it is generally best practice to have each test be as independent as possible.

    Environment variables set using Cypress.env are only in scope for the current spec file.

    Cypress runs each spec file in isolation: the browser is exited between specs. Environment variables added or changed in one spec won’t be visible in other specs.

    Login or Signup to reply.
  2. The plugin cypress-data-session has an option shareAcrossSpecs that seems to work for your scenario.

    This is an example, since you have pageObjects I added a property that is a function (in case serialization breaks functions)

    spec1.cy.js

    before(() => {
      const arrayOfObjects = [{'a':1},{'b':2},{'c': () => 3}]
      cy.dataSession({
        name: 'common', // data name
        setup: () => arrayOfObjects, // data creation commands
        shareAcrossSpecs: true,
      })
    })
    
    it('setting data session', () => {
      expect(Cypress.getDataSession('common')[0].a).to.equal(1)
      expect(Cypress.getDataSession('common')[2].c()).to.equal(3)
    })
    

    spec2.cy.js

    it('using data session in another spec', () => {
      expect(Cypress.getDataSession('common')[0].a).to.equal(1)
      expect(Cypress.getDataSession('common')[2].c()).to.equal(3)
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search