Cucumber Tutorials

 HOME

Cucumber Introduction, Installation, and Configuration

Chapter 1  Introduction of Cucumber Testing Tool (BDD Tool)
Chapter 2 How to install Cucumber Eclipse Plugin
Chapter 3 How to setup Cucumber with Eclipse
Chapter 4 Cucumber – What is Gherkin

Cucumber Scenario, Features & Step Definition

Chapter 1 Cucumber – What is Feature File in Cucumber
Chapter 2 Step Definition in Cucumber
Chapter 3 Cucumber – JUnit Test Runner Class

Cucumber – Hooks & Tags

Chapter 1 Hooks in Cucumber
Chapter 2 Tags in Cucumber
Chapter 3 Conditional Hooks in Cucumber
Chapter 4 What is CucumberOptions in Cucumber?
Chapter 5 Background in Cucumber
Chapter 6 Monochrome in Cucumber
Chapter 7 What is Glue in Cucumber?

Cucumber – Data Driven Testing

Chapter 1 Data Driven Testing using Scenario Outline in Cucumber
Chapter 2 DataTables in Cucumber

Cucumber Integration with Selenium – Maven

Chapter 1 Integration of Cucumber with Selenium and JUnit4
Chapter 2 Integration of Cucumber with Selenium and TestNG
Chapter 3 Page Object Model with Selenium, Cucumber and JUnit
Chapter 4 Page Object Model with Selenium, Cucumber, and TestNG
Chapter 5 Integration of Cucumber7 with Selenium and JUnit5
Chapter 6 Run Cucumber7 with JUnit5 Tests from Maven Command Line
Chapter 7 How to rerun failed tests in Cucumber
Chapter 8 How to create Cucumber Report after rerun of failed tests – NEW
Chapter 9 How to rerun failed tests twice in Cucumber – NEW

Cucumber – Command Line Execution

Chapter 1 Run Cucumber Test from Command Line
Chapter 2 Run Gradle Cucumber Tests from Command Line

Cucumber Integration with Rest API

Chapter 1 Rest API Test in Cucumber BDD
Chapter 2 How To Create Gradle Project with Cucumber to test Rest API

Cucumber Integration with SpringBoot

Chapter 1 Integration Testing of Springboot with Cucumber and JUnit4
Chapter 2 Integration Testing of Springboot with Cucumber and TestNG

Cucumber – Reporting

Chapter 1 Cucumber Tutorial – Cucumber Reports
Chapter 2 Cucumber Report Service
Chapter 3 Implemention of ‘Masterthought’ Reports in Cucumber
Chapter 4 Implemention of ‘Masterthought’ Reports in Cucumber with JUnit4

Cucumber Integration with Allure Reports

Chapter 1 Allure Report with Cucumber5, Selenium and JUnit4
Chapter 2 Allure Report with Cucumber5, Selenium and TestNG
Chapter 3 Integration of Allure Report with Rest Assured and JUnit4
Chapter 4 Integration of Allure Report with Rest Assured and TestNG
Chapter 5 Gradle – Allure Report for Selenium and TestNG

Cucumber Integration with Extent Reports

Chapter 1 ExtentReports Version 5 for Cucumber 6 and TestNG
Chapter 2 How to add Screenshot to Cucumber ExtentReports
Chapter 3 ExtentReports Version 5 for Cucumber 6 and JUnit4
Chapter 4 PDF ExtentReport for Cucumber and TestNG
Chapter 5 ExtentReports Version 5 for Cucumber 7 and TestNG
Chapter 6 Extent Reports Version 5 for Cucumber7 and JUnit5

Cucumber – Parallel Execution

Chapter 1 Parallel Testing in Cucumber with JUnit
Chapter 2 Parallel Testing in Cucumber with TestNG
Chapter 3 Dependency Injection in Cucumber using Pico-Container

Data Driven Testing using Scenario Outline in Cucumber

HOME

In the previous tutorial, I explained the various types of Reports available in Cucumber. In this tutorial, I will explain how we can do Data-Driven Testing in Cucumber. Cucumber inherently supports Data-Driven testing by the use of the Scenario Outline and Examples section.  Using these keywords, Cucumber allows for easy Data-Driven testing to be complete where no changes need to be made to the Java file (StepDefinition file).

Pre-Requisite

  1. Cucumber – 6.10.4
  2. Java – 11
  3. Selenium – 3.141.59
  4. Junit – 4.13.2 ( You can use TestNG also)
  5. Cucumber JUnit – 6.10.4 (If using TestNG, then replace this with Cucumber TestNG)

The project folder structure and code should be in the below state.

