Adding Test Case To Cucumber Files

Reading Time: 3 minutes

In continuation with my previous blog, complete eclipse setup for cucumber, this blog will help us in adding Test Case To Cucumber files and thus, creating a sample project.

In first place, we have covered step 1 to step 4. Likewise,we will go ahead from step 5 to step 7

Adding Test case To Cucumber Feature File

Step 5 :
Firstly, Adding data to your first feature file in the project after identifying a particular scenario. Additionally, it will have .feature extension.

Please note that your file may show errors in first step as you write this code. To remove errors, a plugin is required.
Natural plugin can be installed from the market place to identify the feature keywords.
Just like mentioned in the earlier blog, file will look like below image.

Some important points to remember about feature file :
1. ‘Feature’ keyword is mandatory as without this you will face a compilation error
2. Order of keywords can be in any sequence.
3. Each Feature can have multiple scenarios
4. Not necessarily each scenario should contain ‘when’ , ‘then’,’and’ and ‘given’

Cucumber Test Runner File

Step 6 :
Since we have created our feature file by this point, so now need to add a java file to run this feature file

Now, as we have already created package for test runner in previous blog. We will simply create a java file inside our created package.
Add the following code as it is in your test runner file.

Let’s have the knowledge of Annotations used in above image

As a matter of fact, @RunWith Annotation is from the Junit class. This annotation instructs that test should run using cucumber class and use cucumber api.
Equally important, @CucumberOptions provides a frontage of many things like where our feature file is actually located. In layman terms, it acts as a bridge between java and cucumber.

The first parameter, called features, provides the location of the feature file. Similarly, the second parameter, called glue, provides the path of the step definition class.

Cucumber Step Definition File

Lastly, Our intention for this blog is to set Step Definition file. Likewise, test runner, we have already created a separate package for Step Definition File.

This file is nothing but a simple .java file in which we write our selenium code and required logic. It links with each and every scenario.

Trick :

To have the steps corresponding to feature file in Step definition, simply run your runner file as Junit. In console you will find missing snippets to add in the step definition file.
This way cucumber helps us to give the definition to every sentence corresponding to feature file without a miss!

Afterwards, your step definition file will look like this.

Finally you can again run your test runner file to generate the expected output.


Written by 

Nearly 3 years of experience in automation testing, I call myself an automation enthusiast. I can create, execute automated test scripts using framework guidelines and best practices to ensure wider and efficient end-to-end automation coverage. Simultaneous experience in Defect tracking and bug reporting through JIRA