问题
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