Cucumber Testing Framework: The Ultimate Guide with Practical Examples

cucumber testing

In the fast-evolving world of software development, automation in testing plays a crucial role in delivering high-quality software efficiently.

One of the most popular and effective tools in this space is Cucumber, a Behavior-Driven Development (BDD) framework.

Cucumber allows teams to create test cases in feature files that are easy to understand and follow, even for non-technical stakeholders, as it’s written in Gherkin, a natural language syntax.

 By combining natural language that we speak with powerful automation capabilities, it bridges the gap between business and technical teams.

In this blog, we will explore how to get started with Cucumber Testing, how it integrates with tools like Selenium, and how it can be used with different programming languages such as Java.

Although Cucumber is popularly used with Java, it also has official support for other languages such as Scala, Kotlin, Go, and C++.

In other languages like C# its available with the name Specflow and in Python its available as Behave

 Understanding Behavior-Driven Development (BDD)

Before diving into Cucumber, it’s essential to grasp the concept of Behavior-Driven Development (BDD), as Cucumber is built around this methodology.

What is BDD?

BDD (Behavior-Driven Development) is a software development methodology that emphasizes communication between developers, testers, and non-technical stakeholders like business analysts and product owners.

Its primary goal is to define the behavior of a feature from a user’s perspective before actual development or testing or automation starts.

Key Concepts of BDD:

  1. Collaboration: BDD promotes communication and collaboration between developers, testers, and business stakeholders (BA). The focus is on agreeing on how the system should behave before development begins to bring everyone on a common understanding before beginning the work
  2. User-Centric: Instead of focusing solely on internal system functions, BDD emphasizes how the end-user interacts with the system, ensuring that the software meets real-world needs rather than focusing on the implementation details like buttons, labels etc
  3. Executable Specifications: BDD creates living documentation by turning feature requirements into automated tests. These specifications act as both documentation and tests, ensuring alignment between what the business wants and what the developers are building.

Now you have understood what’s BDD, let’s come back to the main topic Cucumber in Testing ( Automation)


What is Cucumber?

Cucumber is a testing framework that supports BDD, allowing users to write tests in plain language using the Gherkin syntax. This means tests can be easily read and understood by all team members, regardless of their technical expertise. Cucumber focuses on collaboration, helping ensure that everyone is on the same page during the development process.


Key Features of Cucumber

  • Supports BDD: Enables the use of BDD principles, making testing more efficient and communicative.
  • Language-Agnostic: Works with multiple programming languages including Java, Python, Ruby, and JavaScript
  • Easy Integration: Seamlessly integrates with test automation tools like Selenium, Appium and Playwright.
  • Gherkin Syntax: Tests are written in simple English sentences using Gherkin, improving collaboration between testers, developers, and non-technical stakeholders.

real device cloud cta

Getting Started with Cucumber Testing

In this post, we will use IntelliJ as IDE (you can use any of your choice Eclipse, VSCode , etc.), along with Java, Maven, Junit5 on a windows operating system

1. Installing Cucumber

Before you can begin using Cucumber for test automation, you need to set up the necessary environment. Below are the steps to install Cucumber with Java.

Cucumber with Java:

  1. Install Java and configure JAVA_HOME in environment variables. Install Java 8 or above, as Cucumber is compatible with these versions.

    For this cucumber tutorial, we have installed Java 22 from the official Oracle site. If you have Java 11 version or above, the below steps should work. Since we would be using Selenium 4, the latest version where Java 8 is not supported
  1. Install Maven (for dependency and build management). It is generally a zip file which you can download and unzip

Define the path in MAVEN_HOME in the environment variable

System variables for Cucumber test automation setup

Open the command prompt and run the below command to verify if Maven is installed successfully

mvn -v

to verify if Maven is installed successfully

Command Prompt showing Maven and Java version details for Cucumber test automation setup

This command generates a new Maven project with Cucumber current latest version ( 7.18.1) as the test framework. It creates a project with the groupId hellocucumber, the artifactId hellocucumber, and the package hellocucumber. The project version is set to 1.0.0-SNAPSHOT, and the process is automated without user prompts

