Gherkin Keywords in Cucumber
Gherkin is the language used to write Cucumber feature files. It follows a structured syntax using keywords that make it easy for both technical and non-technical stakeholders to understand test scenarios in Behavior-Driven Development (BDD).
What is Gherkin?
Gherkin is a domain-specific language (DSL) for writing human-readable tests.
Each line in a Gherkin file begins with a keyword, followed by natural language describing the behavior.
File extension:
.feature
Gherkin Keywords List
| Keyword | Description |
|---|---|
| Feature | Defines the feature or functionality under test. |
| Scenario | Defines a single concrete example or test case. |
| Scenario Outline | Allows running the same scenario with different data sets. |
| Examples | Provides the data table for Scenario Outline. |
| Given | Describes the initial context or preconditions. |
| When | Describes the action or event (trigger). |
| Then | Describes the expected outcome or result. |
| And | Used to add more conditions to Given, When, or Then. |
| But | Adds a negative condition to a step. |
| Background | Defines common steps shared by all scenarios in a feature file. |
| * | Wildcard, can replace Given, When, Then, etc., to improve readability. |
Example of Gherkin Feature File
Feature: Login functionality Background: Given the user is on the login page Scenario: Successful login When the user enters valid credentials Then the user should be redirected to the dashboard Scenario: Unsuccessful login When the user enters invalid credentials Then an error message should be displayed
Scenario Outline Example with Examples Table
Scenario Outline: Login with multiple credentials Given the user is on the login page When the user enters username "<username>" and password "<password>" Then the login result should be "<result>" Examples: | username | password | result | | user1 | pass1 | success | | user2 | wrong | error message |
Important Points:
Gherkin supports multiple languages (like French, Hindi, etc.).
Keywords must be followed by plain language sentences.
Each scenario is independent (except for
Backgroundsteps).