We have talked a lot about Cucumber feature file from the day when we started tutorials on Behaviour Driven Development. Earlier also we have discussed the sample Cucumber feature file in the tutorial on Cucumber introduction. Today we will discuss everything about the feature file and its major components. These components are the building blocks to develop a complete cucumber feature file.
Let’s start the discussion now.
What is Cucumber Feature File?
A feature file is a test definition file which contains the specification in the form of scenarios and steps. The specifications are written with the help of readable language, primarily English, and Gherkin syntax.
Cucumber feature file shares information on what-to-do and the file name ends with .feature extension.
Since readable language along with Gherkin syntax is used to create a feature file, hence, A cucumber feature file is a business-centric.
Click on below link to know more about Gherkin syntax.
What is the structure of the feature file in Cucumber?
A cucumber feature file consists of mainly two sections, which are listed as follows:
- Feature Section
- Scenario Section
Let’s discuss each one of them separately.
Feature Section
A feature is a topmost section in the Cucumber feature file. It consists of the following two components:
- Test case title
- Description
Below example depicts the Feature section in the feature file:
Feature: Facebook Login Validation #This is test case name This feature is about validation of login functionality of Facebook #It is the description of the test case
Let’s have a look at some points to remember for Feature section:
- A feature referred to as the functionality of the story
- The feature will start with the Feature keyword (as shown above)
Now jumping to the Description part of the Feature section.
Description
A description describes the feature in a Cucumber feature file, which is highly recommended to use as it improves the understanding of the reader. In general, a feature injection template is used to write the description of the feature file in a single or multiple lines.
Scenario Section
A scenario is a collection of statements which illustrates business rule and they are compared with the test case of the Quality Assurance. A feature file can have multiple scenarios.
Below snippet is the live example of a scenario in a feature file.
Scenario: When a user login with correct username and password then user will be logged in to the Facebook. Given User opens the URL When User enters username and password Then User successfully logged in to Facebook
How to use Gherkin Syntax in Scenario of the Cucumber feature file?
We discussed that scenario is the replica of the QA test case and it is designed with the help of Gherkin syntax. Let’s discuss the uses of these Gherkin syntaxes:
Given
It defines the scene of the scenario. Given basically declares the pre-requisite or we can say it introduces us the application in a well-known state before starting the further test execution. You should avoid writing the user’s interaction with Given syntax.
When
It defines the event or action to be performed in a scenario. There should be only one When step in a scenario.
Then
It defines the outcome or result of the When step.
And
Whenever there is a repetition of Given syntax in a test case then this repetition can be overcome with replacing Given by And.
But
Whenever there is a repetition of Then syntax in a test case then this repetition can be overcome with replacing Then by But.
Below snippet shows the use of And & But syntax:
Scenario: This scenario shows the use of And & But syntax Given I am logged in Given I open Account summary page When I minimize account summary page Then I should not see full details Then Account summary page become sticky
Let’ rewrite the above scenario with And & But.
Scenario: This scenario shows the use of And & But syntax Given I am logged in And I open Account summary page When I minimize account summary page Then I should not see full details But Account summary page become sticky
How to write a scenario in Cucumber without using Gherkin syntax?
We replace Gherkin syntax by the asterisk (*) then the test steps are written without Gherkin syntax. Both the steps are valid but the steps which use Gherkin syntax is more clear and user-friendly.
Below scenario shows test steps without using Gherkin syntax.
Scenario: This scenario shows the use of And & But syntax *I am logged in *I open Account summary page *I minimize account summary page *I should not see full details *Account summary page become sticky
This is all about Cucumber feature file. You can post your queries in the comment below. Don’t forget to join our Automation Facebook group for quick updates.
Very Excellent article.
Good article!
Was looking for a way to organise Feature Files for an entire application. Some way that would help logically group various business scenarios. Is there a way to do this through Cucumber? Or is grouping feature files via folders/directories the only option?