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.
Creating a Feature File
- First, navigate to your project’s
features
directory. 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 thefeatures
directory.
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
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.
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.
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.
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.