In today’s newsletter we are going to discuss
Introduction of Cucumber & BDD
Steps to write automation tests using Cucumber
Cucumber Tags
Background in Cucumber
Scenario Outline in Cucumber
What is Cucumber ?
Cucumber is a testing approach/tool that supports Behavior Driven Development (BDD). It provides a way to write tests that anybody can understand, regardless of their technical knowledge.
It explains the behavior of the application in a simple English text using Gherkin language.
What is BDD (Behavior Driven Development)
In very simple words, BDD is a technique where your test cases are written in plain English like sentences. With this approach, the non-technical team members find it easy to understand the flow and collaborate more in the process of software development.
Here is one of the example of plain English written in Gherkin language. This file is also called as Feature File.
Steps to write automation tests using Cucumber
Prerequisite
In this newsletter, we will be using:
Java as the programming language
TestNG as the assertion framework
Maven as the build automation tool
Selenium WebDriver as the browser automation tool
Cucumber as BDD tool
Eclipse as the IDE
Step 1: Write scenarios in Feature FileOnce we create feature file, we can write scenarios as shown below.
Step 2 : Implement scenario using Step Definition File
We can implement above scenario using Step Definition File. Step definition files contain the actual code implementation of the steps mentioned in the feature files.They map Gherkin steps to executable code as shown below.
Step 3 : Create Test Runner File
A Cucumber test runner is a Java class that kicks off the execution of Cucumber scenarios. It specifies the features, step definitions, plugins, tags, and other options that you want to run.
Cucumber Tags
Tags in Cucumber are used to categorize scenarios.
We can add tags like 'smoke' & 'regression' to our feature file, and then you can run specific scenarios.
Example 1 : Executing one Tag from Test runner
Here we are executing all scenarios tagged with “@smoke” by adding tags in test runner file.
Example 2 : Executing multiple Tags from Test runner
Here we are executing all scenarios tagged with “@smoke” & “@regression” by adding tags in test runner file.
Background in Cucumber
Background keyword contains common steps that will be executed before each scenario in the feature file.
In the above example, we have two different scenarios where a user is purchasing product and remove product from cart. In both these scenarios, common step is to login to website for both the scenario.
This is why we create another Scenario for Log in but named it as Background rather than a Scenario. So that it executes for both the scenarios.
Scenario Outline in Cucumber
It allows you to run the same scenario multiple times with different sets of data.
Here's an example of how you can use Scenario Outline for parameterization.
Here is an example of Feature file having scenario outline.
In this example, the <product> and <result> placeholders in the scenario outline are replaced with values specified in the Examples table. The scenario outline will be executed three times with the provided data sets.
In this step definition file, the {string} placeholders in the step definitions are replaced with the corresponding values from the feature file during execution.
“There will be obstacles. There will be doubters. There will be mistakes. But with hard work, there are no limits.” — Michael Phelps
Let’s Connect on Social Media
Connect with me on LinkedIn (8.1K+ Followers) and Twitter.
Please upload Api Notes postman and Restassured