Screenshot of a Maven project structure in an IDE for a Cucumber framework

You can compile the project by opening terminal and command

mvn compile

gauravkhuraana/cucumber (github.com) is the link to the code to refer

Alternatively, you can click on the build option in the menu bar to build the project to check you don’t get any compilation error


2. Understanding Gherkin Syntax

Gherkin is the language used to write Cucumber testing scenarios. It uses a simple format that consists of Given, When, and Then steps. Here’s a basic example:

Feature: User Login

  Scenario: Successful Login

Given the user is on the login page

When the user enters valid credentials

Then the user is redirected to the dashboard

  • Given: Sets up preconditions.
  • When: Describes actions taken by the user.
  • Then: Verifies the outcome of those actions.

You can open the example.feature which you have just created in the above step

The Given-When-Then format can be extended using And to describe additional conditions or actions within a scenario. Here’s an example to demonstrate this:

Feature: User Registration

  Scenario: Successful registration with valid details

Given the user is on the registration page

And the user has entered valid personal information

When the user submits the registration form

And the user agrees to the terms and conditions

Then the user should be redirected to the welcome page

And the user should receive a confirmation email

In this scenario:

  • Given describes the initial state (user is on the registration page).
  • And in the Given step add more details to the precondition (user has entered valid personal information).
  • When describing the action the user takes (submitting the form).
  • And in the When step adds an additional action (agreeing to terms and conditions).
  • Then describes the expected outcome (redirected to the welcome page).
  • And in the Then step specifies an additional expected result (confirmation email is sent).

This approach keeps the scenario clear and concise while allowing multiple actions and outcomes to be included within a single scenario.

Step Definitions in Cucumber:

Step definitions link Gherkin steps to code. In the Cucumber framework, each Gherkin step is matched to a method in the step definition file.

So Whatever we write in plain English is linked to a code (function) which gets called when each of those lines are executed.

You can refer to StepDefinitions.java in the code.


3.  Integrating Cucumber with Selenium

Now you have understood BDD, Gherkin concept and created a project with basic code. Now we will integrate this with selenium to solve our purpose of automation.

Selenium is widely used to automate web browsers, and Cucumber can be integrated with Selenium to automate web applications. Here’s how you can use Cucumber with Selenium in a Java project.

Now we will add the selenium dependency in our pom file under dependencies section.

4.24 is the latest current version as of today which might change in future.

Now you can build the project and see if there are no errors.

Under external dependency, you can verify if there is a jar file for selenium to make sure it’s downloaded.

If you don’t see the jar files under the dependency, you can run the below command in the terminal

4.  Running Tests with Cucumber and Selenium:

To run your tests, we can use Cucumber’s built-in support for JUnit or TestNG. Here’s an example of a JUnit runner for Cucumber which you can see in the project created in the file named RunCucumberTest.java

As of now, we understood the already generated code by the system. We will create our own code now.

After you add a new scenario you will notice that it has underlines under it as those new steps are not defined yet.

Here the plugin/extension for the IDE related to cucumber can help you. You can install those and then you will see this option for creating all step definition options.

In the marketplace search for Cucumber and you will see various plugins with names like cucumber for java, cucumber, etc. You can download the one with maximum downloads.

I have used Cucumber for Java in IntelliJ

This will ask you where you want to create the step definition and you can select the already created step definition folder

Cucumber feature file with step definitions and a context menu for creating step definitions.

In the empty step definitions that got created, we will add selenium code like below

It’s just the basic code that initializes the driver, opens the site, enters the credentials and verifies if we are landed on the dashboard page or not.

After this, we can run our scenario that we have just added following any of the below ways. First one being the simplest if you are trying for the first time.

5.  Running Cucumber Test

There are several ways to run Cucumber tests, depending on your setup and preferences. Here’s an overview of the most common methods:

