how to pass command line arguments with protractor conf

不羁岁月 提交于 2019-12-13 02:19:38

问题


We have protractor-cucumber framework as e2e test. Till now we use the conf file with statically mentioning the environment details inside the conf json. I would like to pass user defined arguments with the protractor_conf file something like below.

protractor protractor_conf.js -DbrowserName=chrome -DexecPlatform=(native/sauce)

and would like to fetch this argument inside the conf.js and substitute under the capabilities section. I could not get proper details on net, so any help/suggestions would be appreciated.


回答1:


You can add Parameters in your conf.js file then pass the arugs from the command line.

Here is an example.

   // The params object will be passed directly to the Protractor instance,
   // and can be accessed from your test as browser.params. It is an arbitrary
   // object and can contain anything you may need in your test.
   // This can be changed via the command line as:
   //   --params.environment.Browser "Firefox"

params: {
  environment: {
   browser: 'Chrome',
   execPlatform: 'sauce',
   password: 'default'
  }
}

Now from we can pass the arugs from command line

protractor conf.js --parameters.environment.browser= Firefox --parameters.environment.execPlatform=sauce --parameters.environment.password=password123

Next, you can also refer these parameters in your spec file.

describe('describe some test', function() {
  it('describe some step', function() {
    $('.password').sendKeys(browser.params.login.password);
   });
});

There are multiple advantages to having a parameter setup.

  • If we know we are going to be using the same values in multiple spec files (i.e login email and password), parameters are great for removing unnecessary repetition.
  • Being able to change parameter values at runtime makes it easier to run the same tests with different data.
  • Increases security – Having passwords hardcoded in your spec files is not a great approach. Parameters give you the ability to keep them out and instead provide them at runtime.



回答2:


You can use process api to parse the arguments in cmd line.

// protractor conf.js

var readParamsFromCli = function() {

    var paramsPair = process.argv.slice(3).filter(function(it){
        return it.startsWith('-D');
    });

    var params = {};

    paramsPair.forEach(function(pair){
        var parts = pair.split('=');
        var name = parts[0].trim().replace('-D', '');
        var value = parts[1] && parts[1].trim() || true;
        params[name] = value;
    });


    return params;
};

var params = readParamsFromCli();

var capbilities = {
    browserName: params.browserName || 'chrome',
    platform: params.execPlatform
};

exports.config = {
    ...
    capbilities: capbilities

};

Then you can run case as following:

protractor protractor_conf.js -DbrowserName=chrome -DexecPlatform=native



回答3:


You could split the configuration up into multiple config files. For example protractor-chrome.conf.js:

const baseConf = require('./protractor.conf').config;

exports.config = Object.assign({
  capabilities: {
    browserName: 'chrome'
  }
}, baseConf);



回答4:


This will be similar to 2nd example but uses the config file directly.

const args = require('minimist')(process.argv.slice(2));
exports.config = {
  //your config stuff
  baseUrl: args.Url ? args.URL : <your default>,
  capabilities: {
   'browserName': 'chrome',
   chromeOptions: {
     args: [args.Options]
    },
   }
}

Then in your package.json script like this:

"e2e": "protractor protractor.conf.js --Url=http://test.com" --Options=--headless


来源:https://stackoverflow.com/questions/52822861/how-to-pass-command-line-arguments-with-protractor-conf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!