Gherkin Domain Specific Language (DSL)

This section will cover details about Gherkin, it’s keywords and how to use them in feature files.

What is Gherkin Language?

Gherkin is a line-oriented language, like as Python and YAML, It uses indentation to define structure. Line endings terminate statements, Either spaces or tabs may be use for indentation. Most lines start with a keyword.

Conventions of Gherkin Language

  1. Single Gherkin source file contains a description of a single feature.

  2. Source files will have .feature extension.

Gherkin Keywords

  1. Feature

  2. Example

  3. Rule (as of Gherkin 6)

  4. Scenario or Example

  5. Given

  6. When

  7. Then

  8. And

  9. But

  10. Background

  11. Scenario Outline or Scenario Template

  12. Examples

Others keywords

  1. | (Data Tables)

  2. @ (Tags)

  3. # (Comments)

Example of Gherkin feature file

Feature: Here testing all keyword which we used in our feature File


  Background:
    Given cleaning the database
    And initializing the database

  Scenario: This is basic scenario
    Given put input here
    When the input processing
    Then i got the result

  Scenario Outline: Login functionality
    Given going to the website
    And click on login button
    When user logs in using Username as <username> and Password as <password>
    Then login should be successful
    But the dashboard not found

    Examples:
      |username  |password|
      |Tom       |password1|
      |Harry     |password2|
      |Jerry     |password3|

Gherkin Keywords description.

Above we listed all keywords which are using in Gherkin feature files. Now lets details about them.

Feature

Feature keyword gives information about the high level business functionality and the purpose of activities under this test. Everybody should be able to understand the intent of feature file by reading the first Feature step. This part basically kept brief.

Scenario or Example

Basically a scenario represents a particular functionality which is under test. By seeing the scenario user should be able to understand the intent behind the scenario and what the test is all about. Each scenario should follow given, when and then format.

  1. Background : Whenever any action/task required to perform in each scenario, those things needs to be placed in Background. For Instance: If user needs to clear database before each scenario then database clear task/action can be put in background.

  2. Given : As mentioned above, given specifies the pre-conditions. It is basically a known state.

  3. When : This is used when some action is to be performed. As in above example we have seen when the user tries to log in using username and password, it becomes an action.

  4. Then : The expected outcome or result should be placed here. For Instance: verify the login is successful, successful page navigation.

  5. And : And is used to combine two or more same type of action. We can use it instead of Given.

  6. But : We can use it instead of Then.

Scenario Outline or Scenario Template

Scenario outlines are using when same test has to be performed with different data set. Such as we have to test login functionality with multiple different set of username and password.

Examples

Examples is data set of Scenario Outline.