• Skip to main content
  • Skip to primary sidebar

AutomationTestingHub

Selenium, Appium, Cucumber and more !!!

How to create Cucumber Step Definition class

March 20, 2018 by anish Leave a Comment

This article is a continuation of our previous one, where you learnt how to create a cucumber test runner class. In this article, you will see how to create cucumber step definition class. After you create this class, you will also add the step definition methods to it. Before we start creating step definition class, let’s first get some basic understanding about it.

Note: This article is a part of our Cucumber Tutorial series. Head over to our Selenium Cucumber Tutorial main page for the step by step cucumber setup guide

What is Cucumber step definition class?

Cucumber step definition class is a normal java class where you can store step definition methods. And a step definition method is a java method that is linked to a step in the scenario in feature file. For each scenario step in your feature file, you should add a step definition method. This way, all your steps in feature files will have a method associated to it. So that, when you run the feature file, Cucumber would execute the step definition methods linked with the steps.

Let us now identify the step definitions (also called as step defs) that you need to add in your project.

Identify the step definition methods to be added

One of the good things about Cucumber is that, it itself provides you the list of step definition methods which you have not implemented yet. It also provides you with the syntax of these methods. Let’s see the steps using which you can generate these step definition methods –

1. In Eclipse, open the test runner class, TestRunner_GoogleHomepage, which you created in the previous article (create cucumber test runner class)

2. Right click anywhere inside the class and select Run As > JUnit Test

3. Once the test execution completes, you will see that Eclipse pops up console output window on the screen. In this console output window, cucumber first displays this message – You can implement missing steps with the snippets below.

Below this message, cucumber lists down all the step definition methods that you have to implement.

you can implement missing steps with the snippets below

This way, cucumber has itself generated the list of step defs that you have to implement. Also, it provides you with the correct syntax of these methods. All that you have to do now is to copy paste these methods to the step definition class. Let’s see how we can do this.

Create new step definition class

In this section, we will create a new package and then add a new step definition class to it. The steps for this are given below –

1. Right click on src folder and select New > Package

Select New - Package for step definition class

2. Give package name as stepDefinitions and click on Finish button

Provide package name for step definition class

3. New package is now successfully added in src folder. Now, right click on this package and select New > Class

Select New - Class to add a new cucumber step definition class

4. Enter class name as StepDefs_GoogleHomepage and click on Finish button

provide cucumber step definition class name

With this, you have successfully created cucumber step definition class. Let us now add the step definition methods to this class.

Add step definition methods to the class

Follow the steps given below to add step definition methods to the class –

1. Open the console window in Eclipse and copy all the methods that are given after “You can implement missing steps with the snippets below:” line

2. Paste all these methods in StepDefs_GoogleHomepage class as shown below

Add step def methods from console

3. Add import statements for Given, When and Then. After this, delete the default comment and throw new PendingException() statement from each step definition method. The code will now look as in the below image. Please note that it doesn’t have any errors now

Import statements and resolve all errors from the step defs class

With this, you have now implemented all the step definition methods at a basic level. As of now, these methods are empty. This means that if you re-run the test runner class, you won’t see any message for unimplemented steps as you saw previously.

Just so that we see some results when we run this test again, let us add print statements in each of these methods. With this approach, when you run the scripts, there will be some text printed on the console. This will show that the step definitions methods were executed.

4. Add System.out.println() method in each step definition method. The complete code will look like this

Add print statements in step def methods

Let us now re-run the test runner class and check that the step definition methods that you have added work fine.

Run cucumber test runner class once again

Before we run the cucumber test runner once again, we need to add the path of the step definition class in the test runner. Let’s do this first and then run the test runner class.

1. Open TestRunner_GoogleHomepage class

2. In glue keyword, add the package name of step definition class. Complete @CucumberOptions annotation is given below

@CucumberOptions(features="resources/features", glue="stepDefinitions")

For better readability, you can split this in multiple lines as well

@CucumberOptions(
		features="resources/features", 
		glue="stepDefinitions"
		)

The test runner class should look as shown in the below image

cucumber test runner class with path for step defs class

3. Now, right click inside this class and select Run As > JUnit Test

4. Once the test case execution completes, console view would be displayed. In the console view, you can see that there is no message for missing implementation for steps. Instead, the console displays all the statements that you had added in step definition methods. This means that the test runner has executed all the step definition methods successfully.

All cucumber step definition methods are executed


We will end the article at this point, as you have completed creating a basic cucumber step definition class. Even though this class is kind of empty, as it just prints out some statements on the console. But from cucumber setup point of view, this is a big step.

In our next article, we will create basic selenium methods and add it to these step definition methods. This way, we have a full-fledged script that performs some actions on a web page. If you have any doubts or any other general feedback, then you can get in touch with us using the comments section.

Next Article: Add selenium logic to the step def methods

Filed Under: Cucumber

Primary Sidebar

Automation Testing Hub

Tutorials Guide

Write test scripts in BDD format using our Cucumber Selenium Tutorial

Use our Appium Tutorial Series to setup Appium from scratch

Categories

  • Android
  • Appium
  • Cucumber
  • Java
  • Playwright
  • Selenium
  • Tosca

Recent Posts

  • Appium Inspector – Download and Install
  • How to download Chromedriver for Selenium
  • Run Playwright tests on different browsers
  • Playwright Automation Testing Tutorial
  • Selenium IDE Chrome Download

Recent Comments

  • Run Playwright tests on different browsers - AutomationTestingHub on Playwright Automation Testing Tutorial
  • Confluence: CPSSoft Test Framework on Appium Inspector – Download and Install
  • Playwright Automation Testing Tutorial - AutomationTestingHub on Download and Install – Node.js and NPM
  • Chrome浏览器在使用Java的Appium中无法启动。 | JAVA语言的经验与见解 on 2 Ways to find appPackage and appActivity name of your App
  • Appium Inspector Tutorial - AutomationTestingHub on Download and Install Appium 2 Server

Archives

  • January 2024
  • July 2023
  • March 2023
  • February 2023
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016

Latest Selenium 3 Guide

Are you using the latest version of Selenium WebDriver? It provides lot better support for the latest browser versions. Check it out here - Setup Latest Selenium WebDriver

Categories

  • Android
  • Appium
  • Cucumber
  • Java
  • Playwright
  • Selenium
  • Tosca

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in

Go to mobile version