mercredi 15 mai 2019

How to set/define environment variables and api_Server in cypress?

Currently, we are using cypress to test our application. We have 2 environments with 2 different api_Servers. I want to define this inside the environment files. I am not sure how to define both the url in same file.

For example,

Environment-1:

baseUrl - https://environment-1.me/ Api_Serever - https://api-environment-1.me/v1

Environment-2:

baseUrl - https://environment-2.me/ Api_Serever - https://api-environment-2.me/v1

So few test cases depend on the baseUrl and 1 test case to check API depends on Api_Serever.

To resolve this I tried to set the baseUrl and Api_Serever inside the config file inside a plugin following this link https://docs.cypress.io/api/plugins/configuration-api.html#Usage.

I created two config files for 2 environments,

{
    "baseUrl": "https://environment-2.me/",
    "env": {
      "envname": "environment-1",
      "api_server": "https://api-environment-1.me/v1"
    }
}

Another file similar to this changing the respective endpoints.

plugin file has been modified as,

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

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

  return fs.readJson(pathToConfigFile)
}

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
    // accept a configFile value or use development by default
    const file = config.env.configFile || 'environment-2'

    return getConfigurationByFile(file)

}

inside test cases, whichever refers to the baseUrl we used visit('/')

This works fine when we run a specific file from the command line using the command cypress run --env configFile=environment-2 all the test cases pass, as the visit('/') automatically replaces with the respective environments expect the API test case.

I am not sure how the API test should be modified to call the API endpoint instead of the base URL.

Can somebody help, please?

Thanks, indhu.

Aucun commentaire:

Enregistrer un commentaire