0

I have two data files (dataUAT.json and dataQA.json) in fixtures where i'm getting data for my spec files. Suppose i have different environment like UAT and QA. I want to use dataUAT.json file for UAT environment and dataQA.json for QA Environment. Is there any way to juggle between them without altering the code? Like directly changing it from the command line?

Nischay tv
  • 21
  • 5

2 Answers2

1

Instead of modifying your plugins, you could pass in an environment variable from the command line:

cypress run --env fixtureEnvironment=UAT
cypress run --env fixtureEnvironment=QA

You could then reference this environment variable when finding the fixture.

cy.fixture(
  `data${Cypress.env('fixtureEnvironment') ?? 'QA'}`)
.then(...);

In the above, I've included a nullish coalescing operator to check for the variable being set, and if it is not, defaulting to QA.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • The expression `Cypress.env('fixtureEnvironment') ? Cypress.env('fixtureEnvironment') : 'QA'` can be simplified to `Cypress.env('fixtureEnvironment') ?? 'QA'` - see [Nullish coalescing operator (??)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) – Fody Jan 19 '22 at 23:01
  • Thanks! I wasn't familiar with that. I'll update the code :D – agoff Jan 20 '22 at 14:08
0

Cypress docs has an example for this and I also have a repo that also showcases this and cypress-grep.

Basically, you will need to make a few adjustments in your plugins/index.js by adding the following:

/ promisified fs module
const fs = require('fs-extra')
const path = require('path')

function getConfigurationByFile(file) {
  const pathToConfigFile = path.resolve('..', 'config', `${file}.json`)

  return fs.readJson(pathToConfigFile)
}

// plugins file
module.exports = (on, config) => {
  // accept a configFile value or use development by default
  const file = config.env.configFile || 'development'

  return getConfigurationByFile(file)
}
jjhelguero
  • 2,281
  • 5
  • 13