Cypress config tools
This package contains plugins for loading environment variables into Cypress
Tools provided include:
- Load variables from the Crossroads Vault
- Load variables from config file (Obsolete) Cypress 4.1.0 added similar functionality. If possible, use Cypress's --configFile flag instead.
- Read JSON file from cypress/config/ Included for easy package extension
Install with
npm install crds-cypress-config --save-dev
loadConfigFromVault
This plugin adds variables to config.env from the Crossroads Vault when Cypress loads.
Environment Variables
This plugin requires the following environment variables to be set in the command line environment before Cypress is run. Since the plugin runs as part of the Cypress environment setup, passing them as config or config.env parameters when Cypress starts will not work.
VAULT_ROLE_ID
VAULT_SECRET_ID
Setup
- Create this JSON file
cypress/config/vault_config.json
and configure a list of sources for secrets and keys you want loaded. Note that this variables are non-sensitive and can be stored in github.
{
"sources": [
{
"secret": "crds-net",
"keys": ["SECRET_1"]
},
{
"secret": "crds-secret-vault-2",
"keys": ["SECRET_3", "SECRET_4"]
}
]
}
- Require this package in
cypress/plugins/index.js
and pass theconfig
value intoloadConfigFromVault
.
//cypress/plugins/index.js
const loadConfig = require('crds-cypress-config');
module.exports = (on, config) => {
return loadConfig.loadConfigFromVault(config);
};
- Set the environment variable "vaultEnv" to the vault's environment when Cypress starts.
npx cypress open --env vaultEnv=int
To confirm your file was loaded correctly when using cypress open
, check the environment variables under Settings => Configuration.
Defaults
If "vaultEnv" is undefined at startup, the plugin will attempt to load secrets from the int
Vault.
loadConfigFromFile
(Obsolete) This plugin adds variables from a JSON config file when Cypress loads.
Cypress 4.1.0 added similar functionality. If possible, use Cypress's --configFile flag instead.
Setup
-
Create a JSON file in
cypress/config/
and add your configurations. Check out Cypress's configuration documents for things that can be configured. -
Require this package in
cypress/plugins/index.js
and pass theconfig
value intoloadConfigFromFile
.
//cypress/plugins/index.js
const loadConfig = require('crds-cypress-config');
module.exports = (on, config) => {
return loadConfig.loadConfigFromFile(config);
};
- Set the environment variable
configFile
to the name of the config file (without the path or extension) when Cypress starts.
npx cypress open --env configFile=my_config
To confirm your file was loaded correctly when using cypress open
, check the environment variables under Settings => Configuration.
Defaults
If "configFile" is undefined at startup, the plugin will attempt to load the cypress/config/int_crossroads.json
file.
Load variables from file and Vault
loadConfigFromVault
can be run after loadConfigFromFile
, allowing you to set vaultEnv
in a sharable config file.
//cypress/config/int_crossroads.json
{
"baseUrl": "https://int.crossroads.net",
"env": {
"CRDS_ENV": "int",
"vaultEnv": "int"
}
}
//cypress/config/vault_config.json
{
"sources": [
{
"secret": "crds-net",
"keys": ["SECRET_1"]
}
]
}
//cypress/plugins/index.js
const loadConfig = require('crds-cypress-config');
module.exports = (on, config) => {
return loadConfig.loadConfigFromFile(config).then(loadConfig.loadConfigFromVault);
};
To run on bash:
export VAULT_ROLE_ID="..."
export VAULT_SECRET_ID="..."
npx cypress open --env configFile=int_crossroads
or on Powershell
$env:VAULT_ROLE_ID = "..."
$env:VAULT_SECRET_ID = "..."
npx cypress open --env configFile=int_crossroads
readConfigFile
This plugin helper reads a file from cypress/config/
and returns a promise with its contents in object form. It must be given the file's name without extension.
It will only read JSON files in cypress/config/
and will return a rejected promise if the file is not found.
//cypress/plugins/index.js
const loadConfig = require('crds-cypress-config');
module.exports = (on, config) => {
return loadConfig.readConfigFile(config.configFile).then(response => {
config.env["CRDS_ENV"] = response.env["CRDS_ENV"];
return config;
});
};