Gherkin Language: Keywords and Syntax Explained


cucumber gherkin language,gherkin,gherkin cucumber test,writing gherkin,cucumber gherkin,cucumber gherkin test automation,what is gherkin,when to use bdd,cucumber using testng,cucumber with gherkin,how to write gherkin scripts,what is feature file,cucumber testng,cucumber runner using testng,cucumber testing,behavior driven development,how to write a test in ghrkin,cucumber with selenium,software testing,automation testing,cucumber tutorials


Introduction of Gherkin Language


  • Gherkin is a language that is used to write scenarios in Cucumber. It is designed to be easy to read, understand and uses a simple syntax that can be used by anyone, regardless of their technical background.
  • The Gherkin language has a set of predefined keywords that are used to write feature files in a structured and readable format. 


Here are the keywords in Gherkin:


  • Feature: Describes a high-level business requirement or feature of the application.
  • Background: Defines steps that are common to all scenarios within a feature file.
  • Scenario: Describes a single test scenario or use case that represents a particular behavior of the application.
  • Given: Describes the initial context or preconditions required for the scenario to start.
  • When: Describes the action or event that triggers the scenario.
  • Then:  Describes the expected outcome or result of the scenario.
  • And: Used to add additional Given, When, or Then steps to the scenario.
  • But: Used to add an exception to a previous Given, When, or Then step.

These keywords provide a standard way of writing feature files that is easily understood by all members of the team, including business stakeholders, developers, and testers. By following this structure and using these keywords consistently, teams can create executable specifications that ensure the application meets the desired behavior.


Gherkin Scenarios consist of three parts: the Feature, the Scenario, and the Steps. 


Here's a breakdown of each part:


1. Feature: A Feature is a high-level description of a functionality or a requirement of the application. It is used to group related scenarios together. 


Here's an example:


Feature: Login Functionality
 
  As a user
 
  I want to be able to log in to the application
 
  So that I can access my account



2. Scenario: A Scenario is a specific example of how the application should behave. It is written using Gherkin syntax and should be focused on a single behavior or requirement of the application. 


Here's an example:


Scenario: Successful login with valid credentials
 
  Given I am on the login page
 
  When I enter valid username and password
 
  And I click on the login button
 
  Then I should be redirected to the home page



3. Steps: Steps are the specific actions that are performed as part of a scenario. They are written using Gherkin syntax and should be easily understood by both technical and non-technical team members. 


Here's an example:


Given I am on the login page


This step specifies the initial state of the scenario, which is that the user is on the login page.



When I enter valid username and password


This step specifies an action that the user should perform, which is to enter a valid username and password.



And I click on the login button


This step specifies another action that the user should perform, which is to click on the login button.



Then I should be redirected to the home page


This step specifies the expected outcome of the scenario, which is that the user should be redirected to the home page.



When writing Gherkin scenarios, it's important to keep them focused on a single behavior or requirement of the application. This helps to make them easier to understand and maintain. 

It's also important to use clear and concise language that can be easily understood by all team members. By following these best practices, teams can create effective Gherkin scenarios that are easy to read, understand, and maintain.

Post a Comment

Post a Comment (0)

Previous Post Next Post