简体   繁体   English

如何设置黄瓜环境变量

[英]how to set cucumber environment variables

I am having the following package.json: 我有以下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

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 . 我想为黄瓜设置一个环境变量,这样我就可以从命令行运行: npm test firefoxnpm test phantomjs

It could also be as a part of package.json 'scripts' as seen above, but I am not sure if I did it right. 它也可以作为package.json'脚本'的一部分,如上所示,但我不确定我是否做得对。 Invoking npm run-script firefox 调用npm run-script firefox

How does one implement it, such that in the js code, like world.js or browser.js I grab the env variable? 如何实现它,以便在js代码中,像world.js或browser.js我抓住env变量?

Once more, now I have got the answer I wanted to use in the first place: 再一次,现在我得到了我想要的答案:

This is how part of package.json (MIND!!! the syntax of quotation marks) looks like: 这是package.json(MIND !!!引号的语法)的一部分如下所示:

"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\"}'"
  }

The JSON object is passed to World. JSON对象传递给World。 In your World.js the code looks like this: 在你的World.js中,代码如下所示:

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

You can do something like that when you want to define env vars. 当你想定义env vars时,你可以做类似的事情。 By the way, it is not mandatory to add the whole path, npm will figure it out 顺便说一下,添加整个路径并不是强制性的,npm会弄明白

{
  "name": "newcucumber",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "cucumber-js",
    "firefox": "NODE_ENV=test 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 haven't tried it myself but, according to cucumber's CLI documentation, there's a way to pass data to the world constructor ( World parameters , by the end of the page) 我自己没有尝试过,但根据cucumber的CLI文档,有一种方法可以将数据传递给world构造函数( 世界参数 ,在页面末尾)

You could pass the browser to the World constructor and let it create the webdriver instance according to your choice. 您可以将浏览器传递给World构造函数,并让它根据您的选择创建webdriver实例。

I guess the script part in your package.json could be (more or less) as follows: 我猜你的package.json中的脚本部分可能(或多或少)如下:

"scripts": {
    "test": "cucumber-js",
    "firefox": "./node_modules/.bin/cucumber-js --world-parameters <JSON>"
  }

where <JSON> would contain the info. 其中<JSON>将包含信息。 about the browser type, etc. Hope it helps. 关于浏览器类型等希望它有所帮助。

I decided not to change something in node_modules or whatever, cause after the next npm update <package> the changes will be lost. 我决定不在node_modules或其他任何东西中更改某些内容,导致在下一个npm update <package> ,更改将丢失。

The Idea is to set the env variable: 想法是设置env变量:

  1. Create a start.sh file in the directory where you call npm test from. 在您调用npm test from的目录中创建一个start.sh文件。
  2. Write in start.sh : 写在start.sh

     #!/bin/bash export BROWSER=$1 npm test 
  3. Inside your browser.js or the file where you call your browsers type: 在您的browser.js或您调用浏览器的文件中输入:

     var chrome = require('chromedriver'), phantom = require('phantomjs-prebuilt'), firefox = require('selenium-webdriver/firefox'), webdriver = require('selenium-webdriver'); ... console.log("What was passed into global env: ", process.env.BROWSER); switch(process.env.BROWSER) { case 'firefox': //Setup Firefox var capabilities = { 'browserName' : 'firefox' } break; case 'phantomjs': var capabilities = { 'browserName' : 'phantomjs' } break; case 'chrome': var capabilities = { 'browserName' : 'chrome' } break; } return browserHandle = new webdriver .Builder() .withCapabilities(capabilities) .build(); 
    1. Call . start.sh firefox (phantomjs, chrome) 打电话. start.sh firefox (phantomjs, chrome) . start.sh firefox (phantomjs, chrome)

6axter82's solutions works for me...to some degree. 6axter82的解决方案在某种程度上对我有用。 In order to pass in variables from command line, I have to add following adjustments. 为了从命令行传入变量,我必须添加以下调整。 (Environment: Windows 10 / VS Code / Command Prompt) (环境:Windows 10 / VS代码/命令提示符)

  1. To execute from scripts property inside package.json, additional backslash () is needed. 要从package.json中的scripts属性执行,需要额外的反斜杠()。
    "firefox": "cucumber-js --world-parameters '{\\"browser\\":\\"firefox\\"}'",

    is converted into 转换成

    "firefox": "cucumber-js --world-parameters {\\\\\\"browser\\\\\\":\\\\\\"firefox\\\\\\"}"

  2. (If default constructor is overridden by setWorldConstructor(World)) Create constructor for World as specified on Cucumber.js/World Doc , specifically (如果默认构造函数被setWorldConstructor(World)覆盖)创建在Cucumber.js / World Doc上指定的World的构造函数,具体

    function World({attach, parameters}) { this.attach = attach this.parameters = parameters }

  3. To reference parameter values inside World class (world.js), use following expression this.parameters.browser 要在World类(world.js)中引用参数值,请使用以下表达式this.parameters.browser

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM