Information Technology - Blog V-Soft Consulting

BDD Framework Using Cucumber: A Guide

Written by Charan Sai Dasagrandhi | Apr 20, 2021 2:00:00 PM

What Is Behavior Driven Development Framework?

Behavior Driven Development (BDD) Framework enables software testers to complete test scripting in plain English. BDD mainly focuses on the behavior of the product and user acceptance criteria. Cucumber is one of the best tools used to develop in the BDD Framework. The Cucumber tool is used for development in various languages like Ruby, Python, and JavaScript. It uses Gherkin, an ordinary language parser, that permits writing scripts in English that can be easily understood. It is used to run the acceptance tests written in BDD. Here we will walk you through what BDD, and why and how you should be using Cucumber in BDD.

Advantages of the BDD Framework

  • BDD scenarios are written in plain English, which is easy to understand by everyone involved in the project, from stakeholders to project team members.
  • Scenarios are written specifically to each functionality which gives clarity for product development and testing.
  • Be it manual or automated, testing becomes very easy as the test cases or the test scripts can be easily identified from the feature files.
  • The maintenance of the framework is simple.
  • BDD also acts as project documentation for all modules and functionalities.

Why You Should Choose Cucumber for BDD Framework

  • It serves as the automated test for test scripts and documentation for the project itself.
  • Test scenarios are clearly organized in the feature files.
  • Tests are written based on user stories and system behavior corresponding to the user story in English using annotations like Given, When, Then.
  • Code can be written in any language like Java, Python, Perl, etc.

Features of Cucumber

Gherkin: In BDD, the scenarios are written in a language named Gherkin. Gherkin uses a simple syntax and keywords are easily understood by anyone like business users and stakeholders. The feature files which store the scenarios under test are written in Gherkin. Gherkin files should have the .feature file extension. Every feature file contains an individual scenario under test.

Features: Feature is a use case that describes the module under test. The feature is composed of the following details.

  • The keyword "Feature:"" is appended before the name of the feature.
  • The feature name is written beside the feature keyword.
  • An optional description is also written below the feature to describe the crux of the feature.

Scenario: A scenario is a series of steps involved in the test execution.

Scenario Outline: Used where test data is replaced with multiple sets of data for each run of a test script.

What Is BDD Framework In Selenium?

A behavior-driven development framework in Selenium (an open-source group of tools and libraries used for browser automation) enables testers a seamless automated test-driven development approach across different browsers and platforms.

Implementation of BDD Framework in Selenium Webdriver

As an example of an implementation of the test script using Cucumber, we are going to use a maven project in Selenium Webdriver. Before we start with the implementation, we need to add the dependency for Cucumber in pom.xml as we are using a maven project in this scenario. Here we are covering two scenarios.

Scenario 1

In this scenario we check the login functionality with a single user:

  1. Launch the Swag labs page
  2. Verify the Swag labs login page
  3. Enter the username
  4. Enter the Password
  5. Click on the Login button
  6. Verify the Sauce demo home page is displayed
  7. Logout of the application

Below is the Loginmethods.java class for multiple sets of data to check the login functionality for a single user.

1. Loginmethods.java

Functionality Methods are defined in this class:

2. LoginPage.feature.java

Feature file: Steps for the scenario is written in this class:

3. runTest.java

unner class: Class to run the feature file:

4. Results in Junit

Below are the Feature file run results in the Junit:

5. Test results in Console Window

Below are the test results in the Console window:

Let us see the same implementation using multiple sets of data in the below scenario:

Scenario 2

In this scenario, we check the login functionality with multiple users:

  1. Launch the Swag labs page
  2. Verify the Swag labs login page
  3. Enter the username
  4. Enter the Password
  5. Click on the Login button
  6. Verify the Sauce demo home page is displayed
  7. Logout of the application

Below is the Loginmethods.java class for multiple data sets to check the login functionality for multiple users.

1. Loginmethods.java

Methods for the functionality are defined in this class:

Here we define the step as:

@When ("^User enters the username as \"([^\”] *)\"$")

public void enter_the_username(String ar1) {

driver.findElement(By.id("user-name")).isDisplayed();

driver.findElement(By.id("user-name")).sendKeys(ar1); }

Here \"([^\"]*)\"$" denotes the user expression for multiple sets of data

The method "enter_the_username" takes String \"([^\"]*)\"$" as an argument, which is passed from the examples during runtime.

2. LoginPage.feature.java

Feature file: Steps for the scenario is written in this class:

Scenario Outline is the tag name used for testing multiple sets of data.

Examples section: To define the multiple data sets.

Here the "<username>" and <"password”> are replaced with the username and password combinations mentioned in the Examples section

Examples:

| username | password |
| standard_user | secret_sauce |
| performance_glitch_user | secret_sauce|

3. runTest.java

Runner class: Class to run the Feature file.

4. Results in Junit

Below are the Feature file run results in the Junit:

5. Test Results in the Console window

Below are the test results in the Console window:

Conclusion

Implementing BDD Framework Using Cucumber is an effective solution as it enables testers to perform and write automated test scripts in natural language, which makes it easier for stakeholders and developers to have a better understanding of test cases. Moreover, it also offers greater flexibility to code in any language like Java, Python, Perl, etc.

About Author

B.Naga Soundarya works as a Senior Test Engineer with V-Soft Digital and has more than 7 + years of IT experience in Software Testing. She is skilled in manual, Functional, Smoke, Regression, Sanity and Automation testing using Selenium Web Driver. She has sound knowledge in Mobile automation testing (Android & iOS) using Appium, TestNG, Maven, GitHub, GitLab and Jenkins.