Passing Variables on the Command Line to a Cucumber Test

Passing variables on the command line to a Cucumber test

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.

How do I pass a CLI argument to a Cucumber Java test suit?

In your method, you can read this value as a system property like this:

@Before
public void server_connection() {
ConnectionToServer serverConnection = new ConnectionToServer(System.getProperty("IPArgumentFromMaven"), 5776);
serverConnection.open();
}

Now you can pass a value for IPArgumentFromMaven to maven from command line like this:

mvn clean test -DIPArgumentFromMaven=localhost

Different arguments per test run with cucumber

cucumber COUNTRY=my_country

then you can get value with

ENV['COUNTRY']

Then you can write

Given I'm from registered on what ever country website
When I login
Then i should receive a ok stats.

in the step definition

  @country = ENV['COUNTRY']

and use them like you want

see here

Passing variables on the command line to a Cucumber test

pass special values to Cucumber

The first link show interesting way using cucumber profiles.

Any way to pass custom additional args to cucumber and parse them?

you can use environment variables for this

from: https://github.com/cucumber/cucumber/wiki/Environment-Variables

When you are Running Features it can sometimes be handy to pass
special values to Cucumber so that they can be picked up in your Step
Definitions. You can easily do this on the command line:

cucumber FOO=BAR --format progress features You can now pick up
ENV['FOO'] in ruby (for example in env.rb or a step definition) and
take actions according to the value



Related Topics



Leave a reply



Submit