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.
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.
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.
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.
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.
In this scenario we check the login functionality with a single user:
Below is the Loginmethods.java class for multiple sets of data to check the login functionality for a single user.
Functionality Methods are defined in this class:
Feature file: Steps for the scenario is written in this class:
unner class: Class to run the feature file:
Below are the Feature file run results in the Junit:
Below are the test results in the Console window:
Let us see the same implementation using multiple sets of data in the below scenario:
In this scenario, we check the login functionality with multiple users:
Below is the Loginmethods.java class for multiple data sets to check the login functionality for multiple users.
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.
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:
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.
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.