最好的办法是使用量角器的内置命令行选项来传递浏览器或在任何此类功能之间切换。
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,还有一种更优雅的方式来做这些事情-
您甚至可以通过访问上述函数对象来传递多个浏览器,请参阅此链接了解更多详细信息。
Is there any way to pass multiple browser via protractor cli