Skip to main content

Cucumber Step Definition

A feature file has many Steps, and each Step has a Step Definition associated with it. When Cucumber executes a Step in a Scenario, it looks for a matching Step Definition to execute. Step Definitions are also known as Glue Code.

Here is the sample feature file consisting of a single scenario and 2 steps starting with the When and Then keyword. Copy the below code and paste it to your feature file:

Feature: Title of your feature

  Scenario: Title of test scenario
  Given I am on the homepage
  And I click on login link

Create a Step Definition file

Step Definitions File is your regular Java Class file without including your main class. You can create it using a different way depending on your IDE.

If you have installed Cucumber Eclipse Plugin, you can generate a step definition file with annotations of your choice.

How to generate Step Definitions

There are several methods to run a feature file. 

  1. Cucumber Eclipse Plugin: Install Cucumber Eclipse plugin and Just right-click on your feature file and click Run As -> Cucumber Feature.
  2. JUnit Runner: Add your tag and set the dry run option to true and execute your Runner class Run As -> JUnit Test 
  3. Tidy Gherkin Plugin: Install Tidy Gherkin Plugin and Copy-paste your steps in the Tidy Gherkin Application.

Cucumber Step Definitions Java

After running the feature file or generating the steps using the plugin, you will see the below code. These are Cucumber Step Definitions which Cucumber auto-generates for us.

If you have executed using runner, you can see that one scenario is executed, and it has two steps, and both are undefined, and we got the suggestion that we can implement missing steps. Copy-paste code snippet starting with @When and @Then to the Step Definition file.

Undefined scenarios:
file:///Users/tarungoswami/git/cucumber-java-demo/src/test/resources/features/test.feature:22 # Title of test scenario

1 Scenarios (1 undefined)
2 Steps (1 skipped, 1 undefined)

You can implement missing steps with the snippets below:

@Given("I am on the homepage")
public void i_am_on_the_homepage() {
    // Write code here that turns the phrase above into concrete actions
    throw new;

@Given("I click on login link")
public void i_click_on_login_link() {
    // Write code here that turns the phrase above into concrete actions
    throw new;

After that, comment or delete the line starting with the throw keyword. Now execute your feature file again. You will not receive any errors. You can place your selenium or java code inside these methods.

In Our JUnit Test Runner Class, insider Cucumber Options, we specify the path for glue code. When Cucumber starts execution, it looks for classes in the path mentioned in the glue code.

glue = {"com.pb.cucumbertest"}

@Given, @When, and @Then are Cucumber annotations in the above code. There is text inside ("") which Cucumber matches your steps written in the Feature file. After the match was found, Cucumber executed the method associated with it. If a match is not found, Cucumber will return an Undefined Steps error.

cucumber-java8 allows step definitions to be defined as lambda's expressions.

What is Step Definition in Cucumber

  • A Step Definition is a piece of code with a pattern attached. Or in other words, a Step Definition is a java method with an expression and annotation above it.
  • A step definition's expression can either be a Cucumber Expression or a Regular Expression.
  • Step definitions aren't connected to a single feature file or scenario. The class file or package name of a step definition does not affect what Gherkin steps it will check. The only thing that matters is the step definition's expression.

Video Tutorial: Cucumber Step Definitions Java

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.