How to Write Step Definitions for Cucumber Java

This is Part 2 of the Cucumber Java Tutorial Series. Check out Part 1 for Setting Up Cucumber Java Project.

Cucumber Step Definitions

As seen in the previous tutorial, we can create easily readable scenarios in gherkin grammar and execute them. But we need to tell Cucumber how to execute them using step definition – we have seen a basic one in the previous post. In this tutorial, we will see how to write more complex step definitions including single arguments, lists etc.

Without Arguments

We will take a look at the step definition we have seen in the previous post – the one without any arguments.

Here the methods were created with the same name as we had given in the scenarios as show below.


Single Argument Step Definitions

Now let’s change the script to take the website name as an argument. This is useful because the same method can be used to navigate to some other page thus enabling code reuse.
Here the step definition is written as shown below

As you can see the url was removed from the annotation and replaced with regular expression and a string argument is added to the method. As you might have realized by now, the method name can be anything as long as the step in scenario matches the regular expression for the method.
Similarly, you can modify the step to add integer arguments as well.
For step :

SEE ALSO:   How to Setup a Cucumber Java Project

You can write the method as

One Dimensional Lists

We may have cases where we have to pass in a list of values. It can be done by changing the argument to List as shown below.


The corresponding step

One Dimensional Lists with Custom Delimiter

We can change the delimiter from the default comma to any value we need using @Delimiter annotation. Sample is shown below.


One Dimensional List using Data Table

The same can be done using Data Table as shown below.

Single method for Multiple Statements

In some case, we may have to handle multiple statements with a single step definition method. In simpler cases, you can handle it with regular expressions itself. For eg, we can have a single method to handle ‘I navigate to’ and ‘I go to’ using the step definition as shown below.

Note that the ?: is to denote the first group as non-capturing.

SEE ALSO:   WebDriver Tutorial 1 : Basic Project Setup

Step Definitions Using Examples

We may have run a scenario for multiple sets of data. It is done using Examples keyword. We define the Scenario Outline (notice the Outline which was not present in previous Scenarios) as shown below.


Here we run the scenario for two sets of data. The arguments are mentioned as variables within angle brackets the values of which are provided in tabular form under Examples section. The step definitions are written as methods with arguments similar to the ones we saw above.

Hope this post was helpful to you. For more such posts, subscribe to our newsletter or follow us on Facebook or Twitter.

You may also like...

3 Responses

  1. Shobhit Agarwal says:

    Nice tutorial.
    i have two questions…
    how we can handle backround in step definition while we have some common steps in all scenarios…

    for e.g.
    Given home page open
    And user is open


    Question 2:
    Can we break these scenarios in multiple feature files if yes…can u please post some live example


    • Shobhit Agarwal says:

      Typo error, Background code like this…rest all are same as i mentioned above

      Given home page open
      And user is login

    • Amal Bose says:

      Q1 : You can specify the Background as you had given here. The background is run before each of your scenarios. So in this case ‘Given home page open’ and ‘And user is login’ will be run before each of the two scenarios provided below it.
      Q2 : You definitely can. Just create another file with ‘.feature’ extension and place it in the features folder. As mentioned in post, it will run both the features.

Leave a Reply

Your email address will not be published. Required fields are marked *