Cucumber Tutorial – What is Feature File in Cucumber

 

In the previous tutorial, we have discussed that how Cucumber can be setup with Eclipse. In this tutorial, we will discuss about the Feature files. 

What is Feature File?  

A file in which we store features description about the features and scenarios to be tested. It is use to provide high-level description of test scenarios and to group related scenarios. 

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

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

These description lines ignored by Cucumber at runtime, but are available for reporting (They 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 −

Feature File Name− Name of the feature under test. For example here it is MyHoliday.feature
Feature − Describe about feature under test, like here “Book flight ticket”
Scenario − What is the test scenario we want to test
Given − Prerequisite before the test steps get executed.
When − Specific condition which should match in order 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.

Below is an example of feature file.

Feature: Book flight ticket 

@BookOneWayFlight
Scenario: Book Flight for one way trip

Given I live in Dublin with 2adults and 2kids
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

What is Step Definition?  

It is a Java method with an expression which is used to link it to Gherkin steps. When Cucumber executes a Gherkin step, it will look for a matching step definition to execute.

To illustrate how this works, look at the following Gherkin Scenario:

    Scenario: Book Flight for one way trip
    Given I live in Dublin with 2 adults and 2 kids

     Here, Given keyword will match the following step definition:

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

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

import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
 
public class MyHolidayDefinition {
 
   @Given("^I live in Dublin with 2 adults and 2 kids$")
   public void Vacation(){
        System.out.println("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$")
   public void TicketType(){       
       System.out.println("I want to book one way flight ticket from Dublin to London on 22nd Jan 2020");
   }      

   @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 FlightOptions(){  
      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 Budget(){  
        System.out.println("Cost of my flight should not be more than 50 Euro per person");
   }  

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

  Runner Class –  Cucumber test runner class is one of the many mechanisms using which you can run Cucumber feature file. The test runner class that will use in this article is a JUnit runner class.Create a runner class file.

  • Select and right-click on the package outline.
  • Click on ‘New’ file.
  • Give the file a name, such as MyRunner.java
  • Write the following text within the file and save it.
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
 
@RunWith(Cucumber.class)
@CucumberOptions(monochrome = true,
                features="src/test/resources/features/MyHoliday.feature",
                tags= {"@BookOneWayFlight"})
 
public classMyRunner { 
}

2 thoughts on “Cucumber Tutorial – What is Feature File in Cucumber

  1. It is advisable to have not more than 10 test scenarios in feature file, but it doesn't mean that if it is more than 10 , there will be error. The error must be of some different reason.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s