Passing variables on the command line to a Cucumber test

Chad Brewbaker picture Chad Brewbaker · Apr 3, 2014 · Viewed 9.9k times · Source

I'm trying to keep usernames and passwords for a cucumber project out of version control.

Is there a way to manually pass variables on the command line like usernames and passwords to a cucumber script?

My backup plan was to put them in a YML file and add that file to the gitignore so they aren't put in version control.

Answer

Parva Thakkar picture Parva Thakkar · Apr 3, 2014

So, I saw your comments with the Tin Man, and answer is Yes.

cucumber PASSWORD=my_password

PASSWORD is set as an environment variable and you can use its value by referring to it as ENV['PASSWORD']. For an example, browser.text_field(:id => 'pwd').set ENV['PASSWORD']

Another way is indirect. What I did in past was to pass profile name and that profile will do something that I want. So, for example, I have a profile name as firefox and a firefox profile in cucumber.yml has a variable named BROWSER_TYPE with its value assigned to firefox. And this variable (BROWSER_TYPE) is used by my method that opens the browser. If its value is firefox, than this method opens firefox browser.

So, what I did here was -

  1. Pass a profile. Name of the profile is firefox
  2. firefox profile is defined in cucumber.yml. You can any thing with the profiles, but in this case, I define a variable named BROWSER_TYPE and assign its value as firefox.
  3. Then I have a method that uses BROWSER_TYPE variable and uses its value to open browser.

Code for these steps -

  1. cucumber -p firefox
  2. My cucumber.yml file looks like firefox: BROWSER_TYPE=firefox PLATFORM=beta

  3. My method to open browser looks similar to -

    @browser = Watir::Browser.new ENV['BROWSER_TYPE']

So, ideally you can create a profile that sets an environment variable with password, and pass that profile name to cucumber.