Book Image

Cucumber Cookbook

By : Shankar Garg
Book Image

Cucumber Cookbook

By: Shankar Garg

Overview of this book

Table of Contents (13 chapters)
Cucumber Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing Scenario Outlines


In the previous recipe, we learnt how we can send test data in Steps itself, which can be used by the application for processing. Until now, the data was associated with one particular Step (implemented by Data Tables); but what if I want to send data which is related to the whole Scenario, and what if I want to repeat all the Steps of a Scenario again and again for different sets of data? This is a classic case of data-driven testing. This will be implemented by using a Scenario Outline.

Getting ready

Let's create a Scenario for a login functionality where we want to test all the possible Scenarios where the login will fail. Based on what we have learned so far, this is how our Scenario will look:

  Scenario: login fail - wrong username
    Given user is on Application landing page
    When user clicks on Sign in button
    Then user is displayed login screen
    When user enters "wrongusername" in username field
    And user enters "123456" in password field
    And user clicks Sign in button
    Then user gets login failed error message

  Scenario: login fail - wrong password
    Given user is on Application landing page
    When user clicks on Sign in button
    Then user is displayed login screen
    When user enters "ShankarGarg" in username field
    And user enters "wrongpassword" in password field
    And user clicks Sign in button
    Then user gets login failed error message

In terms of syntax, there is no problem in this code. Cucumber will treat it as well as any other, but the problem is for the person writing the Feature file. If you look closely, only the dataset is changing and all the other Steps are the same. These are the following problems with this approach to creating Feature files:

  • Copying and pasting Scenarios to use different values can quickly become tedious and repetitive.

  • If tomorrow only one Step changes, it has to be changed in all the Scenarios. So, maintainability and reusability are big issues.

To avoid these problems, let's look at the next section and understand how we can solve them.

How to do it…

Here, we are going to use the Scenario Outline keyword and add one Scenario Outline to test possible login Scenarios:

Scenario Outline: Login fail - possible combinations
    Given user is on Application landing page
    When user clicks on Sign in button
    Then user is displayed login screen
    When user enters "<UserName>" in username field
    And user enters "<Password>" in password field
    And user clicks Sign in button
    Then user gets login failed error message

    Examples: 
      | UserName      | Password      |
      | wrongusername | 123456        |
      | ShankarGarg   | wrongpassword |
      | wrongusername | wrongpassword |

How it works…

Here we have used the Scenario Outline keyword and we have merged all three Scenarios in to one Scenario Outline. One advantage of the Scenario Outline is that our Feature file is now compact and expressive. Let's understand Scenario Outline in more detail:

  • Scenario Outline allow us to send test data to Scenarios through the use of a template with placeholders.

  • A Scenario Outline is run once for each row in the Examples section beneath it (not counting the first row of column headers).

  • A Scenario Outline is a template that is never directly run. It uses placeholders, which are contained within < > in the Scenario Outline's Steps.

  • Think of a placeholder like a variable. It is replaced with a real value from the Examples table row, where the text between the placeholder's angle brackets matches that of the table column header.

    • In the first execution, when Cucumber encounters the first Step with placeholders, which is When user enters <UserName> in username field in our case, Cucumber looks for a column with the header UserName in the Examples table.

    • If there is no column with UserName in the Examples table, then Cucumber does not give an error but instead considers <UserName> as a String and passes it to Step Definition as it is.

    • If Cucumber finds a column with the header UserName, then it picks the first row data from this column and replaces UserName with that value, which is wrongusername in our case, and sends this value to Step Definition.

    • Cucumber repeats this process for all < > for one round of execution.

    • So, for the first execution, this is how our Scenario Outline will look:

      Given user is on Application landing page
      When user clicks on Sign in button
      Then user is displayed login screen
      When user enters "wrongusername" in username field
      And user enters "123456" in password field
      And user clicks Sign in button
      Then user gets login failed error message
  • The value substituted for the placeholder changes with each subsequent run of the Scenario Outline. The values from the second row are taken for the second execution and so on, until the end of the Examples table is reached.

  • The Scenario Outline itself is useless without an Examples table, which Lists the rows of values to be substituted for each placeholder.

    Note

    Now that you have leaned the concept of Scenario Outline, try implementing the following:

    • Write a Scenario Outline with multiple arguments in one Step.

    • Can you create a Scenario Outline with multiple examples? You can group examples of positive tests and negative tests in different tables.