What is Gherkin?
Gherkin is a Business Readable, Domain-Specific Language (DSL) that allows you to describe software behavior in a structured format.
Example: Understanding Business Expectations
A requirement states:
📌 A pop-up message should be displayed when the button is clicked, and errors should disappear.
-
Developer's Assumption: A pop-up message is displayed, and errors are gone when the button is clicked.
-
QA's Assumption: The pop-up message is displayed only when the button is clicked, and errors disappear.
This difference in interpretation is why Gherkin is used—to eliminate ambiguity and ensure all stakeholders have a common understanding.
Keywords Used in Cucumber
In Cucumber, test cases are represented as Scenarios inside a Feature File.
Gherkin Syntax (Keywords):
1️⃣ Feature: Describes the functionality under test.
2️⃣ Scenario: Represents a test case.
3️⃣ Scenario Outline: Used for data-driven testing.
4️⃣ Step Definitions: Defines how each step executes in code.
5️⃣ Steps: The core of Gherkin syntax, including:
-
Given → Defines preconditions.
-
When → Describes the user action.
-
Then → Verifies the expected result.
-
And / But → Used for additional conditions.
Example Gherkin Scenario
Scenario: Make a Minimum Due Payment
Feature: Credit Card PaymentScenario: Make a minimum due payment Given the user is on the credit card payment page When the user fills in all details and selects the "Minimum Amount" option And the user clicks on the "Pay" button Then the credit card confirmation page should be displayed
Converting Business Requirements to Cucumber Test Cases
A Business Analyst (BA) provides a requirement in plain text:
A user should be able to make a credit card payment by selecting the minimum due amount option.
Test Automation in Selenium
-
If automating without Cucumber, you would write Selenium test scripts directly in Java/Python.
Test Automation in Cucumber
-
If using Cucumber, the test must be converted into Gherkin syntax (as shown in the scenario above).
-
Step definitions are implemented in a programming language (e.g., Java, Python).
Feature: Ebay page test scenarios
Scenario: Advanced Search Link Test
Given I am in Ebay Homepage
When I click on Advanced Search Link
Then I navigate to Advanced Search page
Feature: Ebay page test scenarios
Scenario: Advanced Search Link Test
Given I am in Ebay Homepage
When I click on Advanced Search Link
Then I navigate to Advanced Search page
==========================================================
Scenario Outline (Example with Multiple Data Sets)
If we need to test different types of credit card payments, we use Scenario Outline:
Feature: Credit Card Payment
Scenario Outline: Make a payment with different options
Given the user is on the credit card payment page
When the user fills in all details and selects the "<Payment Option>"
And the user clicks on the "Pay" button
Then the credit card confirmation page should be displayed
Examples: | Payment Option | | Minimum Amount | | Full Amount | | Custom Amount |
This helps in data-driven testing by covering multiple scenarios with one test case.