How can I use command line arguments in Angularjs Protractor? How can I use command line arguments in Angularjs Protractor? selenium selenium

How can I use command line arguments in Angularjs Protractor?


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


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)