启动测试时如何将浏览器作为命令行参数传递

时间:2018-06-14 14:01:54

标签: webdriver protractor

我正在尝试针对Chrome和Firefox运行多浏览器测试。 我想一次只运行一个浏览器。所以我想通过测试一个命令行参数--browser chrome,让它只运行chrome浏览器。

我在某处读到了我可以对我的protractor.config.js做params: {}部分。

所以我添加了以下内容:

params: {
    browser: multiCapabilities.browserName,
},

我得到的错误如下:

ReferenceError: multiCapabilities is not defined
at Object.<anonymous> (/Users/csalisbury/src/helios/protractor.config.js:91:18)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Module.require (internal/modules/cjs/loader.js:626:17)
at require (internal/modules/cjs/helpers.js:20:18)
at ConfigParser.addFileConfig (/Users/csalisbury/src/helios/node_modules/protractor/built/configParser.js:135:26)
at Object.initFn [as init] (/Users/csalisbury/src/helios/node_modules/protractor/built/launcher.js:93:22)

定义了multiCapabilities部分。我也尝试使用capabilities并获得相同的消息。

我在这里做错了什么?

这是我的protractor.config.js

exports.config = {
suites: {
    <test suites go here>
},
baseUrl: baseUrl,
directConnect: false,
allScriptsTimeout: 25 * 1000,
jasmineNodeOpts: {
    defaultTimeoutInterval: 90 * 1000
},
getPageTimeout: 120 * 1000,
capabilities: {
    browserName: 'chrome',
    seleniumAddress: seleniumServer,
    platform: 'ANY',
    version: 'ANY',
    chromeOptions: {
        args: ['--no-sandbox', '--test-type=browser', '--lang=en', '--window-size=1680,1050'],
        prefs: {
            'credentials_enable_service': false,
            'profile': {
                'password_manager_enabled': false
            },
            download: {
                prompt_for_download: false,
                directory_upgrade: true,
                default_directory: 'C:\\downloads\\'
            },
        },
    },
    loggingPrefs: { browser: 'SEVERE' }
},
capabilities: {
    browserName: 'firefox',
    'moz:firefoxOptions': {
        args: ['--safe-mode'],
        binary: '/Applications/Firefox.app/Contents/MacOS/firefox'
    },
    seleniumAddress: seleniumServer,
},

framework: 'jasmine2',
onPrepare: function() {
    //Set Up a JUnit XML Reporter - Makes a nice test results area and trend graph in Jenkins
    var jasmineReporters = require('jasmine-reporters');
    jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
        consolidateAll: true,
        savePath: 'report',
        filePrefix: 'xmloutput'
    }));

    //Setup screenshots
    jasmine.getEnv().addReporter(reporter);

    browser.get(browser.baseUrl);
},
// Setup the report before any tests start
beforeLaunch: function() {
    return new Promise(function(resolve) {
        reporter.beforeLaunch(resolve);
    });
},
// Close the report after all tests finish
afterLaunch: function(exitCode) {
    return new Promise(function(resolve) {
        reporter.afterLaunch(resolve.bind(this, exitCode));
    });
},

};

1 个答案:

答案 0 :(得分:0)

browserName使用capabilities设置 - 包括命令行和配置。所以要用命令行设置它,它只是:

protractor conf.js --capabilities.browserName='firefox'

从命令行传递的值将覆盖配置中设置的值,例如,您可以将chrome作为默认浏览器:

exports.config = {
    capabilities: {
        browserName: 'chrome'
    }
}

然后要覆盖chrome并运行firefox,您只需传入browserName,如上所示。

或者,如果您想避免使用命令行选项并按顺序运行每个浏览器,则可以使用multiCapabilities并限制maxSessions允许的数量。

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

来源:protractor config