Search code examples
cucumberbddcucumber-java

How to describe a scenario in gherkin retrieving an Access Token in Given clause


My question is much more conceptual than ever. I'd like to describe a good scenario using the Cucumber feature file where I have to have for each row of my Data table a new access token from the Identity Provider.

I.e

Scenario:
    Given <Code Authorization>
    And <Access Token>
    And The client has the following information
        | email  | FirstName | Phone |
        | xpto@  | Richard   | 343242|
    When the client via Post /xpto
    Then The API response a Json file
        | code | response |
        | 200  | xpto     |

I'll use a Data Table for this kind of approach. However, I cannot give a static Access Token because it will expire. I should get a new one every time when my test run but It is not my test it self. The Token is just a Data that I have to have to test my scenario.

Is it ok call a REST in an Given Step? If I do this I am mixing up the objective of my scenarios.

Any thougts are welcome not for your mind but by the book. :-)

Kind Regards,


Solution

  • It seems that you need the token in order to set up the scenario. In that case it is fine to have that in a Given step. You can perform REST or other calls in step definitions for that Given step. For ex: It may look something like below. You can change wordings as you like but try to word it in a manner that shows initial state of the application.

     Given I have a token for this scenario
     And The client has the following information 
      | email | FirstName | Phone | 
      |xpto@  | Richard   | 343242| 
     ...
     ...
    

    Given steps are meant to establish a given state. It is considered best practice in BDD. You can find this information in official BDD docs here

    Also , if you want to read more about the purpose and structure of Given , When and Then , be sure to have a look here