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

In environment configuration file, add the custom variables in deep structures #3157

Closed
chuchu1313 opened this issue Jan 17, 2019 · 5 comments

Comments

@chuchu1313
Copy link

chuchu1313 commented Jan 17, 2019

Current behavior:

To define an environment variable, in the config file (e.g. cypress/config/development.json) I can add variables on the first level of JSON format.
The second level needs to be named env, like below

{
  "execTimeout": "120000",
  "env": {
       "CC_COOKIES": "aaa"
  }
}

Desired behavior:

Assumed I have two environments (development & qa), and have two config files to define the different environment variables, but still needs to test in three different countries in each environment, and the variable is different depending on the countries.
The desired config structure may be like below
e.g: development.json

    {
      "execTimeout": "120000",
      "id_env": {
          "CC_COOKIES": "aaaa"
           ...
      }, 
      "tw_env":{
          "CC_COOKIES": "bbbb"
           ...
      }, 
      "hk_env":{
          "CC_COOKIES": "cccc"
           ...
      }
    }

e.g: qa.json

    {
      "execTimeout": "120000",
      "id_env": {
          "CC_COOKIES": "dddd"
         ...
      }, 
      "tw_env":{
          "CC_COOKIES": "eeee"
           ...
      }, 
      "hk_env":{
          "CC_COOKIES": "ffff"
           ...
      }
    }

So I can get the variables based on the environment and countries, it could be more flexibility for configs.
Current behavior needs to create 6 different files
(development_id.json, development_tw.json, development_h.jsonk, qa_id.json, qa_tw.json, qa_hk.json)

@chuchu1313 chuchu1313 changed the title In environment configuration file, add the custom varible In environment configuration file, add the custom variables in deep structures Jan 17, 2019
@flotwig
Copy link
Contributor

flotwig commented Jan 17, 2019

You can do this by modifying your plugins file (by default, in cypress/plugins/index.js). In there, you can dynamically modify the configuration at run-time by using your own logic and returning a modified config object. Please see the documentation for details:

https://docs.cypress.io/api/plugins/configuration-api.html

@chuchu1313
Copy link
Author

chuchu1313 commented Jan 18, 2019

Hello @flotwig, thanks for your reply.
I'm dynamic get my config just like below example

    // 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 || 'beta'
    
      return getConfigurationByFile(file)
    }

And my beta.config is like below:

    {
      "execTimeout": "120000",
      "id_env": {
          "CC_COOKIES": "aaaa"
           ...
      }, 
      "tw_env":{
          "CC_COOKIES": "bbbb"
           ...
      }, 
      "hk_env":{
          "CC_COOKIES": "cccc"
           ...
      } 
    } 

In my case, I put id_env, tw_env and hk_env in beta.config is because they are in the same environment(beta). But it is not working in this example. Only when I modify to env it will work.

    {
      "execTimeout": "120000",
      "env": {
           ...
      },
    } 

But which can not be separated define the env variables by different mateix (here is country).
Is there any way that I can use to define different variables by using JSON structures?

@flotwig
Copy link
Contributor

flotwig commented Jan 18, 2019

If I'm understanding correctly, you want to run your tests 6 times in total, 3 times in each environment, once for each country?

If this is the case, no, there is not currently a way to do this with just a static config file. You have some options.

  1. You can create a plugin file that dynamically switches between environment and country based on environment variables. Then, you can iterate through your countries in your continuous integration or manually by specifying environment variables on the command line.

  2. Or, you could modify your specfiles to create three versions of your tests at runtime. This will probably be easier to deal with because you can run all your tests at once in the GUI.

['hk', 'tw', 'id'].forEach((country) => {
    describe(`${country}...`, () => {
        // your tests
    })
})

@jennifer-shehane
Copy link
Member

@chuchu1313 You could probably utilize our Module API to do what you want. https://on.cypress.io/module-api

@jennifer-shehane jennifer-shehane added the stage: awaiting response Potential fix was proposed; awaiting response label Jan 23, 2019
@chuchu1313
Copy link
Author

chuchu1313 commented Jan 24, 2019

@flotwig @jennifer-shehane Thanks for your reply.
I use Module API, and looks like below, which works and can do what I want.

// cypress_run.js

const env = process.argv[2]
const country = process.argv[3]

fs.readFile('cypress/config/' + env + '.json', 'utf8', function (err, data) {

  const configs = JSON.parse(data)

    cypress.run({
        spec: runFiles,
        env: configs[country]
    })
})

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

3 participants