基于黄瓜标签切换 multiCapabilites 配置

Toggling multiCapabilites config based on cucumber tag

我想知道是否有人找到了一种在来自 cucumber.conf.js 文件的不同 multiCapabilities 配置之间切换的方法。目前我有一个配置,它将 运行 并行 chrome 驱动程序与 运行 测试。

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

但是如果我还想为多浏览器测试添加 multiCapabilities 选项怎么办

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

我宁愿不必注释掉或更改代码,而是存储一些 multiCapabilities 配置,我可以使用标志、标签或 g运行t 选项之类的东西来切换这些配置。有没有人有这样的运气?谢谢!

您最好的选择是使用量角器的内置命令行选项来传递浏览器或在任何此类功能之间切换。

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

如果您查看量角器的 cli 选项,并且如果您在 multicapabilties 选项中设置了多个浏览器,则可以像这样传递浏览器名称 -

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

您可以在各种浏览器的 package.json 到 运行 测试中将其设置为单独的脚本-

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

现在您可以使用 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

您还可以在 conf 文件传递​​参数中使用 params 对象,并在测试或命令行中的任何位置访问它们。

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

您可以通过起诉浏览器的全局对象在测试中访问它们 -

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

同样,您可以在命令行中更改它们-

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

有一种更优雅的方法可以通过 getMultiCapabilities-

您甚至可以通过访问上述函数对象来传递多个浏览器,请参阅此link了解更多详细信息。

Is there any way to pass multiple browser via protractor cli