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:
@Testpublic void verifyLogin(){ driver.findElement(username).sendKeys("admin");}
BDD encourages writing:
Scenario: Successful loginGiven user opens the login pageWhen user enters valid credentialsAnd clicks login buttonThen 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 functionalityScenario: Valid user loginGiven user navigates to login pageWhen user enters username and passwordThen 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:
| Keyword | Purpose |
|---|---|
| Feature | Application functionality |
| Scenario | Test case |
| Given | Pre-condition |
| When | User action |
| Then | Expected result |
| And | Additional step |
| Background | Common setup |
| Scenario Outline | Data-driven testing |
Example:
Feature: Product SearchBackground:Given user is logged into applicationScenario Outline:Search productWhen user searches for "<product>"Then search result should displayExamples:|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 TestGiven user opens applicationWhen user enters valid usernameThen dashboard should display
Step:
@When("user enters valid username")public void enterUsername(){loginPage.enterUsername("admin");}
8. Designing Selenium Page Objects
Recommended pattern:
pagesLoginPage.javaHomePage.javaProductPage.javastepsLoginSteps.javaProductSteps.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 APIScenario: Create UserGiven API endpoint is availableWhen user creates a new userThen 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 TestsSelenium REST Assured | Utilities Config Reports CI/CD
11. Cucumber Best Practices
1. Keep Feature Files Business Focused
Avoid:
Click username textboxEnter passwordClick button
Prefer:
User logs into application successfully
2. Avoid Large Step Definitions
Bad:
LoginStep.java500 lines
Better:
LoginStepsSearchStepsPaymentSteps
3. Use Dependency Injection
Recommended:
- PicoContainer
- Spring Context
Avoid static variables.
4. Use Tags
Example:
@smokeScenario: 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
