LOUISVILLE, KENTUCKY
ATLANTA, GEORGIA
CHICAGO, ILLINOIS
CINCINNATI, OHIO
DENVER, COLORADO
MADISON, WISCONSIN
RARITAN, NEW JERSEY
TORONTO, ONTARIO
NOIDA, INDIA
HYDERABAD, INDIA

V-Soft's Corporate Headquarters

101 Bullitt Lane, Suite #205
Louisville, KY 40222

502.425.8425
TOLL FREE: 844.425.8425
FAX: 502.412.5869

Denver, Colorado

6400 South Fiddlers Green Circle Suite #1150
Greenwood Village, CO 80111

TOLL FREE: 844.425.8425

Chicago, Illinois

208 N. Green Street, #302, Chicago, IL 60607

TOLL FREE: 844.425.8425

Madison, Wisconsin

2810 Crossroads Drive, Ste. 4000
Madison, WI 53718

TOLL FREE: 844.425.8425

Atlanta, Georgia

1255 Peachtree Parkway Suite #4201
Cumming, GA 30041

TOLL FREE: 844.425.8425

Cincinnati, Ohio

Spectrum Office Tower 11260
Chester Road Suite 350
Cincinnati, OH 45246

Phone: 513.771.0050

Raritan, New Jersey

216 Route 206 Suite 22 Hillsborough Raritan, NJ 08844

Phone: 513.771.0050

Toronto, Canada

1 St. Clair Ave W Suite #902, Toronto, Ontario, M4V 1K6

Phone: 416.663.0900

Hyderabad, India

Incor 9, 3rd Floor, Kavuri Hills
Madhapur, Hyderabad – 500033 India

PHONE: 040-48482789

Noida, India

H-110 - Sector 63 ,
NOIDA , Gautham Budh Nagar ,
UP – 201301

BDD Framework Using Cucumber: A Guide

An expert software tester monitoring behavior driven development framework

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:

Loginmethods.java  Methods for the functionality are defined in this class Methods for the functionality are defined in this class

2. LoginPage.feature.java

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

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

3. runTest.java

unner class: Class to run the feature file:

BDD Framework Using Cucumber

4. Results in Junit

Below are the Feature file run results in the Junit:

Feature file run results in the Junit

5. Test results in Console Window

Below are the test results in the Console window:

Test results in 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:

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:

Feature file is where 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.

Runner class: Class to run the feature file.

4. Results in Junit

Below are the Feature file run results in the Junit:

Results in Junit

5. Test Results in the Console window

Below are the test results in the Console window:

Test Results in 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.

Independent Testing Center of Excellence

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.

Topics: Software testing, QA TCOE, BDD Framework, Cucumber

Get tech and IT industry Updates

Selenium Guide