Toggle multiCapabilites configuration based on cucumber label

I was wondering if anyone has found a way to switch between the different multiCapabilities configurations coming from the cucumber.conf.js file. I currently have this configuration which will run concurrent chrome drivers to run tests.

multiCapabilities: [{
    'browserName': 'chrome',
    'platform': 'ANY',
    shardTestFiles: true,
    maxInstances: 2
}],

      

But what if I also wanted to add the multiCapabilities parameter for testing multiple browsers

multiCapabilities: [{
    'browserName': 'chrome'
},{
    'browserName': 'firefox'
}]

      

I would rather not want to comment or change the code, but store multiple multiCapabilities configurations that I can toggle using something like the flag, tag, or grunt parameter. Has anyone had any luck with something like this? Thank!

+3


source to share


1 answer


Your best bet would be to use the built-in command line parameters to pass browsers or switch between any such capabilities.

Usage: protractor [configFile] [options]
configFile defaults to protractor.conf.js
The [options] object will override values from the config file.
See the reference config for a full list of options.

Options:
--help                                 Print Protractor help menu
--version                              Print Protractor version
--browser, --capabilities.browserName  Browsername, e.g. chrome or firefox

      

If you look at the cli protractor options , and if you have multiple browser settings in your multicapabilties option, you can pass the browser names like this -

protractor config.js --capabilities.browserName='chrome'
protractor config.js --capabilities.browserName='firefox'

      

You can set this up as separate scripts in package.json to run tests across different browsers.

"scripts": {
"tsc": "tsc",
"test": "protractor ./config.js",
"chrome-tests": "protractor ./config.js --capabilities.browserName='chrome'",
"firefox-tests": "protractor ./config.js --capabilities.browserName='firefox'"
}

      

You can now call this using npm

-

npm run chrome-tests // it would run your tests in chrome browser
npm run firefox-tests // it would run your tests in firefox browser

      

You can also use the object params

in the file path parameters conf

and refer to them anywhere in the tests or on the command line.



params: {
     primaryBrowser: 'chrome'  // I am biased towards chrome :)
     secondaryBrowser: 'firefox'
 },

      

You can access them in your tests by applying the browser global object -

console.log(browser.params.primaryBrowser);
console.log(browser.params.secondaryBrowser);

      

Likewise, you can change them on your command line -

protractor config.js --params.primaryBrowser='firefox'

      

There is another elegant way to do things like this via getMultiCapabilities

-

You can even pass multiple browsers by accessing the above function object, please refer to this link for more details.

Is there a way to pass multiple browsers using protractor cli

+3


source







All Articles