Tutorial 3 – Create feature and Step definition file in cucumber
What you will Learn :
Create a feature file
Configure the cucumber feature
Execute the feature file
Create Step Definition file
Create a feature file
Right click src/test/java > New > Package
Let us name the package as ‘Features’ (you can also name the package as ‘AppFeatures’ or any other name you wish to)
Finish, the package gets created
Next, right click ‘Features’ package > New > File
The filename should describe some feature (example: login, search, addtocart etc). So, let us mention a file name with .feature suffix as seen below
Finish
Notice below that feature file gets created with some default features. This is due to the cucumber plugin that we had installed in the previous tutorial
You can also see some of the comments (lines 1-18) that describe some of the keywords for your ready reference.
Let us remove the sample features (line#19 onwards)
We will create our own feature using the Feature: reserved keyword. So let us write Feature:
Now hit spacebar to create whitespace. Notice that the color changes
Let us give some name to our feature
Let us now write scenario name using Scenario: keyword
Let us now write the ‘Given’ keyword which defines some pre-condition
Carefully see line#22 above. Do NOT add colon after ‘Given’ keyword. Thus is wrong. You should use
Next, we will write the ‘When’ step that defines some action. Since the name of the course is a string, it should be in double quotes, the price is numeric and hence without quotes
Next we will write ‘Then’ statement that defines an outcome
So this was our first scenario. Likewise, you can write any number of scenarios for search feature.
Configure the cucumber feature
Let us see how to configure cucumber feature
Right click feature file > Run As > Run Configurations
See below. Expand ‘Cucumber Feature’ and select New_configuration.
Click ‘Browse’ button and select the path of the feature file
Execute the feature file
Click Apply, click Run
The below message would be seen in console:
The console has suggested us 3 java methods (code snippets) that need to be implemented. So basically, we have not yet implemented the 3 steps in our feature file. The 3 steps are: Given, When, Then.
The feature file currently is in plain English language.
You can also execute the feature file by right clicking the feature file > Run As > Cucumber Feature, see below.
Create Step Definition
Recall the cucumber framework design that we had discussed in previous tutorial. In a step definition file, we map the Give/When/Then steps using @ annotations
Now, under src/test/java, create a new package ‘StepDefinitions’ and under this package, create a class
Next, go to the console and copy the code snippets
Paste these methods in step definition file:
Right now there are 3 errors. Press Ctrl+Shift+o to import Given/When/Then
The errors are resolved now.
See below. The ‘Given’ step in feature file is mapped to @Given in step definition file. Also, the exact step name written in feature file "Search field should be present on the Way2Automation website" is mapped to the step of @Given method in the step def file
Next, whatever is the step name "Search field should be present on the Way2Automation website", cucumber will generate the same method name
Note: You can change the method name to any other desired name, example search_field_should_be_present_on_the_website(). However, the step name CANNOT be changed. So, you cannot change the step name to @Given("Search field should be present on the website").
Next, the string that we mentioned within double quotes in the ‘When’ step, translates to {string} in the step definition file. Similalrly, the integer we mentioned in the ‘When’ step, translates to {int} in the step definition file. Also, cucumber automatically generates 2 method parameters: String, Integer
Next, you can change the variable names as seen below
Similarly, you can change the variable name in @Then method
One thing that you notice in the step definition file is that, cucumber automatically generates an exception. The reason being, we have not yet written any java code for the respective steps.
Next, run the feature file
So, when you now run the feature file, the console shows the same Exception
To resolve this, let us remove the exception from the step def file, see below
Next, we will write some simple java code SOPs
Whatever course name and price you have written in the feature file, the same gets passed to ‘courseName’ and ‘price’ variables respectively in step def file.
Next, let us again run the feature file
Notice the console output, all 3 steps got passed this time. There are no exceptions.
Also notice that the respective course name and price gets passed to the SOPs, see below. You can also see the respective Give/When/Then steps in the console below. The course name mentioned in When/Then steps matches with the course names in respective SOPs
So, this is how we implement the step definition file.
Thank you for reading!