What is Cucumber Feature File & Step Definition? (Example)

For every cucumber project there is a single directory at the root of the project named “features“. This is where all of your cucumber features will reside. In this directory you will find additional directories, which is step_definition and support directories

What is “Feature File”?

Features file contain high level description of the Test Scenario in simple language. It is known as Gherkin. Gherkin is a plain English text language

Cucumber Feature File consist of following components –

  • Feature: A feature would describe the current test script which has to be executed.
  • Scenario: Scenario describes the steps and expected outcome for a particular test case.
  • Scenario Outline: Same scenario can be executed for multiple sets of data using scenario outline. The data is provided by a tabular structure separated by (I I).
  • Given: It specifies the context of the text to be executed. By using datatables “Given”, step can also be parameterized.
  • When: “When” specifies the test action that has to performed
  • Then: The expected outcome of the test can be represented by “Then”

Next, we will learn how to write Feature File in Cucumber with example.

Sample Feature File Example:

Feature: Visit career guide page in career.guru99.com
Scenario: Visit career.guru99.com
Given: I am on career.guru99.com
When: I click on career guide menu
Then: I should see career guide page

Cucumber Basics

What is “Step Definition”?

Step definition maps the Test Case Steps in the feature files(introduced by Given/When/Then) to code. It which executes the steps on Application Under Test and checks the outcomes against expected results. For a step definition to be executed, it must match the given component in a feature. Step definition is defined in ruby files under “features/step_definitions/*_steps.rb”.

Example for Step Definition: Here we will use above Cucumber Feature File example of browsing career.guru99.com do We will use features like “When, Then, Given ” as shown in the below Cucumber Feature File examples.

Step 1:

Given (/^ I am on career.guru99.com$/) do

Browser.goto "http://career.guru99.com" -This will visit career.guru99 on browser

end

Step 2:
When (/^ click on career guide menu$/) do

Browser.text (:name, " career guide" ).click – This will click "career guide menu"

end
	
Step 3:

Then (/^ I should see career guide page$/) do

Browser.goto "http://career.guru99.com/category/career-guide/" - It will visit "career guide page"

end

Summary

  • You need 2 Files – Features and Step Definition to execute a Cucmber test scenario
  • Features file contain high level description of the Test Scenario in simple language
  • Steps Definition file contains the actual code to execute the Test Scenario in the Features file.