What are Gherkins Keywords in Cucumber

  

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

KeywordDescription
FeatureDefines the feature or functionality under test.
ScenarioDefines a single concrete example or test case.
Scenario OutlineAllows running the same scenario with different data sets.
ExamplesProvides the data table for Scenario Outline.
GivenDescribes the initial context or preconditions.
WhenDescribes the action or event (trigger).
ThenDescribes the expected outcome or result.
AndUsed to add more conditions to Given, When, or Then.
ButAdds a negative condition to a step.
BackgroundDefines 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 Background steps).

No comments:

Post a Comment