Search:

Simplify Cucumber Steps

In the later stages of a test automation project, code complexity increases due to development. The following tips can reduce maintenance cost and complexity and achieve a better project structure.

How to Simplify Cucumber Testing Tool Steps? - kloia Blog

Cucumber is a testing tool which is using Behavior Driven Development approach. It helps test engineer to develop scenarios with Gherkin syntax easily. However, in the later stages of the project, code complexity is increased depending on development.

The following tips could be used to reduce maintenance cost and complexity, have better project structure.

1-Background

The scenarios in a feature file usually start with the same steps. Do not repeat these common steps in each scenario, gather the steps under background to prevent the repeat.

Screen Shot 2020-02-19 at 14.30.31

Using Background option in the feature file gathers the common steps at the top of the file and prevents to repeat the same steps for each scenario.


Feature: Credit Cart Payment

  Background:
    Given visit homepage
    And login as buyer
    And add product to basket
    And go to payment page

  @payment
  Scenario: Payment without installment
    When pay by credit card without installment
    Then verify checkout result page

  @payment
  Scenario: Payment with 2 installment
    When pay by credit card with 2 installment
    Then verify checkout result page

  @payment
  Scenario: Payment with 3 installment

    When pay by credit card with 3 installment
    Then verify checkout result page

 

 

2- Scenario Outline

If you want to run a scenario with a dataset, use "Scenario Outline" instead of "Scenario".


Feature: Credit Cart Payment

  Background:
    Given visit homepage
    And login as buyer
    And add product to basket
    And go to payment page

  @payment
  Scenario: Payment without installment
    When pay by credit card without installment
    Then verify checkout result page

  @payment
  Scenario: Payment with 2 installment
    When pay by credit card with 2 installment
    Then verify checkout result page

  @payment
  Scenario: Payment with 3 installment

    When pay by credit card with 3 installment
    Then verify checkout result page

 

Using “Scenario Outline” provides more readable feature files as you see below.


Feature: Credit Card Payment

  @payment
  Scenario Outline: Payment without installment
    Given visit homepage
    And login as <user>
    And add product to basket
    And go to payment page
    When pay by credit card <installment>
    Then verify checkout result page 
    Examples:
      | user   | installment         |
      | user_A | without installment |
      | user_B | with 2 installment  |
      | user_C | with 3 installment  |

3- Combine Steps in a Step

It is possible to group step definitions and create higher-level step definitions. It helps to reduce extra lines in feature files, like previous sections, making it easier to read.


Feature: Credit Cart Payment

  @payment
  Scenario: Payment without installment
    Given visit homepage
    And go to login page
    And fill email
    And fill password
    And click login button
    Then verify welcome page
    When add product to basket
    And go to payment page
    When pay by credit card without installment
    Then verify checkout result page

 


Feature: Credit Cart Payment

  @payment
  Scenario: Payment without installment
    Given visit homepage
    And login as buyer
    When add product to basket
    And go to payment page
    When pay by credit card without installment
    Then verify checkout result page

 

4- Table structure

Although it seems like the Scenario Outline structure at the beginning; the data sets of the data repeat itself only for this step. If the Scenario is Outline, Scenario works as much as the number of data. If it is in the Table structure, the corresponding step repeats itself as much as the number of data defined, but the whole of our scenario runs once.


Feature: Sign-up form

  @signup
  Scenario: Create a new member
    Given visit homepage
    And click Sign-up link
    When fill input fields:
      | Name        | kloiaqa           |
      | Email       | kloiaqa@kloia.com |
      | Password    | your_pass         |
      | Re_password | your_pass         |
    And click Sing-up button
    Then verify welcome page

Cucumber makes scenario development easier thanks to its advantages.
If you use Cucumber’s useful features which mentioned above, your feature files will be more readable and efficient. Beside, these features make maintaining easy for each team member.

Ali Fuat Ateş

Junior Test Automation Consultant at kloia