javascript - 覆盖来自 cypress.env.json 的配置变量

标签 javascript configuration cypress

TL;博士:

我正在尝试覆盖 baseUrl来自 cypress.json 的值使用我的 cypress.env.json文件,但我似乎无法弄清楚如何。有没有办法做到这一点?

背景

设置环境变量 cypress.json文件,然后在 cypress.env.json 中覆盖它们为 easy as pie .在 cypress.json :

{
  "env": {
    "someVariable": "originalValue"
  }
}

... 并在 cypress.env.json :
{
  "someVariable": "newValue"
}

关于配置变量 , the documentation states :

If your environment variables match a standard configuration key, then instead of setting an environment variable they will instead override the configuration value.



但是,如果我尝试设置 baseUrl来自 cypress.json ...
{
  "baseUrl": "http://example.com/setFromCypress.json",
  "env": {
    "someVariable": "originalValue"
  }
}

... 并在 cypress.env.json 中覆盖它...
{
  "baseUrl": "http://example.com/setFromCypress.env.json",
  "someVariable": "newValue"
}

... 然后 someVariable被覆盖,但现有的 baseUrl保持不变(baseUrl 出现在放置在 env 键处的对象内):

A picture of the Cypress configuration when setting baseUrl from both cypress.json and cypress.env.json

设置baseUrl时没有问题在 cypress.json然后在命令行中使用 CYPRESS_BASE_URL 覆盖它:
$ export CYPRESS_BASE_URL=http://example.com/setFromCommandLine

那么,原baseUrl被覆盖:

A picture of the Cypress configuration when setting baseUrl from cypress.json, cypress.env.json and the command line

总结一下:我是在文档中遗漏了什么,还是在文档中遗漏了什么?

最佳答案

一个简单的解决方法:在 plugins/index.js 中做

module.exports = (on, config) => {
  if(config.hasOwnProperty('env') &&  config.env.hasOwnProperty('baseUrl')){
      config.baseUrl = config.env.baseUrl;
  }
  return config
}

关于javascript - 覆盖来自 cypress.env.json 的配置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47262338/

相关文章:

python - 你如何将 bash 别名移植到 ipython > 0.10?

vue.js - Vue Cypress 和 Gitlab CI/CD

cors - Web worker 被 Cypress 上的 self.crossOriginIsolated 阻止

javascript - c3.js TypeError : b. 值未定义

javascript - 如何使用 document.write 将变量值附加到元素

c# - 如何包装和替换 ASP.NET 5 配置中的默认组件之一

java - 配置 Log4j 属性路径的最佳实践

cypress - 断言动态表按日期正确排序

javascript - 使用输入 jQuery 的 .value() 计算

javascript - 是否可以判断 GWT 中是否启用了 cookie?如果不是,什么原生 JavaScript 可以做到这一点?