如何设置黄瓜环境变量 [英] how to set cucumber environment variables

查看:45
本文介绍了如何设置黄瓜环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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
  };
};

这篇关于如何设置黄瓜环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