Search code examples
cucumber-jvmgherkin

how to send payload and header parameters in a gherkin step


new to cucumber. I want to test rest api using cucumber jvm.

suppose , I have the following scenario

scenario: 
 * POST at "http://localhost:8080/x" with payload: 
   """
     <user>
      <name>abc</name>
     </user>
   """
   with header:
   |param1|value|
   |param2|value|

But it is not working.

if I break the step into 2, one that gives payload and other that gives header, I have to hold the first step (because it would be missing header) and do the actual post operation with the second step.

what are my options ? thanks


Solution

  • There was a feature request for supporting both tables and docstrings, but it was closed because of the amount of work to support it in all cucumber implementations, not just the one for the jvm.

    So the workaround would be to split this into multiple steps, collecting all data, and sending it at the end:

    Scenario: Create a user
    Given the following payload:
    """
    <user><name>abc</name></user>
    """
    And the following headers:
    | param | value |
    When the request is sent as "POST" to "http://example.com/users"
    Then the user is created
    

    I think this also helps with readability of the scenario, and both payload and headers would be optional, which might be useful for other (simpler) requests.