Sunday, January 7, 2018

How to write the Step Definitions for the Cucumber tests?

In the previous tutorial we learned about writing the scenarios and steps in the feature file. Also we seen that steps being highlighted in yellow color since they do not have step definitions. Now we will learn writing the step definitions.

I will be taking only one scenario for simplicity. You can follow same approach for remaining scenarios.

Step 1: Create a package say 'cucumberStepsDefinion' as shown below.

Copy the path of the 'cucumberStepsDefinion' package.

Step 2: Keep the cursor on the any step and press 'Alt+ENTER', you will get a pop up with two options 'Create Step Definition' and 'Create All Step Definitions'.

Step 3: Select 'Create Step Definition' (anyways 'Create All Step Definitions' is not working in the first attempt), then paste 'cucumberStepsDefinition' package file path in File location and enter 'StepDefinitions' class name. Click on 'OK' button.

You should see step definition being created in the newly created '' file as below

Step 4: For remaining steps place cursor and press 'Alt+ENTER', this time select 'Create steps definition' and IntelliJ will asks you to choose 'Create new file' or existing 'StepDefinitions' file. Select 'StepDefinitions'

Now you should see all the step definitions being added to the '' file as below

Step 5: Right Click on the 'gmailLoginFeature.feature' and Run 'Feature:gmailLoginFeature'. When you do this we get something like below in the console

Even though we defined the step definitions we are getting 'Undefined steps' in the console. The reason is, we have not mapped our steps from feature file with step definitions.

Step 6: To map them, click on the Edit Configuration in IntelliJ

Step 7: Then select 'Cucumber java' in the left pane, and in the right pane provide 'cucumberStepsDefinition' package name in the 'Glue' field, Features folder location in 'folder path'

Click 'Apply' and 'OK'.

Step 8: Now run the 'gmailLoginFeature.feature' gain,  this time you will see something like below (ignore the exception that doesn't matter) which means steps and step definitions are glued (mapped)

Step 9: Delete the existing or default content in the step definitions and write the respective actions that each step has to perform in their step definitions as below

We are not going to discuss selenium code here, you should be having basic idea selenium. This tutorial concentration is how to implement Cucumber in Selenium Automation.

We completed writing the step definitions as well.  I tried my best make it more understandable with all the screenshots. The next part is run the tests using Junit runner class. We will see that in the next part.

Thanks for reading. #seleniumbabu #HappyLearning


  1. Best and unique information from you on the concept of selenium automation testing. Keep updating more info on Selenium.
    selenium testing training in chennai | Best selenium training in chennai

  2. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
    Best selenium training Institute in chennai

  3. Use your education as your most marketable asset. If you're an entry-level candidate with minimal professional experience, your education is your ticket. Coursework Writing Service


  4. Wonderful post!!Thank you for sharing this info with us.
    Keep updating I would like to know more updates on this topic
    Very useful content, I would like to suggest this blog to my friends.

    software testing selenium training

    Selenium Training in Chennai

  5. Thanks for providing the information . The articles in your blog helped me a lot for improving the knowledge on the subject. Also check my small collection on this at selenium Online Training Hyderabad blog

  6. Do not waste time winning money right now we have slot games Win an online casino.


  7. I found so many interesting stuff in your blog especially writing steps for the improvements of dissertation writing service
    . From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work


Java Program 6: How to reverse a String in Java?

In this post we will discuss different methods to reverse a String in java programming. Method 1: Step 1: Read the input string using ...