0
votes

I want to parameterize my gherkin feature file steps by taking data from some other JSON file. Any suggestions for this. I searched almost everywhere but couldn't find answers.

I am aware of scenario where examples being used with multiple values for a variable using scenario outline in gherkin feature file but not looking for that.

Currrently I am using like this and below values in quotes are being passed to step definitions

    Scenario: Buy last coffee
        Given There is "Starbucks" coffee
        And I added "Sugarless" syrup

Expected: I want to get data of variables from JSON file or any other file also and pass these data values to step definition functions. is it possible?

gherkin feature file:

    Scenario: Buy last coffee
        Given There is "${data.coffeeshop}" coffee
        And I added "${data.sugarType}" syrup

data.json:

    {
        "coffeeshop": "starbucks",
        "sugarType": "Sugarless",

    }
2
Yes it's possible, All you have to do is get the value with in your step def based on the json path. But you might not see the values in the report. - supputuri
Make sure to add another step to read the json as firtst step or in the back ground. - supputuri

2 Answers

3
votes

Its a common Cucumber anti-pattern to try and inject data into feature files. Its difficult to do partly because it goes against the whole ethos of writing good feature files.

The way Cucumber wants you to work is to push the details down and to abstract the process so that the feature is not doing any programming (looping, iterating over steps etc.). You can improve your practice here by thinking about what is in the json file and why you want to iterate over it.

Your json file seems to want to iterate over a number of coffee shops to see if they can make a coffee. So your feature could give the group of coffee shops a name and then talk about whether the group of shops can do something. Perhaps something like

Scenario: Seattle coffee shops can make an iced mocha
  Given our coffee shops are located in Seattle
  Then our coffee shops can make an iced mocha

and implement the scenarios

Given 'our coffee shops are located in Seattle' do
  @coffee_shops = get_seattle_coffee_shops
end

Then 'our coffee shops can make an iced mocha' do
  @coffee_shops.each do | shop |
    assert can_make_recipe(
      shop: shop,
      recipe: Recipes::IcedMocha
    )
  end
end

The above is a very crude start, and I would pull out more code from the step definitions into helper methods. The key part I'm trying to illustrate here is that the scenario and steps are know working with a group of coffee shops rather than just one coffee shop

In your scenario the helper method get_seattle_coffee_shops would load and process your json to get your data. Because this processing has been pushed down from the feature file (non-code) into code this operation is now much easier to implement. This "Pushing the How Down" is a very important technique when Cuking, and is how you should approach any problem which involves programming feature files.

0
votes

I have implemented similar approach recently. I am matching json data based on scenario name. the json will look like this.In this way, you can dynamically match test data with your scenarios.

{
"scenario1":
      {
       "coffeeshop": "starbucks",
        "sugarType": "Sugarless"
      },
"scenario2":
      {
       "coffeeshop": "starbucks",
        "sugarType": "Sugarless"
      }
}

your feature file will look like this,

Scenario: senario1
        Given There is coffee
        And I added syrup

Scenario: senario2
        Given There is  coffee
        And I added syrup