0

Steps from the Background cannot be combined with Examples. Is it a limitation with Behat.

I have the following feature file.

Background:
        Given I have the login Page
        When I login to the application using "<username>"
        Then the list is displayed
        When I select an item from the list
        Then I am taken to the Dashboard
        When I navigate to the Overview Page
        Then the Overview Page is displayed

@javascript @frontend @devlocal
Scenario Outline: To verify the Overview page content   
        Then overview page main headings are displayed
Examples:
        | username  | role          |
        | RoleUser  | ROLE_USER     |       
        | RoleAdmn  | ROLE_ADMIN    |

This produces the error: [Behat\Gherkin\Exception\ParserException]
Expected Comment or Scenario or Outline or Step token, but got Examples on line: 15 in file: /var/Features/Overview.feature

The reason I have so many steps in the "Background" is because that is the logical flow to get to a page. And I have several scenarios using those steps.

I do have a working solution for this problem which is using all those steps from the "Background" within the "Scenario". This works absolutely fine. However I have 5 different scenarios for this feature and if I copy and paste the same steps within all the 5 scenarios it looks absolutely messy and cumbersome. I thought this was the main reason why we use Background section?

Here is what I'm using at the moment:

Background: Given I have the login Page

@javascript @frontend @devlocal
Scenario: To verify the Overview page content   
        When I login to the application using "<username>"
        Then the list is displayed
        When I select an item from the list
        Then I am taken to the Dashboard
        When I navigate to the Overview Page
        Then the Overview Page is displayed
        Then overview page main headings are displayed
Examples:
        | username  | role          |
        | RoleUser  | ROLE_USER     |       
        | RoleAdmn  | ROLE_ADMIN    |

Imagine having all those steps from the Scenario section for all the different scenarios

vijay pujar
  • 1,683
  • 4
  • 19
  • 32

1 Answers1

1

Absolutely messy and cumbersome would be if there was no scenario outlines. I can relate with the issue but this is just the way it works. Scenario background is supposed to be combined with all scenarios in the feature and supposed to work as a standalone block. If you had five scenario outlines and one scenario, your feature would not work, because the background wouldn't know what to do with the <username>.

In theory this could be fairly easily implemented, but being familiar with Behat code a little bit, I feel this would be a very-very-very big change… In fact I raised this issue to propose a new feature request, it would be nice to have something like that. You can subscribe and see what the guys say about it. I'm almost confident the cool factor of this won't win over the complexity.

On the other hand, nothing stops you from combining this logic into another step and passing the necessary parameters there. You should be able to access other contexts from mink and call steps as standard methods. Moving that logic from the feature into the context will clean things up a little bit, but imho this is a little hacky and not a much better approach. In fact I don't think theres anything wrong with what you currently have.

Also, from the logic perspective, you are trying to test too much stuff in your background. Instead, you can create another scenario to test everything that goes in your background, and in other five have just two steps at the beginning instead of six (When I login to the application using "<username>" and Then I successfully navigate the Overview Page is displayed).

Ian Bytchek
  • 8,804
  • 6
  • 46
  • 72
  • Thanks Ian. What do you mean by combining the 6 steps in to 2 steps. Would this mean combining all those 6 steps logic in to 2. That would be a bit of redundancy isn't it (writing the same code again for different scenarios) – vijay pujar Sep 11 '14 at 12:31
  • 1
    Your background contains the common logic that is "inherited" by each outline. You can separate that common logic from the background into another scenario, so instead of checking that background steps pass in each scenario outline you can do the same in a separate scenario outline, thus reducing the load on all other five scenario outlines. There you simply will need login and go the the page from which scenario-specific testing begins. – Ian Bytchek Sep 11 '14 at 12:49
  • 1
    Using the same letters in the code can also be considered redundancy in some aspects. You have limitations and you want to find a way to repeat yourself as little as possible, you can rearrange your code to make it DRY'er, that can make things considerably better. – Ian Bytchek Sep 11 '14 at 12:52
  • 1
    That makes perfect sense now. I may have to revisit loads of my scenarios and try and follow the recommendations above. In a way I will have some redundancy in the code however the feature files would look much cleaner. Thanks a lot. – vijay pujar Sep 11 '14 at 13:03