Cucumber Pillar Page

Cucumber is an open-source testing framework that enables Behavior Driven Development (BDD) by allowing teams to write test scenarios using plain English syntax called Gherkin.

Unlike traditional automation frameworks where tests are written only from a technical perspective, Cucumber allows developers, testers, business analysts, and product owners to collaborate using a common language.

In modern automation frameworks, it is commonly integrated with:

  • Selenium WebDriver for UI automation
  • REST Assured for API testing
  • Java as the programming language
  • JUnit/TestNG for test execution
  • Maven/Gradle for dependency management
  • Jenkins/GitHub Actions for CI/CD execution

1. What is Behavior Driven Development (BDD)?

Behavior Driven Development is a software development methodology that focuses on describing application behavior from the user’s perspective.

BDD connects:

Business Requirement
|
↓
Acceptance Criteria
|
↓
Automated Test Scenario
|
↓
Test Execution

Instead of writing:

@Test
public void verifyLogin(){
driver.findElement(username).sendKeys("admin");
}

BDD encourages writing:

Scenario: Successful login
Given user opens the login page
When user enters valid credentials
And clicks login button
Then user should see dashboard page

Benefits of BDD:

  • Better collaboration between teams
  • Readable test scenarios
  • Living documentation
  • Improved requirement understanding
  • Reusable automation components

2. What is Cucumber?

It is a BDD automation tool that executes test scenarios written in Gherkin language.

A typical test contains three main components:

Feature File

Contains business-readable scenarios.

Example:

Feature: Login functionality
Scenario: Valid user login
Given user navigates to login page
When user enters username and password
Then user should successfully login

Step Definition

Java code that connects Gherkin steps with automation logic.

Example:

@Given("user navigates to login page")
public void openLoginPage(){
driver.get("https://example.com/login");
}

Test Runner

Controls execution.

Example:

@RunWith(Cucumber.class)
@CucumberOptions(
features="src/test/resources/features",
glue="steps"
)
public class TestRunner {
}

3. Cucumber Framework Architecture

A scalable automation framework usually follows this structure:

      Automation Framework

                Feature Files
                     |
                     |
              Step Definitions
                     |
        -------------------------
        |                       |
 Page Objects              API Services
        |                       |
 Selenium WebDriver      REST Assured
        |                       |
        -------------------------
                     |
              Test Execution
                     |
              Reports / CI

4. Setting Up Cucumber with Java

Prerequisites

Install:

  • Java JDK 17+
  • Maven
  • IDE (IntelliJ/Eclipse)
  • Selenium
  • Dependencies

Maven Dependencies

<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>
</dependencies>

5. Cucumber Folder Structure

Recommended enterprise structure:

src
|
├── main
|
└── test
|
├── java
| |
| ├── runners
| ├── steps
| ├── pages
| ├── utils
|
└── resources
|
├── features
├── testdata
└── config

6. Understanding Gherkin Syntax

It uses Gherkin keywords:

KeywordPurpose
FeatureApplication functionality
ScenarioTest case
GivenPre-condition
WhenUser action
ThenExpected result
AndAdditional step
BackgroundCommon setup
Scenario OutlineData-driven testing

Example:

Feature: Product Search
Background:
Given user is logged into application
Scenario Outline:
Search product
When user searches for "<product>"
Then search result should display
Examples:
|product|
|Laptop|
|Mobile|
|Tablet|

7. Cucumber with Selenium WebDriver

Cucumber + Selenium is one of the most popular UI automation combinations.

Architecture:

Feature File
↓
Step Definition
↓
Page Object Model
↓
Selenium WebDriver
↓
Browser

Example:

Feature:

Scenario: Login Test
Given user opens application
When user enters valid username
Then dashboard should display

Step:

@When("user enters valid username")
public void enterUsername(){
loginPage.enterUsername("admin");
}

8. Designing Selenium Page Objects

Recommended pattern:

pages
LoginPage.java
HomePage.java
ProductPage.java
steps
LoginSteps.java
ProductSteps.java

Example:

public class LoginPage {
@FindBy(id="username")
WebElement username;
public void enterUsername(String value){
username.sendKeys(value);
}
}

Benefits:

  • Maintainable automation
  • Less duplicate code
  • Easy UI updates
  • Better readability

9. Cucumber API Testing with REST Assured

It can also automate REST APIs.

Architecture:

Feature File
↓
API Step Definitions
↓
REST Assured Client
↓
API Endpoint
↓
Response Validation

Example Feature:

Feature: User API
Scenario: Create User
Given API endpoint is available
When user creates a new user
Then response status should be 201

Step Definition:

@When("user creates a new user")
public void createUser(){
given()
.body(requestBody)
.when()
.post("/users")
.then()
.statusCode(201);
}

10. Combining UI and API Automation in One Framework

Enterprise frameworks often combine:

UI Layer

Technology:

  • Selenium
  • Page Object Model
  • WebDriver Manager

API Layer

Technology:

  • REST Assured
  • POJO Models
  • JSON Schema Validation

Common Layer

Includes:

  • Configuration management
  • Logging
  • Reporting
  • Test data
  • Utilities

Example:

Automation Framework
|
---------------------
| |
UI Tests API Tests
Selenium REST Assured
|
Utilities
Config
Reports
CI/CD

11. Cucumber Best Practices

1. Keep Feature Files Business Focused

Avoid:

Click username textbox
Enter password
Click button

Prefer:

User logs into application successfully

2. Avoid Large Step Definitions

Bad:

LoginStep.java
500 lines

Better:

LoginSteps
SearchSteps
PaymentSteps

3. Use Dependency Injection

Recommended:

  • PicoContainer
  • Spring Context

Avoid static variables.


4. Use Tags

Example:

@smoke
Scenario: Login test

Run:

mvn test -Dcucumber.filter.tags="@smoke"

12. Cucumber Reporting

Popular reporting options:

  • HTML Reports
  • Extent Reports
  • Allure Reports

Example:

plugin={
"pretty",
"html:target/cucumber-report.html"
}

13. Running Cucumber Tests in CI/CD

Typical pipeline:

Developer Commit
↓
GitHub
↓
Jenkins/GitHub Actions
↓
Maven Test
↓
Test Execution
↓
Reports
↓
Notification

Related Articles

Cucumber Interview Questions Answers

Cucumber JUnit5 Test Runner Setup

Why most Cucumber Projects are not actually doing BDD

Cucumber Test Runner Plugins with JUnit 5