ray salvo
ray salvo

Reputation: 21

Cucumber: read a template with placeholders

I am writing a cucumber framework to test a set of API calls which use long JSON formatted parameters. I would like to hide the JSON in a template file in order to make the scenario easier for my users to read and DRYer, in that the templates may be used by other scenarios and feature files. The templates contain placeholders and I would like to rig the cucumber/ruby code to fill in the values defined in the table of examples. It appears that ERB is the closest thing to doing the replacement. However, I have not found a way to bind the definitions from the table of examples.

It may be that the only way around this is to run the feature file and template through a pre-processor which combines them and manufactures the final feature file. I am looking for a more elegant single step solution, if possible.

Example feature file code:

Feature: Create users
Scenario Outline: Test create a merchant user

Given I am logged in
When I send a :post request to "createUser" using the "Merchant" template:
Then the JSON response should have "$..status" with the text "success"

Examples:
  | OrgName                   | KeyName             | KeyValue   |
  | CClient_RMBP_0_UNIQ_      | paramX              | TRUE       |
  | CClient_RMBP_1_UNIQ_      | paramY              | some text  |
  | CClient_RMBP_2_UNIQ_      | paramZ              | 12345      |

Sample Merchant.json File:

{
    "Organization": {
        "parameters": [
            {
                "key": "orgName",
                "value": {
                    "value": "<OrgName>"
                }
            },
            {
                "key": "<KeyName>",
                "value": {
                    "value": "<KeyValue>"
                }
            }
        ]
    },
    "parentOrganizationId": "1",
    "User": {
        "firstName": "Mary",
        "lastName": "Smith",
        "id": "<OrgName>",
        "language": "en",
        "locale": "US",
        "primaryEmail": "[email protected]",
        "cellPhone": "1-123-456-7890"
    },
    "active": "true"
}

Upvotes: 1

Views: 1822

Answers (1)

jmccure
jmccure

Reputation: 1259

I prefer to hide any notion of the response format from the cucumber steps.

Instead i'd prefer to have the steps at a higher level and have validate methods within the step definitions.

e.g.

When I attempt to create a user using the <type> template
Then the response is successful and contains the user's details

and in my step definition I would have a validate_response method which grabs the last response and checks the user details against the input.

Upvotes: 0

Related Questions