Introduction to Cucumber & Step Definitions

 

Gherkin Syntax

 

Learning Outcome

4

Write clear and maintainable Gherkin tests

3

Separate preconditions, actions, and outcomes

2

Convert requirements into test scenarios

1

Create BDD scenarios using Gherkin keywords

5

Organize scenarios into feature files

 

Gherkin Syntax

 

Basic knowledge of software testing concepts

Familiarity with requirements or user stories

Understanding of test cases (input, steps, expected result)

Basic idea of Behavior-Driven Development (BDD)

Ability to write simple, clear English statements

Basic awareness of automation tools like Cucumber

RECALL

Storytelling Analogy

 

Gherkin is like telling a simple story:

 

  • Feature → The story title

  • Scenario → One episode of the story

  • Given → Starting situation

  • When → What happens

  • Then → Outcome of the event

 It describes what happens in the system, like a story with steps.

 

Why do we use Gherkin syntax?

 

We use Gherkin syntax for the following reasons:

 

  • To describe software behavior in simple, human-readable language

  • To bridge communication between business stakeholders, testers, and developers

  • To convert requirements into structured test scenarios

  • To support Behavior-Driven Development (BDD) practices

  • To make test cases clear, consistent, and easy to understand

  • To enable automation testing by linking scenarios with tools like Cucumber

  • To ensure everyone has a shared understanding of expected system behavior

What is Gherkin?

Gherkin is a structured, plain-English language used to write test scenarios in Behavior-Driven Development (BDD).

It describes how software should behave in a way that both technical (developers/testers) and non-technical (business users) people can understand.

 

Features of Gherkin

 

Written in simple English-like language
 

Follows a fixed structure
 

Used in BDD (Behavior-Driven Development)
 

Works with automation tools like Cucumber
 

Focuses on system behavior, not code

Gherkin  Structure

 

Example:

 

Feature: Login functionality

 

Scenario: Valid user login

Given the user is on the login page

When the user enters valid credentials

Then the user should be redirected to the dashboard

 

Keywords used:

  • Feature → What is being tested

  • Scenario → A specific test case

  • Given → Precondition

  • When → Action

  • Then → Expected result

  • And / But → Additional steps

 

 

 

 What is  pom.xml? 

Differentiate between preconditions, actions, and expected outcomes

 

This focuses on understanding the role of each step:

 

Given =  Precondition

  • Setup or initial state

  • Example: user is on login page

When → Action

  • What the user does

  • Example: enters username and password

Then → Expected Outcome

  • Result of action

  • Example: user is logged in successfully

Example:

 

Given user is on login page        → precondition

When user enters credentials       → action

Then user logs in successfully     → expected result

 

GOAL

Clearly separate setup, action, and result.

 

Summary

4

It improves team communication.

3

It turns requirements into test cases.

2

It uses keywords like Feature, Scenario, Given, When,Then.

1

Gherkin is a simple plain-text language in BDD

5

It makes tests easy to read and automate.

 

Quiz

What is Gherkin syntax used for?

A. Writing programming code

B. Writing test scenarios in plain English

C. Designing databases

D. Creating UI designs

Quiz-Answer

What is Gherkin syntax used for?

A. Writing programming code

B. Writing test scenarios in plain English

C. Designing databases

D. Creating UI designs

 Which annotation is used for teardown in Cucumber?

A.@Before

B.@After

C.@Test

D.@RunWith

Quiz-Answer