Top New Features of Cucumber JVM v6

Advanced Topics, Getting Started — Published August 6, 2021

Behavior Driven Development or BDD is one of the magical terms that many organizations are looking for today. The influence of the BDD methodology has significantly impacted the way the development model works. Its powerful business-driven approach has helped many teams collaborate with different stakeholders to define a better requirement.

One of the well-known tools that help to automate the requirements for the BDD projects is Cucumber. In this article, you will learn about some of the coolest features available as part of Cucumber 6 (cucumber-jvm) & previous versions and how you can leverage them with your automation pack and business discussions.

First, the basics!

For people who are new to Cucumber, the Gherkin language is simple English (or localised) statements put together to make a sensible requirement. It is written in Given-When-Then-But-And format.

The business requirements or the acceptance criteria are written as Scenarios, containing the Gherkin format requirements. Parameterising the scenarios with various combinations are achieved using Scenario Outlines with examples. All these Scenarios are written in a Cucumber feature file in the Cucumber framework.

A sample for a typical feature file is given below:

Feature: Zero Bank - Purchase foreign currency cash
In order to purchase foreign currency
As a valid customer of Zero Bank
I want to have a feature to purchase different types of currencies
Scenario: Calculate conversion cost from USD to GBP
Given I am in the Purchase Foreign Currency page
And I select the currency type as "Great Britain (pound)"
When I try to calculate the conversion cost for "100" USD
Then I should be displayed with the conversion amount with "Great Britain (pound)"
Scenario Outline: Calculate conversion cost from USD to a selected currency type
Given I am in the Purchase Foreign Currency page
And I select the currency type as "<currencyType>"
When I try to calculate the conversion cost for "<usdAmount>" USD
Then I should be displayed with the conversion amount with "<currencyType>"
Examples:
|currencyType|usdAmount|
|Australia (dollar)|100|
|Canada (dollar)|50|
|Switzerland (franc)|75|
|Denmark (krone)|100|
|New Zealand (dollar)|1200|

Rules & Examples:

One of the major features released in cucumber-jvm 6.0.0 is the usage of the Rule keyword. Although it is not a new feature to Cucumber, as it was released first in the cucumber-ruby 4.x and Gherkin 6.0. It’s an optional keyword, but can be very powerful in some business cases.

In general, the Rule keyword will help the team members to think of the scenarios as examples of acceptance criteria or business rules. Examples are different types of scenarios in which the requirement has to be mapped.

This feature will be very useful during the three amigos session, especially for the product owners and to provide better living documentation. The best example for real-time usage can be like this:

Feature: Zero Bank - Purchase foreign currency cash
The Zero Bank app has a feature to buy foreign currency cash if you are travelling to a specific country.
This feature will help the travellers to purchase the native currency much earlier and faster.
Showing the current selling rate will help travellers to understand the conversion rate for one local currency to USD.
Rule: The conversion amount will depend on the daily sell rate of the selected currency
Example: Displaying the current selling rate
Given I am in the Purchase Foreign Currency page
And I select the currency type as "GBP"
Then the current selling rate for the "GBP" equivalent to USD should be displayed

Other useful “Keywords”:

Apart from the Rules keyword, many keywords allow the business stakeholders to define the right set of requirements.

Some of them are:

  • Ability: To provide details on the ability of the feature or module
  • Business Need: What is the business requirement for the feature or module
  • Scenario Template: Defines a scenario of a requirement
  • Scenarios: Contains more examples for a defined scenario

The keywords are interchangeable as follows:

High-Level RequirementFeature, Ability, Business Need
ScenarioScenario Outline, Scenario Template
Combinations / ParametersExamples, Scenarios

Though there are new keywords introduced to the Feature file, the way of writing the step definitions remains the same. Cucumber can automatically deduct the scenarios and examples. You can see the step definitions for the above example here.

A sample for Ability, Scenario Template & Scenarios:

Ability: Zero Bank - Purchase foreign currency cash
Scenario Template: Buy multiple foreign currencies
Given I am in the Purchase Foreign Currency page
And I select the currency type as "<currencyType>"
When I try to calculate the conversion cost for "<usdAmount>" USD
Then I should be displayed with the conversion amount with "<currencyType>"
Scenarios:
|currencyType |usdAmount|
|AUD |100 |
|CAD |50 |
|GBP |1200 |

Online Cucumber Reports:

This is one of the coolest features available as part of Cucumber to date. You can now view Cucumber reports online with a link generated after every execution. The reports show the test results and act as living documentation. The online reports will be published via the https://reports.cucumber.io site with a unique URL provided in the console output and it will self-destruct in a day, but you can also store it if you wish.

Sample Online Cucumber Report

This feature can be enabled by either:

  • Adding Cucumber.properties file in src/test/resources/cucumber.properties and adding the below code:
cucumber.publish.enabled=true
  • Adding Junit Platform Properties file in src/test/resources/junit-platform.properties with the below code:
cucumber.publish.enabled=true
  • Add an environment variable:
CUCUMBER_PUBLISH_ENABLED=true
  •  From Junit Runner class:
@CucumberOptions(publish = true)

Conclusion

The new features in Cucumber 6 will be of great help to the business stakeholders who like to define their requirements more understandably. This will enable more scenarios to be added for various business needs and for you to perform more collaborative work towards delivering a stable product.

Cucumber Video Demo

References:

  1. Sample code for this Cucumber Demo on GitHub
  2. Cucumber-jvm on GitHub
  3. Cucumber installation docs (java)
  4. Cucumber report collections
  5. Zero Web Security

Are you ready?

Get started Schedule a demo