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 'StepDefinitions.java' 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 'StepDefinitions.java' 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

7 comments:

  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

    ReplyDelete
  2. 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

    ReplyDelete

  3. 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

    ReplyDelete

  4. 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

    ReplyDelete
  5. I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me. write essays for money

    ReplyDelete

DevOps 01: What is DevOps and How it benefits organizations?

DevOps is a culture in an organization, where the development team and operations team help each other by sharing information, process and t...