Julio
Julio

Reputation: 879

Can I access parameters in my protractor configuration file?

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?

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?

Upvotes: 32

Views: 25441

Answers (4)

Sergey Pleshakov
Sergey Pleshakov

Reputation: 8948

Easy solution

Protractor is a node process. Any node process can be started with custom node variables. Not sure how it's done in windows (please comment if you know how) but for mac and any linux/unix OS you can start protractor with environment variable like this

MY_VAR=Dev protractor tmp/config.js

And then it will be available anywhere within your process

console.log(process.env.MY_VAR)

EVEN OUTSIDE OF onPrepare IN YOUR CONFIG

Upvotes: 1

alecxe
alecxe

Reputation: 473813

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

Access the params object through the global browser object:

console.log(browser.params.baseUrl);

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

console.log(browser.baseUrl);

Upvotes: 29

exbuddha
exbuddha

Reputation: 633

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:

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
  });
})();

Upvotes: 9

yurisich
yurisich

Reputation: 7109

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

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

Upvotes: 9

Related Questions