Skip to main content

BDD Testing Framework (Cucumber integration) in Katalon Studio

Add Feature Files

Note:
  • For better performance, you should clean up the Katalon workspace frequently. Navigate to File > Clean up.

Features File is located within Include/features folder from your project folder and can be seen from Tests Explorer:

features files

The content of Features File follows BDD conventions: Given, When, Then. When creating a new Features File, there is an option to Generate sample Feature template, which generates a sample template for your Features File. This option ensures that the created Features File matches with BDD convention, so that you will reduce efforts in creating Features File in the correct format.

new feature file

Let's look at an example of Katalon Demo Cura System: https://katalon-demo-cura.herokuapp.com/. We want to test the Login feature with a valid and invalid credential, so the content will be something like this:



Using tags is a good way to organize features and scenario. A feature or scenario can have multiple tags.

Sample Features File

#Author: your.email@your.domain.com
#Keywords Summary :
#Feature: List of scenarios.
#Scenario: Business rule through list of steps with arguments.
#Given: Some precondition step
#When: Some key actions
#Then: To observe outcomes or validation
#And,But: To enumerate more Given,When,Then steps
#Scenario Outline: List of steps for data-driven as an Examples and <placeholder>
#Examples: Container for s table
#Background: List of steps run before each of the scenarios
#""" (Doc Strings)
#| (Data Tables)
#@ (Tags/Labels):To group Scenarios
#<> (placeholder)
#""
## (Comments)
#Sample Feature Definition Template
@Login
Feature: Login Feature

As a user, I want to login to Cura System
so that I can make an appointment.

@Valid
Scenario Outline: Login with a valid credential
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

Examples:
| username | password |
| John Doe | ThisIsNotAPassword |

@InValid
Scenario Outline: Login with an invalid credential
Given I navigate to Cura System homepage
When I click Make Appointment button
And I enter an invalid username <username> and password <password>
And I click Log in button
Then I should NOT be able to login successfully

Examples:
| username | password |
| Jane Doe | ThisIsNotAPassword |

Maintain Features File

Note:
  • Katalon Studio code inspection will detect and highlight any missing Step Definitions in Features File to help the user create the required step definitions.

There are cases the current Features File meet one of the following maintenance difficulties:

  • The current format is not organized properly.
  • Figure out which Step Definitions is mapped with current Gherkin step.
  • Recalculate steps in the Features file when there are changes in Step Definitions.

Above difficulties have been handled directly from the context menu of Feature File editor.



Pretty Format

Re-do the format.

Find Step

Find relevant step of current Gherkin step in existing Step Definitions files.

Recalculate steps

Recalculate steps in the Feature file when there are changes in Step Definitions.

Define Steps

How to define steps

Each Gherkin step in the Features file needs to be defined as a set of programming code so that the machine can execute the actions of these steps. These Step Definitions can be implemented in Keyword folder by leveraging the Script Mode. Katalon Studio built-in keywords can also be re-used in step definition files as well. When Katalon Studio executes any Features files in the test case, it will also look for the matching step definitions in the source folder.

Note:
  • Step Definitions can be written in any Cucumber-supported programming languages including Groovy and Java.

For example, let's take the Gherkin scenarios from Features File above and define the steps:



Step Definitions

class MyStepDefinition {

/**
* The step definitions below match with Katalon sample Gherkin steps
*/

@Given("I navigate to Cura System homepage")
def I_navigate_to_Cura_System_homepage() {

WebUI.openBrowser("https://katalon-demo-cura.herokuapp.com/")
//WebUI.waitForPageLoad(30)
}

@When("I click Make Appointment button")
def I_click_makeAppointment_button() {

WebUI.click(findTestObject('Page_CURA Healthcare Service/a_Make Appointment'))
}

@And("I enter username (.*) and password (.*)")
def I_enter_valid_username_password(String username, String password) {

WebUI.setText(findTestObject('Page_CURA Healthcare Service/input_userName'), username)
WebUI.setText(findTestObject('Page_CURA Healthcare Service/input_password'), password)
}

@And("I click Log in button")
def I_click_login_btn() {

WebUI.click(findTestObject('Page_CURA Healthcare Service/button_Login'))
}

@Then("I should be able to login successfully")
def I_login_successfully() {

WebUI.click(findTestObject('Page_CURA Healthcare Service/button_Login'))
WebUI.verifyTextPresent('Make Appointment', false)
WebUI.closeBrowser()
}

@And("I enter an invalid username (.*) and password (.*)")
def I_enter_invalid_username_password(String username, String password) {

WebUI.setText(findTestObject('Page_CURA Healthcare Service/input_userName'), username)
WebUI.setText(findTestObject('Page_CURA Healthcare Service/input_password'), password)
}


@Then("I should NOT be able to login successfully")
def I_login_unsuccessfully() {

WebUI.verifyTextPresent('Login failed! Please ensure the username and password are valid.', false)
WebUI.closeBrowser()
}

}

Set default package for step definitions

Note:
  • Available from version 7.8.0 onwards.

You can define a step definitions' location for Cucumber by using CucumberKW.GLUE = ['package1', 'package2']. The default value of CucumberKW.GLUE = [''] is all packages, which means the test engine takes time to scan all the packages. Defining specific locations narrows down the packages to find the steps definitions before executing feature files; hence, reducing the execution time.

We recommend putting the script of directing to a package in a test listener.

import com.kms.katalon.core.annotation.AfterTestCase
import com.kms.katalon.core.annotation.BeforeTestCase
import com.kms.katalon.core.context.TestCaseContext
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW

class NewTestListener {
@BeforeTestCase
def sampleBeforeTestCase(TestCaseContext testCaseContext) {
CucumberKW.GLUE = ['package1', 'package2']
}
}

Run a Feature File

From Toolbar

Katalon Studio allows you to run the feature file instantly by itself to make sure it works properly. Open the desired Features file, click the Play button on the main toolbar.



In Test Cases

Katalon Studio supports Cucumber keywords along with the original built-in keywords. You do not have to import Cucumber libraries into Katalon Studio.

To include Cucumber Feature file in Katalon Studio test case:

Execute a single Feature File (with or without tags)

Execute multiple Feature Files (with or without tags)

Execute using Cucumber Runner

Cucumber Reports

In Katalon TestOps

Katalon TestOps has dedicated support for viewing BDD test results as well as advanced analytics and reports such as Traceability Matrix Report. To learn about Traceability Matrix Report, you can refer to this document: Traceability Matrix Report.

By default, the BDD test report feature on Katalon TestOps is disabled to avoid mixing the BDD and Katalon Studio formatted data. To enable the feature, you can refer to this guide: View BDD Test Results in Katalon TestOps.

In Katalon Studio

There is no custom report for executing Feature File. Katalon Studio uses only generated Cucumber reports for test suite and test suite collection execution level, in which the test cases contain the Cucumber Features file.

The generated Cucumber reports of test suite and test suite collection are located in the same folder of Katalon Studio report's folder. In Katalon Studio Tests Explorer, right-click at the desired Report > Open Containing Folder. Katalon Studio redirects you to the local folder where Cucumber Reports are stored.

Katalon Studio supports three formats for Cucumber reports: JSON, XML, HTML.