how to pass command line arguments with protractor

2019-07-27 01:13发布

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.

4条回答
仙女界的扛把子
2楼-- · 2019-07-27 01:34

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);
查看更多
干净又极端
3楼-- · 2019-07-27 01:36

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.
查看更多
手持菜刀,她持情操
4楼-- · 2019-07-27 01:40

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
查看更多
Deceive 欺骗
5楼-- · 2019-07-27 01:47

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
查看更多
登录 后发表回答