Gherkin is the language used in Cucumber feature files. It is not a programming language — it is a structured way to write test scenarios in English. Feature files have the .feature extension and live in your project under src/test/resources/features.
| Keyword | What It Does | Think of It As |
|---|---|---|
| Feature | Names the feature being tested | The chapter title |
| Scenario | One specific test case | One test method |
| Given | The starting state / precondition | "I am on the login page" |
| When | The action the user performs | "I click the login button" |
| Then | The expected result | "I should see the dashboard" |
| And | Additional step of the same type | More Given, When, or Then steps |
| But | Negative additional step | "But the error should NOT appear" |
| Background | Steps shared by ALL scenarios in this file | Like @BeforeMethod in TestNG |
| Scenario Outline | Same test with different data | Like TestNG @DataProvider |
| Examples | Data table for Scenario Outline | The rows of test data |
| @tag | Label for organizing scenarios | Like TestNG groups |
Feature: Banking Portal Login
As a bank customer
I want to log in to the banking portal
So that I can access my account
Scenario: Successful login with valid credentials
Given the user is on the Banking Portal login page
When the user enters username "testuser"
And the user enters password "password123"
And the user clicks the login button
Then the user should be redirected to the dashboard
And the welcome message should be displayedThe three lines after Feature (As a... I want... So that...) are called the "user story." They are optional comments that describe who, what, and why. Cucumber ignores them, but they help anyone reading the file understand the purpose.
If every scenario in a file starts with the same Given steps, move them to a Background block. It runs before each scenario — just like @BeforeMethod in TestNG.
Feature: Banking Portal Login
Background:
Given the user is on the Banking Portal login page
Scenario: Successful login
When the user enters username "testuser"
And the user enters password "password123"
And the user clicks the login button
Then the user should be redirected to the dashboard
Scenario: Login with invalid password
When the user enters username "testuser"
And the user enters password "wrongpassword"
And the user clicks the login button
Then an error message "Invalid credentials" should be displayed
Scenario: Login with empty username
When the user enters password "password123"
And the user clicks the login button
Then an error message "Username is required" should be displayedScenario Outline lets you run the same scenario with different data sets. Use angle brackets for placeholders, and provide the data in an Examples table. Each row creates a separate test run. This is the Cucumber equivalent of TestNG DataProvider.
Feature: Banking Portal Login
Scenario Outline: Login with multiple credentials
Given the user is on the Banking Portal login page
When the user enters username "<username>"
And the user enters password "<password>"
And the user clicks the login button
Then the login result should be "<result>"
Examples:
| username | password | result |
| testuser | password123 | success |
| admin | admin123 | success |
| invalid | wrong | failure |
| | password123 | failure |
| testuser | | failure |This single Scenario Outline generates 5 separate tests — one per row in the Examples table. In the report, each row shows as a separate test with pass/fail status.
You can have multiple Examples tables in one Scenario Outline with descriptive labels. Use "Examples: Valid Credentials" and "Examples: Invalid Credentials" to group your data logically. This makes the feature file much more readable.
Every Scenario must have at least one step (Given, When, or Then). A Scenario with no steps will cause a Cucumber error. Also, keep your step text short and action-focused — "the user enters username" is better than "the user types their username into the username input field on the page."
Q: What is Gherkin? What keywords does it use?
A: Gherkin is the language for writing Cucumber feature files. It uses keywords: Feature (names the test feature), Scenario (one test case), Given (precondition), When (user action), Then (expected result), And/But (additional steps), Background (shared setup steps for all scenarios), Scenario Outline (template for data-driven testing), Examples (data table for Scenario Outline), and @tags (labels for organizing and filtering). Feature files have the .feature extension and are designed to be readable by non-technical stakeholders.
Key Point: Gherkin uses Given-When-Then to describe behavior. Background shares setup. Scenario Outline enables data-driven testing.