6

I know Background keyword is available for running a common steps before running each scenario. Likewise is there anything like "After" keyword is available for the commons steps after each scenario, not the logical steps in java code like after hooks, i mean in gherkin steps itself. i need like below

Background
Given I use the API header information
  | Content-Type | application/json;v=3 |
And I connect to postgresql

Scenario Outline:
    And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
    And I store the input payload individual field details for database validation
    And I form a client with this resource url "/transaction"
    When I make a POST call and capture the response
    And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"


Examples:
| HTTPCode |
| 200      |

After
Then I validate the output response with expected data
And I verify the HTTP error code is "<HTTPCode>"
And I fetch and validate latest created data from "transaction" table
And I validate the created card is inserted into "field" table
mmar
  • 1,840
  • 6
  • 28
  • 41

1 Answers1

1

Short answer, you can probably use an After hook, more on this here, but it's not recomended for your case. BDD is used for communication with non-technical stakeholders

From how you've written the scenario outline, it seems that this only runs once and if the response if different form 200, then the last 2 steps will fail (or even the first Then).

If the only thing you need to check is the happy flow, in which the response is 200, there is no need for Scenario Outline with Examples. Simply create one scenario.

Background
Given I use the API header information
  | Content-Type | application/json;v=3 |
And I connect to postgresql

Scenario: Happy flow
    And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
    And I store the input payload individual field details for database validation
    And I form a client with this resource url "/transaction"
    When I make a POST call and capture the response
    And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"
    Then I validate the output response with expected data
    And I verify the HTTP error code is "200"
    And I fetch and validate latest created data from "transaction" table
    And I validate the created card is inserted into "field" table

If you are expecting to add more response codes then it might be a good idea to rewrite the scenario outline in a different way. There is no need for After keyword for your verification (Then steps). You are writing them only once anyway, when using scenario outline.

Background
Given I use the API header information
  | Content-Type | application/json;v=3 |
And I connect to postgresql

Scenario Outline:
    And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
    And I store the input payload individual field details for database validation
    And I form a client with this resource url "/transaction"
    When I make a POST call and capture the response
    And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"
    Then I validate the output response with expected data
    And I verify the HTTP error code is "<HTTPCode>"
    And I fetch and validate latest created data from "transaction" table
    And I validate the created card is inserted into "field" table

Examples:
| HTTPCode |
| 200      |

Keep in mind that you will need to manage the last steps differently if you are adding more response codes.

It also might be a good idea to hide details such as | Content-Type | application/json;v=3 | and manage them inside the step definition.

Update:

What I gathered from your comment is that you want to have those last 4 steps (Then steps) written only once inside a feature file and all the scenarios to use them.

As far as I know, there is no After which can execute the verification steps same as Background does for the preconditions, in the Gherkin language.

There is a way to simplify this, but it reduces reeadability. For example, if you have 10 scenarios and 2 scenario outlines that use the exact same four Then steps, then you could try nesting all of them in a more general one and if steps come from different step definistion files, then you could use picocontainer to group them and reduce the number how many times you call them inside the feature file. More details here .

Problem is that your Then steps are a bit complicated to be written in just one or even 2 simpler steps, because you have 3 params and 5 verifications.

To conclude, I think it's better to have them written for each scenario / scenario outline. I would be difficult for another person to look at the feature file and not see any Then, only to find them at the bottom. A better way would be to try and group more scenarios in scenario outlines, so the steps don't repeat that much.

Hope it helps!

Daniel Fintinariu
  • 2,733
  • 1
  • 14
  • 16
  • Thanks. But the main reason i asked for is, i do have 50+ scenarios. For all scenarios the steps are more or less same, only changeable steps are the middle ones i mentioned in my question. For each scenario my HTTP code will change and i will validate by putting it in 'Examples:'. For others i validate with expected json and actual json contents. So it looks like i am repeating most of the steps again and again. So was looking for some clean definitions. Can you help me on this? – mmar Sep 28 '17 at 15:35
  • Sorry, I don't think I understand exactly what you are trying to do. I'll give it another go in the answer. – Daniel Fintinariu Sep 29 '17 at 08:15