【问题标题】:How can I pass an env name to Cypress tests to load a set of env variables?如何将环境名称传递给赛普拉斯测试以加载一组环境变量?
【发布时间】:2021-10-27 19:09:50
【问题描述】:

如何将环境名称传递给赛普拉斯测试以加载一组环境变量?赛普拉斯页面仍然神秘,没有回答这个问题:https://docs.cypress.io/guides/guides/environment-variables#Option-2-cypress-env-json

此外,他们的示例以迫使我编写 shell 脚本来处理切换变量的方式做到这一点:https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/server-communication__env-variables。没有办法硬编码环境名称对我有用:https://github.com/cypress-io/cypress-example-recipes/blob/2e5fb3da420f544637ff77d81ff7261f5d2164e8/examples/server-communication__env-variables/cypress/integration/spec.js#L5

所以,我创建了一个名为 cypress.playground.json 的文件,但我不知道如何告诉赛普拉斯加载它。

{
    "baseUrl": "https://my-url.hostname/ui/",
    "defaultCommandTimeout": 10000,
    "video" : true
}

并且,从命令行运行时。这是我“猜测”如何传递 env 值:

✘-1 ~/Documents/GitHub/me [Branch-Name|✔] 
16:35 $ cypress run --headed --browser chrome --env playground
Cypress encountered an error while parsing the argument env

You passed: playground

The error was: Cannot read property 'split' of undefined
✘-1 ~/Documents/GitHub/me [Branch-Name|✔] 
16:35 $ cypress run --headed --browser chrome --config playground
Cypress encountered an error while parsing the argument config

You passed: playground

The error was: Cannot read property 'split' of undefined

我的意图是在运行测试之前覆盖默认值 baseUrl

【问题讨论】:

    标签: cypress


    【解决方案1】:

    这是我想出的变种。

    这个区别是你可以将cypress.json与你指定的文件结合起来,例如cypress.playground.json

    /plugins/index.js

    const fs = require('fs')
    
    module.exports = (on, config) => {
    
      const configFile = `cypress.${config.env['env-file']}.json` // relative to project root
      const addConfig = JSON.parse(fs.readFileSync(configFile))
      const combinedConfig = { ...config, ...addConfig }  // overwrites keys like baseUrl
    
      return combinedConfig
    }
    

    命令行

    yarn cypress open --env env-file=playground
    

    cypress.playground.json

    {
      "baseUrl": "http://example.com"
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 2020-10-09
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    相关资源
    最近更新 更多