0
votes

My cucumber Gherkins look like this:

Feature: Story XYZ- Title of Story
  """
  Title: Story XYZ- Title of Story  
  """

  Background: 
    Given Appointments are being created using "SOAP" API

  @scenario1
  Scenario Outline: Create an appointment for a New start service order    
    Given Appointment does not exist for order id "Test_PR_Order123"
    When Request for create appointment is received for order "Test_PR_Order123" and following
      | FieldName        | FieldValue         |
      | ServiceGroupName | <ServiceGroupName> |
      | SerivceGroupID   | TestSG123          |
      | ServiceType      | <ServiceType>      |
    Then Appointment ID should be created
    And Appointment for order "Test_PR_Order123" should have following details
      | FieldName        | FieldValue         |
      | ServiceGroupName | <ServiceGroupName> |
      | SerivceGroupID   | TestSG123          |
      | ServiceType      | <ServiceType>      |
    And Appointment history should exist for "Create Appointment"

Examples: 
  | ServiceType | ServiceGroupName      |
  | Service1    | ServiceGroup1         |
  | Service2    | ServiceGroup2         |

@scenario22
  Scenario Outline: Create an appointment for a Change Service order
    Given Appointment does not exist for order id "Test_CH_Order123"
    When Request for create appointment is received for order "Test_CH_Order123" and following
      | FieldName        | FieldValue         |
      | ServiceGroupName | <ServiceGroupName> |
      | SerivceGroupID   | TestSG123          |
      | ServiceType      | <ServiceType>      |
    Then Appointment ID should be created
    And Appointment for order "Test_CH_Order123" should have following details
      | FieldName        | FieldValue         |
      | ServiceGroupName | <ServiceGroupName> |
      | SerivceGroupID   | TestSG123          |
      | ServiceType      | <ServiceType>      |
    And Appointment history should exist for "Create Appointment"

Examples: 
  | ServiceType | ServiceGroupName      |
  | Service1    | ServiceGroup1         |
  | Service2    | ServiceGroup2         |

In above feature there is a background which will execute for each example in both Scenario Outline. Also, in java implementation we have implemented @After and @Before hooks which will also execute for each example.

We are using spring-cucumber for data injection between steps.

Problem occurs when all examples in first scenario outline ends, @After implemented method is invoked 2 times. When 2nd time @After starts at the same time 2nd Scenario Outline examples start executing. As a result sequential execution of scenarios is disturbed and automation start to fail.

Please suggest if this is a bug in cucumber or we are missing anything.

1
Does the After and Before hook have any tag filters? - Grasshopper

1 Answers

1
votes

One of the many things you are missing is keeping scenarios simple. By using a scenario outlines and by embedding so many technical details in your Gherkin you are making things much harder for yourself. In addition you are using before and after hooks to make this work.

Another problem is that your scenarios do not make sense. They are all about making appointments for orders, but your don't at any point create the order.

Finally you have two identical scenarios that you say do different things. The first is for New, the second is for Change. There has to be some difference otherwise you would not need the second scenario.

What I would do is try and extract a single scenario out of this tangle and use that to diagnose any problems. You should be able to end up with something like

Scenario: Create an appointment for an order
  Given there is an order
  And appointments are made using SOAP
  When a new start appointment is made for the order
  Then the appointment should be created
  And the appointment should have a history

There should be no reason why you can't make this scenario work without any @before or @after. When you have this working then create other scenarios whatever other cases you are trying to examine. Again you should be able to do this without doing any of the following

  1. Using example data to differentiate between scenarios
  2. Using outlines
  3. Using @before and @after

When using Cucumber you want to push the complexity of automation outside of Cucumber. Either pull it up to script before Cucumber starts, or push it down to execute in helper methods that are called in a single step definition. If you keep the complexity in Cucumber and in particular try and link scenarios to each other and use @before and @after to keep state between scenarios you will not have a pleasant time using (misusing) Cucumber.

Its far more likely that your problems are caused by your code than by Cucumbers. Even if Cucumber did have a problem with outlines and hooks, you can fix your problems by just not using them. Outlines are completely unnecessary and hooks are mostly misused.