Cucumber Tutorial – What is Feature File in Cucumber

HOME

In the previous tutorial, we discussed How Cucumber can be setup with Eclipse. In this tutorial, we will discuss the Feature files. 

What is a Feature File?

Cucumber tests are grouped into features. A Feature file is one in which we store a description of the features and scenarios to be tested. It is used to provide a high-level description of test scenarios and group-related scenarios. A Feature File is an entry point to the Cucumber tests.

The first keyword in the Feature file is the Feature keyword, followed by: and short text that describes the feature.

You can add free-form text underneath the Feature to add more description.

These description lines are ignored by Cucumber at runtime, but are available for reporting (They are included by default in HTML reports).

The name and the optional description have no special meaning to Cucumber. Their purpose is to provide a place for you to document important aspects of the feature, such as a brief explanation and a list of business rules (general acceptance criteria).

The free format description for Feature ends when you start a line with the keyword Example or Scenario Outline (or their alias keywords).

You can place tags above Feature to group related features, independent of your file and directory structure.

A simple feature file consists of the following keywords/parts −

  • FeatureFile Name – Name of the feature under test. For example, here is LoginPage.feature
  • Feature − Describe the feature under test, like here “Login to HRM Application”
  • Scenario − What is the test scenario we want to test
  • Given − Prerequisite before the test steps get executed.
  • When − Specific condition that should match to execute the next step.
  • Then − What should happen if the condition mentioned in WHEN is satisfied
  • And/But – If we have several Given’s, When’s, or Then’s, then we can use And /But

Steps to create a Feature file

Step 1 – Create a new Maven project.

<dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>7.14.0</version>
</dependency>

Step 3 – It is suggested to create the Feature files in the src/test/resources source folder. By default, this folder is not present. So, first, create a Source Folder named src/test/resources. Right-click on the project, and select New → Source Folder.

Provide the name to the Source Folder and click on the Finish button.

This creates a new Source Folder.

Step 4 –We want all our Feature files to be present inside a folder. So, create a new folder with the name Features in the src/test/resources folder.

Step 5 – Create a Feature file in the features package.

Right-click on the Feature folder, select New ->File and mention the name LoginPage.feature. Click the Finish button. (Remember to add .feature at the end of the file, otherwise, this feature file will be just an ordinary plain text file).

The below image is an example of the new feature file created. This sample feature file gives an idea how what an actual Feature file should look like.

Below is an example of a valid feature file.

@LoginPage
Feature: Login to HRM Application
 
   @ValidCredentials
   Scenario: Login with valid credentials
    
   Given User is on HRMLogin page "https://opensource-demo.orangehrmlive.com/" 
    When User enters username as "Admin" and password as "admin123"
    Then User should be able to login successfully and new page open

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!!

Cucumber Tutorial – Cucumber Reports

HOME

The previous tutorial is all about  JUnit Test Runner. In this tutorial, will show you how to generate reports in Cucumber. 

Cucumber with JUnit gives us the capability to generate reports in the form of HTML, XML, JSON & TXT. Cucumber frameworks generate very good and detailed reports, which can be share with all stakeholders. 

Let’s generate the Cucumber Report

Step 1 − Create a Maven project named MyCucumberProject in Eclipse.

Step 2 − Create a feature file named MyHoliday.feature under src/test/resources

Step 3 − Create a StepDefinition named MyHolidayDefinitions.java under src/test/java

Step 4 − Create a TestRunner class under src/test/resources

Step 5 − Run JUnit TestRunner class by right click Run As -> JUnit

Write the following code for Feature File

Feature: Book Flight and Hotel for Vacation

@BookOneWayFlight
Scenario: Book Flight for one way trip

  Given I live in Dublin with 2 adults and 2 kids
  And I want to book one way flight ticket from Dublin to London on 22nd Jan 2020
  When I search online
  Then TripAdvisor should provide me options of flights on 22nd Jan 2020
  And Cost of my flight should not be more than 50 Euro per person
  And Tickets should be refundable
 
@BookHotel
Scenario: Book Hotel for one the trip

  Given I need 1 room with 2 double beds
  And I want to book hotel from 22nd Jan 2020 to 25th Jan 2020
  When I search online
  Then TripAdvisor should provide me options of hotels for time period of 22nd Jan 2020 to 25th Jan 2020
  And Fare of my room should not be more than 200 Euro per night
  And Breakfast should be included in the room fare

Below is the full program, which shows the step definition of above mentioned feature 

import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class MyHolidayDefinitions {

          @Given("^I live in Dublin with 2 adults and 2 kids$")
          public void needFlight() {
                   System.out.println("I live in Dublin with 2 adults and 2 kids");
          }

          @Given("^I need 1 room with 2 double beds$")
          public void needRoom() {
                   System.out.println("I need 1 room with 2 double beds");
          }

          @When("^I search online$")
          public void onlineSearch() {
                   System.out.println("I search online");
          }

          @Then("^TripAdvisor should provide me options of flights on 22nd Jan 2020$")
          public void searchFlightInTripAdvisor() {
                   System.out.println("TripAdvisor should provide me options of flights on 22nd Jan 2020");
          }

         @Then("^Cost of my flight should not be more than 50 Euro per person$")
          public void flightFare() {
                   System.out.println("Cost of my flight should not be more than 50 Euro per person");
          }

          @Then("^TripAdvisor should provide me options of hotels for time period of 22nd Jan 2020 to 25th Jan 2020")
          public void searchRoomInTripAdvisor() {
                   System.out.println(
                                      "TripAdvisor should provide me options of hotels for time period of 22nd Jan 2020 to 25th Jan 2020");
          }

          @And("^I want to book one way flight ticket from Dublin to London on 22nd Jan 2020$")
          public void ticketType() {
                   System.out.println("I want to book one way flight ticket from Dublin to London on 22nd Jan 2020");
          }

          @And("^I want to book hotel from 22nd Jan 2020 to 25th Jan 2020$")
          public void stayDuration() {
                   System.out.println("I want to book hotel from 22nd Jan 2020 to 25th Jan 2020");
          }

          @And("^Fare of my room should not be more than 200 Euro per night$")
          public void roomFare() {
                   System.out.println("Fare of my room should not be more than 200 Euro per night");
          }

          @And("^Breakfast should be included in the room fare$")
          public void includeBreakfast() {
                   System.out.println("Breakfast should be included in the room fare");
          }

          @And("^Tickets should be refundable$")
          public void refunableTicket() {
                   System.out.println("Tickets should be refundable");
          }
}

Cucumber HTML Reports

For HTML reports, add html:target/cucumber-reports to the @CucumberOptions plugin option.

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature", 
plugin = { "pretty","html:target/cucumber-reports" }, tags = { "" })

public class TestRunner {
}

We have specified the path of the Cucumber report, which we want it to generate it under the target folder. This will generate an HTML report at the location mentioned in the formatter

HTML Report Output

Cucumber Json Reports

For Json reports, add json:target/cucumber-reports/Cucumber.json to the @CucumberOptions plugin option.

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature", 
plugin = { "pretty","json:target/cucumber-reports/Cucumber.json" }, tags = { "" })

public class TestRunner {
}

JSON Report Output

Cucumber JUNIT XML Report

For JUNIT reports, add junit:targe/cucumber-reports/Cucumber.xml to the @CucumberOptions plugin option.

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature", 
plugin = { "pretty","junit:target/cucumber-reports/Cucumber.xml" }, tags = { "" })

public class TestRunner { 
}

Cucumber JUnit XML Report