Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Gherkin keywords

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 929
    Comment on it

    Gherkin is a Business Readable language that Cucumber understands and describes the application flow. In cucumber each line should start with a Gherkin keyword. Gherkin provides the following keywords:

     

    Feature

    Scenario

    Scenario Outline

    Examples

    Background

    Given

    When

    Then

    And

     

    Feature: Each feature file should start with "Feature" keyword. This keyword basically defines the business logic that we are testing. Let suppose we have to test the login feature then there will be one feature file for the login functionality and this feature file may contain different scenarios in which we will test the login functionality for valid user, login functionality for invalid user and so on.

     

    Scenario: Scenario basically defines a business rule. As per the above defined feature, there will be different scenarios like Validate login error messages, validate login functionality for valid user, validate login functionality for invalid user, etc..

     

    Scenario Outline: We use Scenario Outline in place of Scenario when there is need to parameterize the testcase or any complex business logic. Suppose we have to test the login functionality for 3 users then there we have to write 3 different scenarios and 3 different step definitions too.

     

    Feature: Verify the Signin Functionality

     

     Scenario: Signin for User1

        Given I am at Home Page

        And I clicked on SignIn link

        And I enter “User1” as username

        And I enter “User1” as password

        And I clicked on SignIn button

        Then I should see the Activities page

     

     Scenario: Signin for User2

        Given I am at Home Page

        And I clicked on SignIn link

        And I enter “User2” as username

        And I enter “User2” as password

        And I clicked on SignIn button

        Then I should see the Activities page

     

     Scenario: Signin for User3

        Given I am at Home Page

        And I clicked on SignIn link

        And I enter “User3” as username

        And I enter “User3” as password

        And I clicked on SignIn button

        Then I should see the Activities page

       

    Since the business logic is same and only the username and password is changing, so we will use Scenario Outline instead of Scenario and pass the value in the Examples section.

     

     Scenario Outline: Signin for Users

        Given I am at Home Page

        And I clicked on SignIn link

        And I enter <Username> as username

        And I enter <Password> as password

        And I clicked on SignIn button

        Then I should see the Activities page

     

        Examples:

         | Username | Password |

         | User1        | User1       |

         | User2        | User2       |

         | User3        | User3       |

     

    Examples: Examples is basically a section that contains the data table. The first row in the table must contain a header corresponding to the variables used in the Scenario Outline steps. Examples section is a mandatory part for Scenario Outline ie whenever we are using Scenario Outline in our feature file then we have to use Examples section in the last. Refer the above example.

     

    Background: In the Scenario Outline example we have seen that we have given the pre-condition(given after ‘Given’ keyword) three times. Since this step is repeated in every scenario, so we can use Background keyword and put the “Given” keyword step inside Background. Now, we do not have give the pre-condition step with each scenario. Step mentioned in the Background section gets executed before every scenario.

     

    Background:

        Given I am at Home Page

     

    Given: This keyword is used to define the pre-condition of any scenario. When cucumber executes the Given logic then it is basically creating a platform for the next step to execute.

     

    When: When steps are used to describe an action that user will perform. We should use only single When step for each scenario.

     

    Then: Then steps are used for the assertion purpose. In When steps we have triggered an action, so some output or result is displayed. Now we verify that result in Then step.

     

    And: And steps are basically used to concatenate Given, When, Then steps.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: