Cucumber (Gherkin)

Writing Your First Gherkin Feature File

Estimated reading: 3 minutes 167 views
cucumber-guides-featured-image

Now that you have Cucumber set up in your project, it’s time to write your first Gherkin feature file. This guide will help you create a feature file and define scenarios using Gherkin syntax.

Gherkin feature files are written with the .feature extension and contain a structured language with keywords like Feature, Scenario, Given, When, Then, And, and But. Let’s write a simple feature file for a checking the navigation on the Kicks Web App functionality.

kicks-app-flutter-web
Kicks App | Flutter Web

Creating a Feature File

  • First, navigate to your project’sfeaturesdirectory. This is where all your feature files will reside. To get started with our scenarios, we need to create a new file.
  • Create a file named home-screen.feature inside the features directory.
cucumber-creating-home-screen-feature-file
Creating The Home Screen Feature File
				
					src/test/resources/features/home-screen.feature

				
			

By creating the home-screen.feature file, we establish a dedicated space for our home screen scenarios. This approach not only helps in organizing our tests but also makes it easier to locate and update them as needed.

Write a Feature Scenario 

Ensuring a seamless user experience starts with verifying that all navigation links on the home screen function as expected. Below we are going test a few test cases related to the navigation menu.

				
					# Author: J Richard | Mastering QA
Feature: Home Screen - Verify Web App Navigation Menu Links

  Scenario: Click the Login Icon
    Given the user is on the home screen
    When the user clicks the "login" icon
    Then the "Login" screen is displayed

  Scenario: Click the Products link
    Given the user is on the home screen
    When the user clicks the "Products" link
    Then the "Products" screen is displayed
				
			
home-screen-feature-scenarios
Home Screen Feature File Scenarios

Understanding Gherkin Syntax

  • Feature: Describes the feature under test.
  • Scenario: Describes a specific test case for the feature.
  • Given: Sets up the initial state.
  • When: Describes the action taken by the user.
  • Then: Describes the expected outcome.
  1. Given the user is on the home screen

    • This step sets the initial context. It assumes the user has already landed on the home screen of the web application.
  2. When the user clicks the “login” icon

    • This action step simulates the user clicking on the login icon, which is expected to take them to the login screen.
  3. Then the “Login” screen is displayed

    • The final step verifies the outcome, ensuring that clicking the login icon indeed navigates the user to the login screen.
Click the Products link Test

The second scenario is pretty much similar but in this case we will be clicking the products link and verifying that we landed there successfully.

Conclusion

You’ve written your first Gherkin feature file. Next, you’ll learn how to implement step definitions to make these scenarios executable. Continue to the next guide to explore step definitions.

Leave a Comment

Share this Doc

Writing Your First Gherkin Feature File

Or copy link

CONTENTS
Review Your Cart
0
Add Coupon Code
Subtotal
Total Installment Payments
Bundle Discount