Cucumber Scenario

Profile picture for user devraj

The Scenario is one of the core Gherkin Structures, which is an executable specification of the system in Cucumber. A feature file can have one or more scenarios, and every Scenario consists of one or more steps. Every Scenario starts with the keyword Scenario (or its localized keyword) followed by an optional scenario title. The keyword Scenario is a synonym of the keyword Example.

Steps in a Scenario are executed one at a time in the sequence we have written them in, and each Step has a corresponding Step Definition associated with it.

You will further read about Scenario Outline in the latter course, which is similar to scenario structure; the only difference is the provision of multiple inputs.

Cucumber Scenario Example

#Sample Feature Definition Template

@search
Feature: Title of your feature

  #This is sample scenario
  @smoke
  Scenario: Title of your scenario
    Given I an on the homepage
    When I click on Login Page
    Then i should see Login page

In Scenario, You can have as many steps as you like, but 3-5 Steps per Scenario are recommended. Too many Steps will cause the Scenario to lose its expressive power as a specification and documentation. 

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

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

Apart from the above three, we also use And and But keywords in Scenario, which we will discuss later.

Video Tutorial: Cucumber Scenario