In the previous tutorial, we discussed How Cucumber can be setup with Eclipse. In this tutorial, we will discuss the Feature files.
What is 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 to 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 MyHoliday.feature
- Feature − Describe the feature under test, like here “Book flight ticket”
- Scenario − What is the test scenario we want to test
- Given − Prerequisite before the test steps get executed.
- When − Specific condition which should match in order 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 and add cucumber-java dependency to the POM.xml.
<dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>6.10.4</version> </dependency>
Step 2 – 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.
Provide the name to the Source Folder and click on the Finish button.
This creates a new Source Folder
Step 3 – 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 src/test/resources folder, and select New →Package. Provide name Features to the package and click the Finish button.
Step 4 – Create a Feature file in the Features package.
Right-Click on the Feature folder, select New ->File, and mention the name MyHoliday.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).
The below image is an example of the new feature file created. This sample feature file gives an idea how an actual Feature file should look like.
Below is an example of a valid feature file.
Feature: Book flight ticket @BookOneWayFlight Scenario: Book Flight for one way trip Given I live in Dublin with 2adults and 2kids And I want to book one way flight ticket from Dublin to London on 22nd Jan 2020 When I search online Then TripAdvisor should provide me options of flights on 22nd Jan 2020 And Cost of my flight should not be more than 50 Euro per person And Tickets should be refundable
Congratulations. We are able to create a Feature file. Happy Learning!!