Complete Guide to Cucumber BDD and Behavior-Driven Development

Introduction:

Cucumber is an open-source Cucumber BDD testing tool and a popular choice in the software testing industry. Cucumber is a Behavior-Driven Development (BDD) framework that allows you to write test cases in plain English using Gherkin syntax. It bridges the gap between business stakeholders and technical teams by using a common language that everyone can understand, making Cucumber testing efficient and easy to follow.

Let’s Understand What is Cucumber Framework:

The Cucumber framework executes test cases written in Gherkin language. Cucumber BDD test automation makes use of the following files importantly:

Feature file:

In this file, test cases are written using Gherkin, which is almost plain English-like. This file describes the behavior and functionality of the software using a specific syntax called Gherkin, which is the foundation of Cucumber BDD. Below is the basic use of a few keywords.

1. Feature: Defines the functionality being described in the feature file.

  • Example: Feature: Login functionality

2. Scenario: Describes a particular situation or test case that validates a feature.

  • Example: Scenario: User logs in with valid credentials

3. Given: Describes the initial context or preconditions for the scenario.

  • Example: Given the user is on the login page

4. When: Describes the action or event that triggers the behavior.

  • Example: When the user enters valid credentials

5. Then: Describes the expected outcome or result after the action.

  • Example: Then the user should be logged in successfully

Note: Apart from the above, there are many more keywords that can be used in the feature file, which are available in the official Cucumber documentation.

Get started with Cucumber BDD - Contact us now!

Step Definition file:

Each step in a feature file is associated with a step definition implemented, which is the implementation of code as per the feature file. Step definitions define the actions or operations that must be executed for each step of the test scenario. They map the plain text steps in the feature file to the corresponding code implementation.

Test Runner File:

It’s the heart of the framework and is mainly used for the following:

  • Configuration of Cucumber Options: It configures various settings for the test execution, such as which feature files to execute, which step definition files to link with, and other options like reporting.
  • Integration with Testing Frameworks: The Test Runner file helps in integrating Cucumber BDD with other testing frameworks like JUnit or TestNG for managing the execution lifecycle and generating reports.
  • Execution Control: It provides control over the execution of the tests, like choosing specific tags to execute or skipping certain tests.

Why Cucumber Framework?

  • Business-Readable Tests: Cucumber BDD uses Gherkin syntax (Given-When-Then) that non-technical stakeholders can easily understand. It bridges communication gaps between business and technical teams, making Cucumber testing more efficient.
  • Reusability: Step definitions can be reused across multiple scenarios, reducing code duplication and making test maintenance easier.
  • Behavior-Driven Development (BDD): Promotes collaboration between developers, QA, and business analysts. Tests are written before development, ensuring features meet business requirements.
  • Test Organization:
    • Features are organized in separate files.
    • Clear hierarchy: Feature → Scenario → Steps.
    • Easy to manage large test suites.
  • Parameterization:
    • Supports data tables and scenario outlines.
    • Easy to test multiple data combinations.
  • Rich Reporting: Built-in HTML reports with detailed test execution results.
  • Parallel Execution: Supports running tests in parallel, reducing test execution time.
  • Tags and Filtering: Group scenarios using tags and run specific groups of tests.
  • Active Community Support: A large user community with regular updates, improvements, and extensive documentation.
  • Cross-Platform Support: Cucumber BDD works across multiple platforms.
  • Cost-Effective: Open-source tool, free to use, reducing testing time and effort.
  • Supports Continuous Integration: Easily integrates with CI tools for continuous testing.

Implementation of Cucumber Framework:

We will be covering the following Cucumber BDD test scenario to perform automation testing. I have taken a random practice site: https://www.saucedemo.com/ and navigated to the login page to verify invalid and valid login scenarios using Cucumber testing techniques.

Implementation of Cucumber Framework with test automation

Project Configuration:

This project has been created using Java 17 (you can use any latest version), Cucumber 7.20.1, Selenium 4.27.0, Cucumber testing 7.20.1, Maven archetype 4.0.0. We need to add the dependencies for all from the Maven repository into the pom.xml file. Once Java and Eclipse are installed, please follow the below steps.

Step 1: Open Eclipse IDE:

Launch Eclipse IDE and install the Cucumber and TestNG plugins from Help → Eclipse Marketplace.

Step 2: Create a New Maven Project:

Go to File → New → Project → Select Maven → Maven Project and click Next.

Step 3: Select Maven Archetype:

Choose org.apache.maven.archetypes:maven-archetype-quickstart and click Next.

  1. Fill in the required fields such as Group Id and Artifact Id.
  2. Optionally, you can provide a version and a package name.
  3. Click Finish.
  4. On the console type “Y” (refer below screen) to confirm properties configuration – This step is optional if it does not ask for confirmation.
Cucumber BDD Project Configuration Setup

Step 4: Add Selenium WebDriver and Cucumber-Related Dependencies:

1. Once the Maven project is created, open the pom.xml file. Add the Selenium WebDriver and

2. Cucumber testing dependency within the <dependencies> section.

Adding Selenium WebDriver and Cucumber dependencies in Maven

Step 5: Implementing the Feature File for Login Page

1. Create a feature file named LoginPage.feature under the src/test/java folder. Both test cases are self-explanatory in the feature file below.

(Note: Folder structure can be created as per testing standards. For understanding, I have mentioned all under the src/test/java folder.)

Creating feature file for login page in Cucumber BDD

2. Created a Page object module file say LoginPage.java under src/test/java folder(refer below image). It contains all the elements on the Login page.

First part of feature file for login page in Cucumber

3. Create a Step definition file say LoginPageStepDefinition.java under src/test/java folder.(refer below image). It contains code implementation for the steps in the feature file.

Second part of Cucumber feature file for login functionality

Second section of the login page feature file implementation

4. Create a Test Runner file say TestNGTestRunner.java under src/test/java folder.(refer below image).This File is used to run the script by Right click and  “Run As → TestNG Test”.

Completing Cucumber feature file for login page testing

5. Create a java file say Hooks.java under src/test/java folder(refer below image). This file will contain pre and post feature conditions like launching browser, closing browser, screenshot on failure.

Finalizing feature file for login page testing in Cucumber

6. Create a java file called BaseTest.java under src/test/java folder(refer below image). This file will give you driver instance

Adding more steps in feature file for Cucumber BDD

7. We have given logic of report generation under TestRunner file and after execution of it we will get reports under test-output folder → ExtentReport which will contain Spark.html which we can open and see in browser (Refer below image)

Final steps of the login feature file in Cucumber BDD

Completing the last section of the login feature file

Note : All above implementation is just an example of basic automation using cucumber but yes we can truly implement more things like proper project structure, remove hardcoding, create common utilities, driving data, rerunning failed test cases and many more things.

Conclusion:

Cucumber BDD brings clarity, collaboration, and automation to software testing, leading to enhanced communication between stakeholders, early identification of issues, and better alignment with business requirements. By focusing on behavior-driven testing, it ensures that software behaves as expected in real-world scenarios, making it a valuable framework for agile teams and BDD testing practitioners.

For a practical implementation of Cucumber BDD with Selenium, you can access the complete code in this GitHub repository.

Have advanced BDD questions? Our experts are here to help!

Author's Bio

Nilesh Ingawale
Nilesh Ingawale

I am Nilesh Ingawale, with 9+ Years of Experience as a Manual and Automation Tester. Highly motivated and versatile Automation Tester with demonstrated work ethic and a solutions-oriented focus in demanding fast-paced environments. Professional and articulate with exceptional communication skills to present complex results at all levels.