52

I'm using Protractor to perform some end to end tests, and I'd like to pass in login credentials through the command line instead of storing them in a spec file. I found one post where someone used process.argv.forEach, but how can I store those values and use them in another spec file? I have a file called login-spec.js where I'd like to use the command-line arguments.

Thanks!

Brendan
  • 1,403
  • 4
  • 18
  • 37

2 Answers2

96

In the reference config this section can be interesting:

  // The params object will be passed directly to the protractor instance,
  // and can be accessed from your test. It is an arbitrary object and can
  // contain anything you may need in your test.
  // This can be changed via the command line as:
  //   --params.login.user 'Joe'
  params: {
    login: {
      user: 'Jane',
      password: '1234'
    }
  },

And you can access the params object like this in your code: browser.params.login.user

So in your case if you call protractor like this:

protractor ... --params.login.user=abc --params.login.password=123

You can access these variables in your code like this:

browser.params.login.user and browser.params.login.password

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • 5
    if you try and pass an array on the command line "protractor --params.myArray=[1,2,3]" it will be seen as a string that you will need to JSON.parse(browser.params.myArray); – rob Jan 12 '15 at 11:41
  • 2
    Another quick note, the = sign on the command line is optional. So "protractor --params.myNum=50 ..." is equivalent to "protractor --params.myNum 50 ..." Originally I had thought the comment in the answer was wrong. – rob Jan 12 '15 at 11:44
  • 1
    Works like a charm! Though, since it takes arguments as strings, it makes kind of a mess dealing with booleans. My hint: if you have defaults in your code, set them to false and override them from the CLI if necessary (e.g. protractor ... --params.myFlag=true), since both "true" and "false" are actually falsy. If you have to pass false via the CLI, you have to use the only falsy string there is: protractor ... ---params.myFlag= (leave it blank at the end, yes it works, as wrong as it looks!) – schonarth Apr 15 '15 at 17:42
  • How to set a url param as another param instead of value? ex --params.url=params.stagingUrl did not work – artdias90 Mar 03 '16 at 10:33
  • @artdias90: I don't think this is possible directly. But if you store the URL's already in variables, you can reuse that variable. Or you can introduce one in your shell a line ahead. I created a big hash with all of the setup variables, indexed by the main environment's name and I pass just the environment's name as a protractor param. Roughly: `Environments = {test: {url: '...', name: '...', ...}, live: {...}, ...}` and `--params.env=test.` – Lajos Veres Mar 03 '16 at 11:17
  • is it possible to do like `suite: browser.params.suite.name` , i set it like `--params.suite.name=login` – ji-ruh Sep 29 '16 at 09:59
  • If I have a capabilities value which is an array, what is the correct way to pass it as a parameter? For example (that not works): `--capabilities.chromeOptions.args = ["--headless", "--disable-gpu", "--window-size=800x600"]` – Eduardo Baitello Sep 28 '17 at 15:06
  • @EduardoBaitello This may be useful: https://stackoverflow.com/a/31167011/1665673 – Lajos Veres Sep 28 '17 at 22:13
0

The downside of accepted answer - these variable will be available when browser started. So if you intend to use them in the config (create if/else logic) this won't work.

A workaround

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)
Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40