What is Gherkin?
By Sandra Felice, Community Contributor - November 19, 2024
Gherkin is a simple, structured language used to write test scenarios in a way that’s easy for both technical and non-technical stakeholders to understand.
What is Gherkin?
Gherkin is a domain-specific language that defines test cases in a human-readable format, primarily for behavior-driven development (BDD). It allows stakeholders, including non-technical team members, to understand and contribute to the specifications of a software application.
Gherkin’s syntax is structured and uses simple, clear language to describe system behavior, making bridging the gap between technical and non-technical team members easier.
Gherkin scenarios are typically written in a Given-When-Then format, which describes the initial context, the action taken, and the expected outcome.
Gherkin Syntax
Gherkin follows a set of keywords that define the structure of a test case:
- Feature: Describes the overall functionality of a system or an application.
Example: Feature: User login - Scenario: Defines individual test cases that represent a single test.
Example: Scenario: Valid user login - Given: Sets the initial context or preconditions for the scenario.
Example: Given the user is on the login page - When: Describes an action performed by the user.
Example: When the user enters valid credentials - Then: Specifies the expected outcome or result of the action.
Example: Then the user is redirected to the dashboard - And/But: Used to add additional conditions to any of the above steps.
Example: And the welcome message is displayed
Using Gherkin for Selenium Automated Testing
Using Gherkin in combination with Selenium enhances the automation testing process by providing a clear and concise way to write test cases that can also serve as documentation.
Using tools like Cucumber, you can create a framework where Gherkin scenarios map directly to automated tests. This approach improves team members’ collaboration and ensures that the tests remain aligned with business requirements.
Below is an example of how the team can capture the login process using Gherkin.
Scenario: Login to an E-commerce Website
Feature: User Login
As a user of an e-commerce website, I want to log in to my account to view my order history.
Scenario: Successful login with valid credentials
Given the user is on the login page, When the user enters valid credentials, And clicks the login button, Then the user should be redirected to the dashboard.
Let’s translate the above Gherkin scenario into Selenium code, assuming we’re using Java with Selenium:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import io.cucumber.java.en.*; public class LoginSteps { WebDriver driver; @Given("the user is on the login page") public void user_is_on_login_page() { // Set up ChromeDriver and navigate to login page driver = new ChromeDriver(); driver.get("https://example-ecommerce.com/login"); } @When("the user enters valid credentials") public void user_enters_valid_credentials() { // Enter valid username and password driver.findElement(By.id("username")).sendKeys("testuser"); driver.findElement(By.id("password")).sendKeys("password123"); } @When("clicks the login button") public void user_clicks_login_button() { // Click on login button driver.findElement(By.id("loginButton")).click(); } @Then("the user should be redirected to the dashboard") public void user_redirected_to_dashboard() { // Validate that the user is on the dashboard String dashboardURL = driver.getCurrentUrl(); assert dashboardURL.equals("https://example-ecommerce.com/dashboard"); driver.quit(); } }
How it Works
- Feature file: The Gherkin scenario defines the login process flow in plain language.
- Step Definitions: The Gherkin steps are mapped to actual Selenium code, automating the test case.
- Execution: When you run this test using a framework like Cucumber, it executes the Selenium WebDriver code based on the Gherkin steps.
If you are a developer or a tester, with BrowserStack Automate, you can easily integrate Selenium with Gherkin for automation testing, enabling tests to double as documentation for stakeholders.
BrowserStack offers a real device cloud platform where you can access over 3500+ different device, browsers, and OS combinations.
Combining Selenium with Cucumber provides a low-maintenance, parallel-executable framework that can be set up in Java, streamlining web automation across real devices and browsers.