Features and Scenario in Behat

Profile picture for user devraj

Feature File: In a project we have different feature files.  A file in which we store feature, feature description and scenarios to be tested. The extension of feature file should be ".feature".  For each functionality there should be separate feature file. Example search.feature, registration.feature etc.The names for Scenarios and Feature file must be unique.

In feature file you describe your test in Descriptive language like English, French etc. Feature file contains Business requirement. It is an essential part of Behat, as it serves as an automation test script as well as live documents.

Feature file can contain either Scenario or Scenario outline or combination of them. A feature file is an entry point to the Behat tests.

Feature: A feature can be defined as a standalone unit or functionality of a project. Each independent functionality of the project under test can be termed as a feature. Each feature file start with keyword Feature: or its localised equivalent.

Feature Title: The text that immediate follow feature keyword is Title of the Feature file.

Feature Description: The suggested best practice is, to write a small description of the feature beneath the feature title in the feature file. This will fulfil the need of a good documentation as well.

Scenario: A feature file can have one or more scenarios. A feature consist of list of scenarios which start with Scenario: keyword. Scenario is one of the core Gherkin Structures. Every scenario starts with the keyword Scenario (or its localized keyword). The keyword Scenario is a synonym of the keyword Example.

Scenario Title: "Scenario:" keyword is followed by an optional scenario title.

Steps: Scenario further consist of Steps which start with keyword like Given, When, Then, But or And. Every scenario has one or more steps.

When we specify business requirement, we need to specify the pre-conditions, user actions and expected output. These can be easily represented using given, when and then.

  • Given: Pre-conditions are mentioned in Given keyword.
  • When: User actions are described in When.
  • Then: It is used for expected output.

Example of Feature and Scenario:

#This is sample comment
Feature: Search
  In order to filter the content
  User should be able to
  to search on the website

  Scenario: Search for a word that does not exist
    Given I am on the homepage
    When I fill in "searchTerm" with "XBox"
    And I press "search_submit"
    Then I should see "No products found"

  Scenario Outline: Search for a product
    Given I am on the homepage
    When I fill in "searchTerm" with "<term>"
    And I press "search_submit"
    Then I should see "<result>"
      | term    | result              |
      | Samsung | Samsung Galaxy S II |
      | XBox    | No products found   |

In above example @search, @tag1 and @tag2 are tags. Comments are there with #. There is Background section also, which we will discuss later.