a. Running Cucumber Tests from the IDE

  • IntelliJ IDEA: Right-click on the feature file or the test runner class and select “Run”. You can also run tests from the “Run” menu or the test runner class. (RunCucumberTest)

We can even run individual scenarios from the feature file by the run button that comes in front of the scenario

  • Eclipse: Right-click on the feature file or test runner class, and select “Run As” > “Cucumber Feature” or “JUnit Test”.

b. Running Cucumber Tests via Maven

Use the Maven command line to run Cucumber tests. Ensure you have the Cucumber Maven dependencies set up in your pom.xml.

  • Run Tests: Execute the following command in your project directory:
mvn test
  • Specific Profile or Tests: You can also specify profiles or test groups if configured in your pom.xml.

c. Running Cucumber Tests from the Command Line

For Cucumber with JUnit 5 or TestNG, you can run tests using the command line. This involves using the appropriate test runner class.

  • JUnit 5:
mvn test -Dcucumber.options=”–tags @yourTag”
  • TestNG: Use the TestNG XML configuration file to specify the Cucumber test runner.

d. Running Cucumber Tests using CI/CD Tools

Integrate Cucumber tests with CI/CD pipelines (e.g., Jenkins, GitHub Actions, GitLab CI) by configuring your build scripts to run Cucumber tests as part of the build process.

  • Jenkins: Configure a Jenkins job to execute mvn test or ./gradlew test.
  • GitHub Actions/GitLab CI: Add steps in your workflow or pipeline configuration to run Cucumber tests.

These methods cover a range of scenarios for running Cucumber tests, from local development to integration in automated build systems. Choose the one that best fits your workflow and environment.

Now we will try to take full advantage of the different features of cucumber by writing a little extra code to get maximum output


  6.  Writing Efficient Tests with Cucumber

a. Reusability of Steps

One of the key advantages of the Cucumber framework is the reusability of steps. You can reuse step definitions across multiple scenarios, making test maintenance easier.

So testers/developers with less knowledge of coding can write feature files with just the existing steps and a new script is ready without writing a single line of code. This is the major reason software teams prefer cucumber in testing.

Here’s an example:

Scenario: Successful Login

  Given the user is on the login page

  When the user enters valid credentials

  Then the user is redirected to the dashboard

Scenario: Unsuccessful Login

  Given the user is on the login page

  When the user enters invalid credentials

  Then the user sees an error message

Both scenarios though different purposes use the same Given step, demonstrating step reusability.

b. Data-Driven Testing with Cucumber

Using Scenario outline

Cucumber supports data-driven testing using Scenario Outline. This allows you to run the same scenario multiple times with different inputs.

Scenario Outline: User Login

  Given the user is on the login page

  When the user enters <username> and <password>

  Then the user sees the <outcome>

  Examples:

| username | password | outcome       |

| validUser| validPass| dashboard page |

| invalidUser | invalidPass | error message|

Using Data table

A Data Table allows us to pass multiple sets of data to a single step in a scenario. This is especially useful for validating different combinations of inputs in a single test case. Here’s an example where we validate multiple valid user credentials in a single step using a Data Table:

Scenario: Successful Login with multiple valid credentials

  Given the user is on the login page

  When the user enters the following credentials:

| username       | password   |

| user1@example.com  | password1  |

| user2@example.com  | password2  |

| user3@example.com  | password3  |

  Then the user should be redirected to the dashboard for each set of credentials

The concept of Data tables which looks like Scenario Outline but there is a difference.

In the case of Scenario outline, All steps are repeated as many times as there are examples. So for a single scenario outline, number of test cases = Number of examples

In the case of a Data table, there would be 1 test case with a step that would be repeated multiple times.

So the scenario outline example when executed will run 2 test cases while the second example will have just 1

7. Report generation in Cucumber

For report generation, we should add the below line in RunCucumberTest class  i.e. we will use the plugin_property_name

This will generate html and json files in the path we have specified

How will the report be generated?

There are 2 ways you can generate a report

1)      Running the test via command line

2)      Run the Test via runner file

Cucumber HTML report displaying test execution results,

