Javascript 我可以访问量角器配置文件中的参数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29406962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:18:42  来源:igfitidea点击:

Can I access parameters in my protractor configuration file?

javascriptangularjstestingprotractore2e-testing

提问by Julio

I start my protractor tests by running the following:

我通过运行以下命令开始我的量角器测试:

protractor protractor.conf.js --params.baseUrl=http://www.google.com --suite all

I would like to run a 'before launch' function which is dependant of one parameter (in this case, the baseUrl). Is it that possible?

我想运行一个“启动前”函数,该函数依赖于一个参数(在本例中为 baseUrl)。有可能吗?

exports.config = {
    seleniumServerJar: './node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',
    seleniumPort: 4455,
    suites: {
        all: 'test/*/*.js',
    },
    capabilities: {
        'browserName': 'firefox'
    },
    beforeLaunch: function() {
        console.log('I want to access my baseUrl parameter here: ' + config.params.baseUrl);
    },
    onPrepare: function() {

        require('jasmine-reporters');
        jasmine.getEnv().addReporter(
            new jasmine.JUnitXmlReporter('output/xmloutput', true, true));

    }
};

If I run that I get a ReferenceError because config is not defined. How should I do that? Is that even possible?

如果我运行它,我会收到一个 ReferenceError 因为未定义配置。我该怎么做?这甚至可能吗?

采纳答案by alecxe

I am not completely sure if protractor globals are set at the beforeLaunch()stage, but they are definitely available at onPrepare()step.

我不完全确定是否在beforeLaunch()舞台上设置了量角器全局变量,但它们绝对可以在onPrepare()步骤中使用。

Access the paramsobject through the global browserobject:

params通过全局browser对象访问对象:

console.log(browser.params.baseUrl);

Update: Using Jasmine 2.6+, protractor 4.x, browser.params was empty, but the following worked in onPrepare()step:

更新:使用 Jasmine 2.6+,量角器 4.x,browser.params 为空,但以下onPrepare()步骤可以正常工作:

console.log(browser.baseUrl);

回答by Droogans

In case you need every single item in the entire configuration file, you can use browser.getProcessedConfig()to do this.

如果您需要整个配置文件中的每一项,您可以使用它browser.getProcessedConfig()来执行此操作。

onPrepare: () => {
    browser.getProcessedConfig().then(console.log); // even `params` is in here
}

回答by exbuddha

Here is a sample code to iterate thru cmd line args in your Protractor config file and set specs (and some other run configuration values) directly from command line:

这是一个示例代码,用于通过量角器配置文件中的 cmd 行参数进行迭代,并直接从命令行设置规范(以及其他一些运行配置值):

config.js

配置文件

// usage: protractor config.js --params.specs="*" --params.browser=ie --params.threads=1
//        protractor config.js --params.specs="dir1|dir2"
//        protractor config.js --params.specs="dir1|dir2/spec1.js|dir2/spec2.js"

// process command line arguments and initialize run configuration file
var init = function(config) {
  const path = require('path');
  var specs;
  for (var i = 3; i < process.argv.length; i++) {
    var match = process.argv[i].match(/^--params\.([^=]+)=(.*)$/);
    if (match)
      switch (match[1]) {
        case 'specs':
          specs = match[2];
          break;
        case 'browser':
          config.capabilities.browserName = match[2];
          if (match[2].toLowerCase() === 'ie') {
            config.capabilities.browserName = 'internet explorer';
            config.capabilities.platform = 'ANY';
            config.capabilities.version = '11';
            config.seleniumArgs = ['-Dwebdriver.ie.driver=' + path.join('node_modules', 'protractor' ,'selenium' ,'IEDriverServer.exe')];
          }
          if (match[2] !== 'chrome' && match[2] !== 'firefox')
            config.directConnect = false;
          break;
        case 'timeout':
          config.jasmineNodeOpts.defaultTimeoutInterval = parseInt(match[2]);
          break;
        case 'threads':
          config.capabilities.maxInstances = parseInt(match[2]);
          config.capabilities.shardTestFiles = config.capabilities.maxInstances > 1;
          break;
      }
  }

  // generate specs array
  specs.split(/\|/g).forEach(function(dir) {
    if (dir.endsWith('.js'))
      config.specs.push(dir);
    else
      config.specs.push(path.join(dir, '*.js'));
  });

  return config;
};

exports.config = (function() {
  return init({
    specs: [],
    framework: 'jasmine',
    jasmineNodeOpts: {
      defaultTimeoutInterval: 300000 // 5 min
    },
    capabilities: {
      browserName: 'chrome',
      shardTestFiles: false,
      maxInstances: 1
    },
    directConnect: true
  });
})();