Last Updated On
In the previous tutorial, we discussed How Cucumber can be setup with Eclipse. In this tutorial, we will discuss the Feature files.
What is a Feature File?
Cucumber tests are grouped into features. A Feature file is one in which we store a description of the features and scenarios to be tested. It is used to provide a high-level description of test scenarios and group-related scenarios. A Feature File is an entry point to the Cucumber tests.
The first keyword in the Feature file is the Feature keyword, followed by: and short text that describes the feature.
You can add free-form text underneath the Feature to add more description.
These description lines are ignored by Cucumber at runtime, but are available for reporting (They are included by default in HTML reports).
The name and the optional description have no special meaning to Cucumber. Their purpose is to provide a place for you to document important aspects of the feature, such as a brief explanation and a list of business rules (general acceptance criteria).
The free format description for Feature ends when you start a line with the keyword Example or Scenario Outline (or their alias keywords).
You can place tags above Feature to group related features, independent of your file and directory structure.
A simple feature file consists of the following keywords/parts −
- FeatureFile Name – Name of the feature under test. For example, here is LoginPage.feature
- Feature − Describe the feature under test, like here “Login to HRM Application”
- Scenario − What is the test scenario we want to test
- Given − Prerequisite before the test steps get executed.
- When − Specific condition that should match to execute the next step.
- Then − What should happen if the condition mentioned in WHEN is satisfied.
- And/But – If we have several Given’s, When’s, or Then’s, then we can use And /But.
Steps to create a Feature file
Step 1 – Create a new Maven project.
![](https://qaautomation.expert/wp-content/uploads/2022/06/image-54.png?w=332)
Step 2 – Add cucumber-java dependency to the POM.xml.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.14.0</version>
</dependency>
Step 3 – It is suggested to create the Feature files in the src/test/resources source folder. By default, this folder is not present. So, first, create a Source Folder named src/test/resources. Right-click on the project, and select New → Source Folder.
![](https://qaautomation.expert/wp-content/uploads/2022/06/image-55.png?w=486)
Provide the name to the Source Folder and click on the Finish button.
![](https://qaautomation.expert/wp-content/uploads/2022/06/image-56.png?w=511)
This creates a new Source Folder.
![](https://qaautomation.expert/wp-content/uploads/2022/06/image-57.png?w=305)
Step 4 –We want all our Feature files to be present inside a folder. So, create a new folder with the name Features in the src/test/resources folder.
Right-click on the src/test/resources folder, and select New →Package. Provide the name of Features to the package and click the Finish button.
![](https://qaautomation.expert/wp-content/uploads/2022/06/image-62.png?w=514)
Step 5 – Create a Feature file in the features package.
Right-click on the Feature folder, select New ->File and mention the name LoginPage.feature. Click the Finish button. (Remember to add .feature at the end of the file, otherwise, this feature file will be just an ordinary plain text file).
![](https://qaautomation.expert/wp-content/uploads/2022/06/image-61.png?w=512)
The below image is an example of the new feature file created. This sample feature file gives an idea how what an actual Feature file should look like.
![](https://qaautomation.expert/wp-content/uploads/2022/06/image-59.png?w=719)
Below is an example of a valid feature file.
@LoginPage
Feature: Login to HRM Application
@ValidCredentials
Scenario: Login with valid credentials
Given User is on HRMLogin page "https://opensource-demo.orangehrmlive.com/"
When User enters username as "Admin" and password as "admin123"
Then User should be able to login successfully and new page open
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-39.png?w=836)
Congratulations. We can create a Feature file. Happy Learning!!
I'm trying to have 13 test scenarios in my feature file, but it is showing error. why???
LikeLike
It is advisable to have not more than 10 test scenarios in feature file, but it doesn't mean that if it is more than 10 , there will be error. The error must be of some different reason.
LikeLike