
📌 Cucumber with Selenium WebDriver
Cucumber with Selenium WebDriver: Cucumber and Selenium are two of the most widely-used tools in the QA automation ecosystem. By combining them, teams can write easy-to-understand test cases in plain English, bridging the gap between technical and non-technical stakeholders.
In this article, you will learn how to integrate Cucumber with Selenium WebDriver and create automation test cases using the BDD approach.
Table of Contents
- What is Cucumber?
- What is Selenium?
- Why Use Cucumber with Selenium?
- Prerequisites for Integration
- Required JAR Files and Downloads
- Sample Scenarios using Cucumber with Selenium
- Conclusion
📌 What is Cucumber?
Cucumber with Selenium WebDriver: Cucumber is an open-source tool that supports Behavior Driven Development (BDD). It allows writing test cases in simple, plain-text English using a language called Gherkin.
This makes it easier for business analysts, testers, and developers to collaborate on test automation.
- Gherkin Syntax is human-readable
- Features are written in .feature files
- Steps are mapped to underlying Java code
📌 What is Selenium?
Cucumber with Selenium WebDriver: Selenium WebDriver is a web automation tool used for functional testing of web applications. It supports multiple programming languages, including:
- Java
- Python
- C#
- Ruby
Selenium is widely used for browser-based automation across platforms and devices.
📌 Why Use Cucumber with Selenium?
Cucumber with Selenium WebDriver: Integrating Selenium with Cucumber enables teams to write automated test scripts that are:
- Easy to read and understand
- Maintainable and reusable
- Written in natural language, accessible to all stakeholders
✅ Benefits of Using Cucumber with Selenium:
Benefit | Description |
Readable Test Cases | Use Gherkin to write test cases in plain English |
Better Collaboration | Bridges gap between QA, developers, and BAs |
Realistic Scenarios | Write test cases based on user stories |
Reusable Components | Use the same step definitions across tests |
📌 Prerequisites for Cucumber and Selenium Integration
Before you begin, make sure you have the following installed and configured:
Java Development Kit (JDK)
- Required for running Selenium and Cucumber code.
Eclipse / IntelliJ IDE
- Or any Java-compatible development environment.
Selenium JAR Files:
- selenium-server-standalone
Download from: https://www.selenium.dev/downloads
Cucumber JAR Files:
- cucumber-core
- cucumber-java
- cucumber-junit
- cucumber-html
- gherkin
- cucumber-reporting
- cucumber-jvm-deps
- hamcrest-core
- junit
All can be downloaded from:
💡 Tip: You can manually download the JARs or manage them via Maven or Gradle for easy dependency management.
How to Download Cucumber Core (Example):
- Visit https://mvnrepository.com
- Search for cucumber-core
- Select version (e.g., 1.2.2)
- Click Download JAR
⚠️ It’s recommended to always use the latest compatible versions to avoid dependency conflicts.
📌 Automation Testing Using Selenium with Cucumber
Let’s go through three practical BDD Scenarios using Selenium + Cucumber.
✅ Scenario 1: Print Text to Console
Feature File Example:
gherkin
Feature: Print Message
Scenario: Print Hello World
Given I want to print a message
Then Print “Hello World”
✅ Scenario 2: Enter Login Credentials and Reset
Feature File:
gherkin
Feature: Login and Reset
Scenario: Enter login and reset
Given User is on login page
When User enters username and password
Then User clicks on reset button
✅ Scenario 3: Data-Driven Login Testing with Multiple Sets
This scenario executes login steps with multiple sets of credentials using Scenario Outline.
gherkin
Feature: Guru99 Login
Scenario Outline: Login with valid credentials
Given User is on login page
When User enters “<username>” and “<password>”
Then User clicks on reset button
Examples:
| username | password |
| user1 | pass1 |
| user2 | pass2 |
| user3 | pass3 |
Note: You’ll need corresponding Java step definitions using Selenium methods like sendKeys() and click() to interact with UI.
📌 Sample Step Definition (Java)
java
@When(“^User enters \”([^\”]*)\” and \”([^\”]*)\”$”)
public void user_enters_credentials(String username, String password) {
driver.findElement(By.id(“user_login”)).sendKeys(username);
driver.findElement(By.id(“user_pass”)).sendKeys(password);
}
📌 Reporting and Logs
Once test execution is completed, you can generate:
- HTML reports using cucumber-html
- Code coverage using Cobertura (optional)
- Execution logs for debugging test failures
📌 Conclusion
By integrating Cucumber with Selenium WebDriver, you adopt a powerful BDD testing strategy that ensures collaboration, test clarity, and maintainability.
This combination allows:
- Non-technical stakeholders to understand test flows
- Developers and testers to work in sync
- Reusable and scalable test automation framework