如何设置黄瓜环境变量

2022-01-22 00:00:00 node.js phantomjs javascript cucumber

我有以下 package.json:

I am having the following package.json:

{
  "name": "newcucumber",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "./node_modules/.bin/cucumber-js",
    "firefox": "./node_modules/.bin/cucumber-js -- --profile.desktop.env.browser ff"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "chromedriver": "^2.24.1",
    "cucumber": "^1.3.0",
    "firefox-profile": "^0.4.2",
    "geckodriver": "^1.1.2",
    "phantomjs-prebuilt": "^2.1.12",
    "selenium-webdriver": "^3.0.0-beta-2"
  }
}

我使用以下方式运行程序:

I run the program using:

npm test

我想为 cucumber 设置一个环境变量,这样我就可以从命令行运行:npm test firefoxnpm test phantomjs.

I would like to set an environment variable for cucumber, such that I could run from the command line: npm test firefox or npm test phantomjs.

如上所示,它也可以作为 package.json 'scripts' 的一部分,但我不确定我是否做得对.调用 npm run-script firefox

It could also be as a part of package.json 'scripts' as seen above, but I am not sure if I did it right. Invoking npm run-script firefox

如何实现它,以便在 js 代码(如 world.js 或 browser.js)中获取 env 变量?

How does one implement it, such that in the js code, like world.js or browser.js I grab the env variable?

推荐答案

再一次,我得到了我最开始想使用的答案:

Once more, now I have got the answer I wanted to use in the first place:

这就是 package.json 的一部分(注意!!!引号的语法)的样子:

This is how part of package.json (MIND!!! the syntax of quotation marks) looks like:

"scripts": {
    "test": "cucumber-js",
    "firefox": "cucumber-js --world-parameters '{"browser":"firefox"}'",
    "chrome": "cucumber-js --world-parameters '{"browser":"chrome"}'",
    "safari": "cucumber-js --world-parameters '{"browser":"safari"}'",
    "phantomjs": "cucumber-js --world-parameters '{"browser":"phantomjs"}'"
  }

JSON 对象被传递给 World.在您的 World.js 中,代码如下所示:

The JSON object is passed to World. In your World.js the code looks like this:

module.exports = function() {
  this.World = function(input) {
    console.log(input.browser); // Do whatever you want with the JSON (input) object
  };
};

相关文章