Implementing BDD with Katalon Studio

Test engineers are responsible for testing the code written by developers. The purpose of these tests is to ensure that the product functions correctly and provides a suitable experience for users. Test engineers use various approaches to write and implement test scenarios. In this article, we will discuss the BDD (Behavior Driven Development) approach and the commonly used Cucumber tool for implementing this approach.

What is the BDD Approach?

The BDD approach enables business analysts, customers, and other non-technical individuals to test the product and describe its behaviors in a language that closely resembles everyday conversation. The BDD approach focuses on how the product should behave and is used to verify that the software functions correctly. BDD facilitates the understanding and modification of test scenarios, even for individuals without coding skills.

Other Tools Used for the BDD Approach

There are several tools available for implementing the BDD approach, such as Cucumber, SpecFlow, EasyB, JBehave, FitNesse, and BeanSpec. These tools make it easier to write and manage test scenarios. Among these tools, the most widely used one supported by Katalon Studio is Cucumber. Cucumber is the most popular tool for implementing the BDD approach, thanks to its ease of use and extensive community support.

Cucumber and Gherkin

Writing test scenarios with Cucumber follows a standard syntax, making them easier to read and understand. Cucumber provides the standard structure through Gherkin. Test scenarios are written using expressions like Given/When/Then.

Cucumber facilitates the writing and execution of test scenarios in a structured format, ensuring that the scenarios are more readable and understandable. Gherkin is used to define the standard structure, and expressions such as Given/When/Then are used to write test scenarios.

In BDD, test scenarios are written in a human-readable format, allowing non-technical stakeholders to understand and validate the expected behaviors of the software. This promotes collaboration between technical and non-technical team members and improves the overall quality of the software being developed.

In summary, implementing the BDD approach with Cucumber in Katalon Studio allows test engineers to write test scenarios in a structured and easily understandable format. This promotes effective communication and collaboration among team members, ensuring that the software meets the desired behaviors and functions correctly.

Step 1: Adding a Feature File

In this step, we will learn how to create a feature file in Katalon. Feature files are created under the "Include" directory in the Katalon standard structure.

 "New Feature File" tab in this directory to open a screen where we can specify the name of the feature file.

In the opened screen, we give a name to our feature file. If we select the "Generate sample Feature template" option, it will provide us with an example feature file template.

Example Template: Now we have created our feature file. The feature file contains areas that show yellow warnings because the test scripts are not defined. We will see that these warnings disappear once the test scripts are written.
Let's briefly look at the expressions used in the feature file.

Feature: Represents a structure consisting of a series of scenarios.

Scenario: Represents a structure consisting of a series of steps.

Scenario Outline: Used to work with multiple test data.

Given: Represents the step for necessary initial conditions for the operation.

When: Represents the step where the operation takes place.

Then: Represents the step where the results of the operation are evaluated.

And, But: Represents additional steps for evaluating the steps.

Background: Represents the steps that will run before each scenario.

Examples: Represents the test data to be used.

Tags/Labels: Used to group scenarios.

For example, let's prepare a login scenario using the Katalon demo system. The Katalon site we will use is https://katalon-demo-cura.herokuapp.com/.

We know that the yellow warning areas indicate missing test scripts. Now we have three scenarios prepared with a Cucumber feature file written in the Gherkin language. Since the scenario outlines are prepared with a single test data, we consider it as a single scenario. If we were to test multiple test data, the number of scenarios would increase proportionally to the number of data.

Step 2: Editing the Feature File

If you want to ensure better management and maintenance processes, each step from creating to editing feature files becomes crucial. While modifications may not be highly significant when the number of tests is low, as the number of scenarios increases, maintenance and scenario editing become time-consuming. The fundamental principle in scenario writing is that each feature should be verified by a scenario.
We will use the following Cucumber features by right-clicking on the feature file:

Pretty Format: Automatically formats the feature file for better readability. Find Step: Used to map the relevant test script. Recalculate Steps: After the steps are mapped, it checks for any unmatched steps.

Step 3: Defining the Steps

We have created our feature file. Now, we will create the test scripts. There are six undefined steps that we will define test scripts for.

Given I navigate to Cura System homepage

When I click Make Appointment button

And I enter username <username> and password <password>

And I click Log in button

Then I should be able to login successfully

Then I should NOT be able to login successfully

To add the test scripts, right-click on the package where we previously created the Step Implementation file and create a new file. After giving it a suitable name, we will use either Spy Web or Record Web menus to create the test scripts.

After obtaining the test scripts with Spy Web or Record Web, we will copy them to the Step Implementation file we created.

To get the snippets of the undefined steps in the Cucumber feature file, press the run button. In the Console tab, it will provide the snippets of the steps to be implemented. We will copy all of them and paste them into our Step Implementation file. After this step, we will replace the contents of the snippets with the test scripts we obtained using Spy Web or Record Web.

Once we copy the test snippets and complete the content of the test scripts, our Step Implementation file will be ready.

After creating the test scripts, we will proceed to define these steps in the feature file. For this, right-click in the feature file and select "recalculate steps".

Defining the Default Package for Step Definitions

If you are working with multiple packages, you can significantly reduce your test execution time by defining the Test Listener. You can do this within the Step Implementation or through the Test Listeners module. or You can interpret the script of the written test steps only in this package by defining it as the default package. If you don't make any definitions or leave 'CucumberKW.GLUE = ['']' blank, it will search for test scripts in all packages.

Step 4: Running the Feature File

When you hover over the feature file, the Run button will be activated.
However, it will not allow running directly from the Step Implementation file.

Step 5: Editing Cucumber Hooks

Cucumber Hooks provide us with a structure that can be used to contribute to the output process or perform certain special operations.

They will output the scenario name to the Console before and after running the scenario.

In this article, we have tried to explain how to implement the Cucumber BDD approach with Katalon Studio in a simple way. Katalon Studio is an automation tool that can be used for web, API, mobile, and desktop applications either separately or within a single project using Cucumber BDD. If you would like to get acquainted with Katalon Studio automation tool and discuss the test solutions we provide for your applications, please feel free to contact us.