4

I have a test where I'd like to pass a language key through the command line, and have the browser use that language key in the chromeOption args.

How would I use a default of en-us, but pass in whatever language I want and change it from the default based on what is passed in?

I've got this in my conf file:

exports.config = {
...
    capabilities: {
        browserName: 'chrome',
        chromeOptions: {
            args: [
                'lang=en-419',
            ]
        }
    },
    params: {       
        language: {
            lang: 'en-US'
        }
    },
...
};

I would like to run this command and have the browser switch to es-419 for this run of the tests

protractor conf.js --params.language.lang=es-419

What do I add in to modify the capabilities based on the passed language, with en-us as a default if nothing is passed?

I've tried a few things, which crash with errors like 'object has no method capabilities' or 'chromeOptions undefined'.

browser.capabilities.chromeOptions.args.update(browser.params.login.lang);
browser.getCapabilities().then(function(){
           chromeOptions.args = browser.params.language.lang;
    });
browser.manage().capabilities.chromeOptions.args(browser.params.language.lang);
capabilities: {
    chromeOptions: {
        args: { 'lang=' + this.params.language.lang
        }
    }
}

Any ideas?

user2020347
  • 883
  • 7
  • 17

1 Answers1

1

I was able to alter the language capabilities by accessing the command-line through the node process object:

function getLangParam(defaultValue) {
  // Each arg will have the entire string value between
  // whitespace, dashes and all.
  var PARAM_KEY = '--params.language.lang=';

  var result = defaultValue;
  process.argv.forEach(function (arg) {
    // Check if the arg starts with the PARAM_KEY
    // and update the result if we find it
    if (arg.indexOf(PARAM_KEY) === 0) {
      result = arg.substring(PARAM_KEY.length);
    }
  });
}

var lang = getLangParam('en-US');
exports.config = {
  capabilities: {
    browserName: 'chrome',
    chromeOptions: {
      args:['lang=' + lang]
    }  
  }

  // The rest of your config...

};

The browser object is not available when the config is being parsed according to this SO question: Can I access parameters in my protractor configuration file?

Community
  • 1
  • 1
kennbrodhagen
  • 4,258
  • 2
  • 26
  • 21
  • Hello! Thank you for the answer. Can you expand on how you run this? Just protractor conf.js --params.langauge.lang='es-419'? Do I even pass a param, or just hardcode something in to the file? I can't seem to get the language to change regardless of if I pass this a param, or even change the getLangParam line. It looks like the args section doesnt even know what lang is. If I do: browser.getProcessedConfig().then(function(configData){ console.log(configData.capabilities); }); I get lang=undefined. Sorry if I am not understanding, I am new to this. – user2020347 Jul 07 '15 at 01:10
  • Yes you can just run protractor conf.js --params.language.lang='es-419'. Sorry I'm not familiar with the lang options of Chrome myself. I assumed you were already able to get the language to change by hard-coding the values you showed and just needed to know how you could pass them in on the command line. – kennbrodhagen Jul 12 '15 at 00:55