Writing the scenario

Cucumber is a popular BDD test automation tool. Cucumber-JVM is the Java implementation of Cucumber. In Cucumber, you express acceptance criteria in a natural, human-readable form. In Cucumber, scenarios are stored in Feature Files, which contains an overall description of a feature as well as a number of scenarios. For example there is feature file called search_by_keyword.feature, and looks something like this:

Feature: Searching by keyword
  In order to find items that I would like to purchase
  As a potential buyer
  I want to be able to search for items containing certain words
  Scenario: Should list items related to a specified keyword
    Given I want to buy a wool scarf
    When I search for items containing 'wool'
    Then I should only see items related to ‘wool’

Step definitions

In Cucumber, each line of the Gherkin scenario maps to a method in a Java class, known as a Step Definition. These use annotations like @Given, @Whenand @Then, that match lines in the scenario to Java methods. You define simple expressions to indicate parameters that will be passed into the methods:

public class SearchByKeywordStepDefinitions {
    BuyerSteps buyer;
    @Given("I want to buy (.*)")
    public void buyerWantsToBuy(String article) {
    @When("I search for items containing '(.*)'")
    public void searchByKeyword(String keyword) {
    @Then("I should only see items related to '(.*)'")
    public void resultsForACategoryAndKeywordInARegion(String keyword) {

“BDD Test Automation with Cucumber-JVM” Tech Bite was brought to you by Medžida Mustafić, Junior Test Engineer at Atlantbh. 

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

Software DevelopmentTech Bites
February 23, 2024

Background Jobs in Elixir – Oban

When and why do we need background jobs? Nowadays, background job processing is indispensable in the world of web development. The need for background jobs stems from the fact that synchronous execution of time-consuming and resource-intensive tasks would heavily impact an application's  performance and user experience.  Even though Elixir is…
QA/Test AutomationTech Bites
December 22, 2023

Selenium Grid 4 with Docker

Introduction When talking about automation testing, one of the first things that comes to mind is Selenium. Selenium is a free, open-source automated testing framework used to validate web applications across different browsers and platforms. It is not just a single tool but a suite of software. Every component of…

Want to discuss this in relation to your project? Get in touch:

Leave a Reply