Cucumber Tutorial – What is Feature File in Cucumber

HOME

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.

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

Provide the name to the Source Folder and click on the Finish button.

This creates a new Source Folder.

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.

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

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.

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

Congratulations. We can create a Feature file. Happy Learning!!

2 thoughts on “Cucumber Tutorial – What is Feature File in Cucumber

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

    Like

Leave a comment