In case, the project uses Maven, we need to add the below dependencies to the project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>CucumberDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <cucumber.version>6.10.4</cucumber.version>
        <selenium.version>3.141.59</selenium.version>
        <junit.version>4.13.2</junit.version>
        <maven.compiler.source.version>11</maven.compiler.source.version>
        <maven.compiler.target.version>11</maven.compiler.target.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${maven.compiler.source.version}</source>
                    <target>${maven.compiler.target.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>


Below is an example of Scenarios with a different set of data (no Scenario Outline).

  @InvalidCredentials1
  Scenario: Login with invalid username and password

    Given User is on Home page
    When User enters username as "Admin1" and password as "admin1"
    Then User should be able to see an "Invalid credentials"

  @InvalidCredentials2
  Scenario: Login with blank username

    Given User is on Home page
    When User enters username as "" and password as "admin123"
    Then User should be able to see an "Username cannot be empty"

  @InvalidCredentials3
  Scenario: Login with invalid credentials

    Given User is on Home page
    When User enters username as "Admin" and password as ""
    Then User should be able to see an "Password cannot be empty"

The stepdefinition corresponding to the above feature file is

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScenarioOutlineDefinitions {

    WebDriver driver;

    @Given("User is on Home page")
    public void userOnHomePage() {

        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\Vibha\\Software\\chromedriver_win32\\chromedriver.exe");

        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://opensource-demo.orangehrmlive.com/");
    }

    @When("User enters username as {string} and password as {string}")
    public void entersCredentials(String userName, String passWord) throws InterruptedException {

        System.out.println("Username Entered");
        driver.findElement(By.name("txtUsername")).sendKeys(userName);

        System.out.println("Password Entered");
        driver.findElement(By.name("txtPassword")).sendKeys(passWord);

        driver.findElement(By.id("btnLogin")).submit();

    }

    @Then("User should be able to see an {string}")
    public void verifyErrorMessage(String expectedErrorMessage) throws InterruptedException {

        String actualErrorMessage = driver.findElement(By.id("spanMessage")).getText();
        System.out.println("Error Message :" + actualErrorMessage);
        Assert.assertEquals(actualErrorMessage,expectedErrorMessage);

        //close the browser
        driver.quit();

    }
}

TestRunner Class

 Run the test by Right Click on TestRunner class and Click Run As  > JUnit Test Application.

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(monochrome = true, plugin = "pretty", features = "src/test/resources/Features/ScenarioOutline.feature",
        glue = "definitions")

public class CucumberRunnerTest {
}

Below is the output of the above program:

Data Driven Testing using Scenario Outline

Scenario Outline – It is used to repeat the same tests by passing different values or arguments to Step Definition

Examples – All scenario outlines have to be followed with the Examples section. This contains the data that has to be passed on to the scenario in Feature File.

We can repeatedly do something like the above and check for each parameter that we want to test, but that will make tests hard to maintain with many repetitions. Instead, we can use a Scenario Outline to add different inputs or arguments to the same scenario. We can re-write it like this:

Feature:  Example of Scenario Outline

  @InvalidCredentials
  Scenario Outline: Login with invalid credentials

    Given User is on Home page
    When User enters username as "<username>" and password as "<password>"
    Then User should be able to see an "<errorMessage>"

    Examples:
      | username   | password | errorMessage             |
      | Admin1     | admin1   | Invalid credentials      |
      |            | admin123 | Username cannot be empty |
      | Admin      |          | Password cannot be empty |

Note:-  The table must have a header row corresponding to the variables in the Scenario Outline steps.

The Example’s section is a table where each argument variable represents a column in the table, separated by “|”. Each line below the header represents an individual run of the test case with the respective data. As a result, if there are 2 lines below the header in the Examples table, the script will run 2 times with its respective data.

When Cucumber starts to run this program, first, it will map parameters from the data table to placeholders like, and soon in the Feature File. The corresponding StepDefinition of Feature file is mentioned above. The steps can use delimited parameters that reference headers in the examples table. The cucumber will replace these parameters with values from the table before it tries to match the step against a step definition. There is no change in the StepDefinition as mentioned in the above example.

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScenarioOutlineDefinitions {

    WebDriver driver;

    @Given("User is on Home page")
    public void userOnHomePage() {

        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\Vibha\\Software\\chromedriver_win32\\chromedriver.exe");

        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://opensource-demo.orangehrmlive.com/");
    }

    @When("User enters username as {string} and password as {string}")
    public void entersCredentials(String userName, String passWord) throws InterruptedException {

        System.out.println("Username Entered");
        driver.findElement(By.name("txtUsername")).sendKeys(userName);

        System.out.println("Password Entered");
        driver.findElement(By.name("txtPassword")).sendKeys(passWord);

        driver.findElement(By.id("btnLogin")).submit();

    }

    @Then("User should be able to see an {string}")
    public void verifyErrorMessage(String expectedErrorMessage) throws InterruptedException {

        String actualErrorMessage = driver.findElement(By.id("spanMessage")).getText();
        System.out.println("Error Message :" + actualErrorMessage);
        Assert.assertEquals(actualErrorMessage,expectedErrorMessage);

        //close the browser
        driver.quit();

    }
}

TestRunner Class

 There are no changes in TestRunner class also.

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(monochrome = true, plugin = "pretty", features = "src/test/resources/Features/ScenarioOutline.feature",
        glue = "definitions")

public class CucumberRunnerTest {
}

Below is the output in Console:

Congratulations. We have seen an example of Scenario Outline. I hope this is helpful. Happy Learning!!