Skip to main content

Cucumber with Java: Scenario

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

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

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 Scenario

#Sample Feature Definition Template

Feature: Title of your feature
  I want to use this template for my feature file

  #This is sample scenario
  Scenario: Title of your scenario
    Given I want to write a step with precondition
    And some other precondition
    When I complete action
    And some other action
    And yet another action
    Then I validate the outcomes
    And check more outcomes

In above example you can see a single scenario tagged with smoke and this scenario consist of different steps which starts with Gherkin keywords Given, and, then, when etc. Above file has single scenario, you can declare multiple in similar way.

Each steps of Scenario has step definition associated with it, you will read about it later.

Video Tutorial


Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.