Search code examples
cucumberbddcucumber-jvmgherkincucumber-java

How to write Tests with multiple steps in cucumber


If you have a simple form, you enter your name, Gender and save it. When you select the Gender drop-down it will list [Male | Female].

So when writing test scenarios for the above, the typical way is to Write it in steps.

Precondition : User Loged in and Form Opened

Steps                                | Expected Result
------------------------------------------------------------------------
1 : User Enters the name             | Entered name should be there         
2 : User Clicks on Gender Drop-down  | Male and Female should be listed
3 : Users Selects a Gender           | Selected Gender should be there
4 : Clicks on Save                   | Information get saved.   

Following is one way to represent this is Cucumber.

Given User Loged in and Form Opened
When User Enters the name
Then Entered name should be there         
When User Clicks on Gender Drop-down
Then Male and Female should be listed
When Users Selects a Gender
Then Selected Gender should be there
When Clicks on Save 
Then Information get saved. 

Now How can we represent this in Cucumber? As I read you are not suppose to have multiple actions within the same scenario. If you add multiple testcases for each step above, the Testcases will grow exponentially. What is the best way to handle this?


Solution

  • My approach would be to think about documenting in Gherkin (using principles of BDD) - the application behavior and not test cases. In fact, this article provides some good context around misconceptions of BDD.

    With that said, even though you are trying to validate the above criteria in a single scenario, I would recommend at least splitting into 2, to keep your acceptance tests succinct, readable and maintainable.

    Here's my attempt, at reformulating:

    Scenario: Gender Dropdown values
    Given a user is logged in
    When the user clicks the Gender dropdown
    Then Male and Female options are displayed
    
    @EndtoEnd
    Scenario Outline: Saving User information
    Given a user is logged in
    When the user enters a <Name> and selects a <Gender>
    And Clicks on Save
    Then the information gets saved
    
    Examples:
    |Name|Gender|
    |Keith|Male|
    |Jenn|Female|
    ||Female| # Negative
    |Rob||    # Negative
    

    Additionally, you can also think about throwing in some negative test scenarios as well.