Next-Gen App & Browser
Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles
Learn how to perform Gherkin testing with this comprehensive guide. Master syntax, scenarios, and best practices for clear, behavior-driven testing.
Published on: September 3, 2025
Gherkin testing is the process of writing and executing tests using the Gherkin language, which is a plain-text, structured format for describing software behavior. It is most commonly used with Behavior-Driven Development (BDD) frameworks like Cucumber or Behave.
In testing, Gherkin is a simple, structured language used to write test cases in a way that’s easy for both technical and non-technical people to understand.
Syntax of Gherkin File
Feature: User login
Scenario: Successful login with valid credentials
Given the user is on the login page
When the user enters valid credentials
Then the user should be redirected to the dashboard
Core Keywords and Their Applications
Gherkin is a plain-text language used to describe software behaviors in a way that’s both human-readable and structured enough for automated testing tools. It’s most commonly associated with frameworks like Cucumber for Behavior-Driven Development.
The main idea is to bridge communication between technical and non-technical team members. Instead of writing complex test scripts, you write simple scenarios that outline what the system should do in specific situations.
A Gherkin file (usually ending in .feature) is organized into features, scenarios, and steps.
Example:
Feature: User login
Scenario: Successful login with valid credentials
Given the user is on the login page
When the user enters valid credentials
Then the user should be redirected to the dashboard
Gherkin uses specific keywords to define the role of each line. These keywords can be localized into different languages.
For repetitive scenarios with different inputs, you use Scenario Outline and Examples.
Scenario Outline: Login attempts
Given that the user is on the login page
When the user enters "<username>" and "<password>"
Then the system displays "<message>"
Examples:
| username | password | message |
| alice | pass123 | Welcome, alice! |
| bob | wrong | Invalid credentials|
Use # at the start of a line for comments. They are ignored during execution.
# This is a comment about the scenario
Indentation is optional but recommended for readability. Consistent spacing makes large feature files easier to scan.
A common approach is to indent steps under their corresponding Scenario or Background so the structure is clear at a glance. For example, keeping all Given, When, and Then statements aligned under a scenario title helps distinguish them from higher-level keywords like Feature or Scenario Outline.
Note: Run your Gherkin tests across 3000+ desktop browsers. Try LambdaTest Now!
In Gherkin, the language used to write BDD scenarios, keywords structure the specification so that it is easy to read, consistent, and executable by automation testing tools like Cucumber.
Here are the core Gherkin keywords and how they are applied:
Example:
Feature: User login
Users should be able to log in with valid credentials
Example:
Scenario: Successful login with valid credentials
Example:
Given the user is on the login page
Example:
When the user enters valid credentials
Example:
Then the user should see the dashboard
Example:
And the welcome message is displayed
But the user does not see admin options
Example:
Background:
Given the user is logged in
Example:
Scenario Outline: Login with multiple user roles
Given the user is on the login page
When the user logs in with <username> and <password>
Then the <role> dashboard is displayed
Examples:
| username | password | role |
| alice | pass123 | admin |
| bob | pass456 | member |
Beyond the core keywords, Gherkin has a few advanced constructs that help you make scenarios more expressive, reusable, and maintainable when working on larger test suites.
Here are the main ones and how they are used.
Example:
@smoke @login
Scenario: Successful login with valid credentials
Given the user is on the login page
When the user enters valid credentials
Then the user should see the dashboard
Example:
When I send the following payload:
"""
{
"username": "alice",
"password": "pass123"
}
"""
Example:
Given the following users exist:
| name | role | active |
| Alice | admin | true |
| Bob | member | false |
Example:
Rule: Only active members can log in
Scenario: Active user logs in successfully
Scenario: Inactive user is denied access
Example:
When the user logs in with username "alice" and password "pass123"
Let’s look at the simple Gherkin test written using Selenium and Cucumber that validates the login functionality of LambdaTest eCommerce Playground.
Here is the Gherkin feature file:
Feature: User Login
Scenario: Login with valid credentials
Given the user is on the login page
When the user enters valid credentials
And clicks on the login button
Then the user should be logged in successfully
Below is the test script for the step definitions written using Selenium Cucumber:
package steps;
import io.cucumber.java.en.*;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
public class LoginSteps {
WebDriver driver;
@Given("the user is on the login page")
public void the_user_is_on_the_login_page() throws MalformedURLException {
ChromeOptions options = new ChromeOptions();
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=account/login");
}
@When("the user enters valid credentials")
public void the_user_enters_valid_credentials() {
driver.findElement(By.id("input-email")).sendKeys("testuser@example.com");
driver.findElement(By.id("input-password")).sendKeys("Password123");
}
@When("clicks on the login button")
public void clicks_on_the_login_button() {
driver.findElement(By.cssSelector("input[value='Login']")).click();
}
@Then("the user should be logged in successfully")
public void the_user_should_be_logged_in_successfully() {
String heading = driver.findElement(By.cssSelector("h2")).getText();
if (!heading.contains("My Account")) {
throw new AssertionError("Login failed. Expected My Account page.");
}
driver.quit();
}
}
Code Walkthrough:
Here is how each step in the Gherkin feature file is implemented in the Selenium script during Cucumber testing:
For more information, check this guide on Selenium testing using Gherkin.
Cloud testing platforms such as LambdaTest provides an online Selenium Grid that lets you run Gherkin-based BDD tests at scale across 3000+ real browsers and devices.
With LambdaTest, you can perform Selenium test automation using Cucumber, helping teams validate user stories in plain English while ensuring cross-browser compatibility. This reduces hassles of setting up test infrastructure and accelerates feedback in Agile workflows.
To perform Gherkin testing on LambdaTest, you only need to point your WebDriver to LambdaTest remote grid and configure the automation capabilities. You can generate capabilities from the LambdaTest Automation Capabilities Generator.
ChromeOptions browserOptions = new ChromeOptions();
browserOptions.setPlatformName("Windows 10");
browserOptions.setBrowserVersion("dev");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("username", "Your LambdaTest Username");
ltOptions.put("accessKey", "Your LambdaTest Access Key");
ltOptions.put("project", "Gherkin Testing With Selenium");
ltOptions.put("w3c", true);
ltOptions.put("plugin", "java-testNG");
browserOptions.setCapability("LT:Options", ltOptions);
Then, replace your LT_USERNAME and LT_ACCESS_KEY with actual values (or set them as environment variables).
Your .feature files and Gherkin scenarios remain unchanged, since LambdaTest only affects the execution environment, not the test logic.
To get started, refer to this guide on Selenium Cucumber testing on LambdaTest.
Here are some of the best practices for writing effective Gherkin scenarios that are both readable and maintainable.
If you are using Cucumber, you can follow these Cucumber best practices to write effective Gherkin scenarios.
Here are the common challenges teams face with Gherkin testing, along with practical solutions to address them.
Solution: Use domain language that reflects business processes. Agree on a shared vocabulary with stakeholders and use it consistently across all scenarios.
Solution: Break them into smaller, focused scenarios. One scenario should test one behavior. Use Scenario Outline for variations instead of cramming everything into one.
Solution: Keep backgrounds minimal and only for truly shared, stable setup steps. Any scenario-specific setup should remain in that scenario.
Solution: Write steps at a higher level of abstraction, describing actions in business terms. Let automation code handle the translation to UI interactions.
Solution: Maintain a shared step library and review new steps for reusability before adding them. This keeps the step set small and easier to maintain.
Solution: Be precise about the observable result while keeping the business perspective. For example, “Then the order appears in the pending orders list.”
Solution: Use Gherkin for high-value, business-facing behaviors. Keep unit and integration tests in code where they belong.
Solution: Ensure test data is reset between runs and use stable, dedicated environments for acceptance testing.
Solution: Schedule regular scenario reviews. Treat Gherkin scripts as living documentation, not write-once artifacts.
Gherkin provides a clear and structured way to describe software behavior in plain language while keeping scenarios directly tied to automated tests. By understanding its syntax, keywords, and advanced constructs, teams can write feature files that are both readable and executable. When combined with Selenium, Gherkin becomes a powerful bridge between business requirements and technical implementation, ensuring test coverage aligns with real-world use cases.
Following best practices helps maintain consistency and scalability, while awareness of common challenges prepares teams to resolve issues before they become blockers. In the end, effective use of Gherkin testing fosters better collaboration, improves test automation efficiency, and enhances overall software quality.
On This Page
Did you find this page helpful?