8. Parallel execution in Cucumber with Junit

You would need to create a property file with the name junit-platform.properties under resources.

It should have one line as

cucumber.execution.parallel.enabled=true

Now we will add the configuration to the maven surefire plugin in pom.xml

Now if you run your code it will execute the feature files and even scenarios in parallel. Parallelism can make your scenario run with a faster speed. Parallelism at the scenario level  is available in Junit5

Executing Cucumber Tests in Parallel with JUnit

Hope you are able to follow till here and be able to install cucumber, run code, generate reports and even run in parallel your cucumber test.

gauravkhuraana/cucumber (github.com) is the link to the code to refer

Let me conclude with some major points so you make a decision

Why should you use Cucumber?

  • Improves Communication: Since tests are written in natural language, non-technical stakeholders can review and contribute to the test creation process.
  • Enhances Collaboration: Developers, testers, and business analysts work together on the same set of requirements.
  • Readable Test Scenarios: Test scenarios in Gherkin syntax are easy to write and comprehend, making documentation clear and concise.
  • Automation Support: Cucumber can easily integrate with Selenium to provide end-to-end automation.
real device cloud cta

Why not use cucumber for Testing?

There are some key factors you should consider before using Cucumber as all tools have some disadvantages and cautions to consider.

Collaboration (Point 3) is critical. Without collaboration between technical and non-technical teams, the purpose of using Cucumber for Behavior-Driven Development (BDD) is lost.”

1. Maintenance Overhead

  • Cucumber scenarios can become difficult to maintain as the application grows, leading to duplication and complexity in step definitions. Regular refactoring is essential. A small change in English (or whichever language) word would lead to creating a new step definition

2. Slow Test Execution

  • Cucumber tests, especially end-to-end ones, can be slow when interacting with external systems like databases and browsers, slowing down build and feedback cycles as we have introduced another layer of Gherkin

3. Limited Collaboration If Used Only by Automation Teams

  • If Cucumber is used solely by automation engineers, you lose the key benefit of collaboration between developers, testers, and non-technical stakeholders, which is at the core of BDD.

4. Step Definition Management

  • Managing and maintaining step definitions for every Gherkin step can become tedious and lead to unnecessary complexity, especially as test coverage increases.

Using Cucumber automation framework effectively requires careful planning to avoid these pitfalls, ensuring it fosters collaboration and is used in the right context for testing.

Conclusion

Cucumber is a powerful tool for test automation, offering simplicity and flexibility through its natural language syntax. By combining it with tools like Selenium, testers can build robust, readable, and maintainable automated test suites. Whether you’re new to automation or looking to enhance your testing strategy, Cucumber testing provides an excellent foundation for Behavior-Driven Development and improved collaboration.

 FAQs About Cucumber Testing

1. What is the main benefit of using Cucumber in test automation?

Cucumber enables testers to write tests in plain language, which improves communication and collaboration between technical and non-technical teams.

2. Can Cucumber be used with languages other than Java?

Yes, Cucumber supports multiple languages like Python (with Behave), Ruby, and JavaScript, allowing flexibility for teams.

3. What is the purpose of Gherkin in Cucumber test automation?

Gherkin is the syntax used to write test cases in Cucumber testing. It allows the creation of human-readable test scenarios that can be understood by all stakeholders.

4. How does Cucumber integrate with Selenium?

Cucumber can be integrated with Selenium to automate browser actions, making it a powerful tool for web application testing.

5. How do I handle complex data in Cucumber?

You can use Scenario Outline and Examples to run the same test scenario with different sets of input data, making it easier to handle complex cases.

6. What is the difference between Scenario outline and Data tables?

A Scenario Outline is used to run the same scenario with different sets of data by specifying an Examples table, where each row represents a new test case. In contrast, a Data Table is used to pass multiple data inputs to a single step within a scenario, allowing validation of multiple entries in one run without repeating the entire scenario.

7. Why don’t I see an option to create a step definition or a colored feature file?

You need to install an extension from the marketplace of your IDE by searching the keyword cucumber