如何通过Cypress命令行传递整个JSON作为cypress.env?

huangapple go评论63阅读模式
英文:

How do I pass in an entire JSON as the cypress.env through the command line in Cypress?

问题

我熟悉通过命令行界面逐个添加环境变量,方法是输入:

./node_modules/.bin/cypress run -- --env itemToOverride="val"

但假设我有一个完整的 JSON 文件,可以作为有效的 Cypress 环境变量,我如何通过命令行界面传递它,而不必逐个添加 JSON 中的每个项目?

例如,假设我有 env1.json 和 env2.json,它们都有适用于我的测试的有效结构,但我想要能够先运行所有测试用 env1,然后再用 env2,类似于:

./node_modules/.bin/cypress run -- --env env1.json
英文:

I am familiar with adding individual environment variables through the CLI by entering:

./node_modules/.bin/cypress run -- --env itemToOverride="val"

But let's say I have an entire JSON that can be a valid cypress.env, how do I pass that in via CLI without having to add each item from the JSON individually?

For example, let's say I have env1.json and env2.json which both have valid structure for my test, but I want to be able to run all my tests with env1 then env2? Similar to:

./node_modules/.bin/cypress run -- --env env1.json

答案1

得分: -1

以下是翻译好的部分:

"For others that stumble here and want a working answer: The easiest solution to this would be to create a config folder in your project directory containing all the environment JSONs you want. I wrote a script to take an environment file's name as an argument when running it. I decided on a bash script, but you could also use an npm script to achieve this. Basically, to run the tests in 'env1' you would type on your command line:

./cypress --env=env1

The way I achieved this was by copying the selected JSON from the config folder to the main directory, then renaming it 'cypress.env.json'. This is a setup function in my bash script to get you started:

  echo "Selecting environment variables from $ENV.env.json"
  cp ./cypress/config/"$ENV.env.json" ./
  mv "$ENV.env.json" "cypress.env.json"
} ```

It's an obscure problem that does not have very many answers online, but I hope this helps!"

<details>
<summary>英文:</summary>

For others that stumble here and want a working answer: The easiest solution to this would be to create a config folder in your project directory containing all the environment JSONs you want. I wrote a script to take an environment file&#39;s name as an argument when running it. I decided on a bash script, but you could also use an npm script to achieve this. Basically, to run the tests in &quot;env1&quot; you would type on your command line: 

./cypress --env=env1

The way I achieved this was by copying the selected JSON from the config folder to the main directory, then renaming it &#39;cypress.env.json&#39;. This is a setup function in my bash script to get you started:
 

function doSetup {
echo "Selecting environment variables from $ENV.env.json"
cp ./cypress/config/"$ENV.env.json" ./
mv "$ENV.env.json" "cypress.env.json"
}


It&#39;s an obscure problem that does not have very many answers online, but I hope this helps!

</details>



huangapple
  • 本文由 发表于 2023年2月14日 02:39:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定