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.
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.
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
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 |
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
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.