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!

2 Answers 2

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

8
  • 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, 2015 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, 2015 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, 2015 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 3, 2016 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. Mar 3, 2016 at 11:17
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)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.