Skip to main content

Work with BDD feature files in Katalon Studio

Learn how to work with BDD feature files in Katalon Studio.

Add feature files

This section shows you how to add feature files in Katalon Studio. Steps in the scenario will then be defined by step definitions.

To add a new feature files in Katalon Studio, do as follows:
  1. In Katalon Studio, open a Katalon project. Then, navigate to Test Explorer > Include > features. Right-click on the features folder and choose New Feature File.
    create new feature file
  2. The New Feature File dialog appears. Give your feature file a name. You may also choose to Generate sample Feature template. This option ensures that the created feature file matches with BDD convention.
    new feature file
    A new feature file is created.new feature file
  3. Add your scenarios to the feature file following the sample format:
    • Feature: List of scenarios.

    • Scenario: Business rule through list of steps with arguments.

    • Scenario Outline: Used where test data is replaced with multiple sets of data for each run of a test script.

    • Given: Precondition step.

    • When: Key actions.

    • Then: Observe outcomes or validation.

    • And, But: Enumerate more Given, When, Then steps.

    • Background: List of steps run before each of the scenarios.

    • Examples: Container for data set.

    • Tags/ Labels: To group relevant scenarios. Using tags is a good way to organize features and scenarios. A feature or scenario may have multiple tags.

    For example, to test the Login feature of the Katalon Demo Cura System (https://katalon-demo-cura.herokuapp.com/), there are two scenario outlines: login with a valid credential, and login with an invalid credential. In each scenario, there are specific Given, When, Then steps as shown below:
    log in feature file
    Sample Feature File Script:
    #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 feature files

Note:
  • Katalon Studio code inspection can detect and highlight any missing step definitions in the feature file to help you create the required step definitions.

For better management, organize your feature files with a multi-level system. A feature file can contain many scenarios, however, it's recommended you have one scenario per feature file for easy maintenance.

In Katalon Studio, there are three options to help you maintain the feature file. Right-click anywhere in the feature file editor view and choose from the following options:

OptionDescription
Pretty FormatRe-do the format when the current format is not organized properly.
Find Step

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

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

Define steps

Step definitions

After you added your feature files, you need to define and link steps before using the feature files.

Each Gherkin step in the feature files needs to be defined as a set of programming code so that Katalon Studio can execute the action of that step. These step definitions can be implemented in the 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 feature files in a test case, it also looks for the matching step definitions in the source folder.

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

For example, for two scenario outlines (Login with a valid credential and login with an invalid credential), there are six (6) steps defined as shown below:

  • 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

step definition

Step Definitions Sample Script

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()
}

}

How to define steps

Steps in the feature files must be defined before use.

To create new step definitions, follow these steps:
  1. In Test Explorer:
    1. For version 9.0.0 onwards: Navigate to the Include/scripts/groovy folder and create a sub-package. Then, right-click on the newly created package and choose New > Step Definition.
    2. For version 8.6.5 and earlier: Navigate to the Include/scripts/groovy folder, right-click and choose New > Step Definition.
  2. In the displayed Keyword dialog, choose your Package, then enter your Class Name.
    You can also enable the option to Generate sample @Given, @When, @Then steps.
    keyword
  3. Once you are done, click OK.
A new step definition is created.

new step definition

Set the default package for step definitions

Note:
  • This feature is only available for Katalon Studio 7.8.0 and later.
  • From 9.0.0 onwards, setting the default package for step definitions is required.

To help Cucumber locate the step definitions file, you can use the CucumberKW.GLUE keyword. The default value of CucumberKW.GLUE = [''] is all packages, which means the test engine takes time to scan through all packages. To speed things up, you can specify the package locations, for example, CucumberKW.GLUE = ['package1', 'package2'] to narrow down where to find the step definitions.

We recommend putting the script declaring the 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 beforeTestCase(TestCaseContext testCaseContext) {
CucumberKW.GLUE = ['package1', 'package2']
}
}
Starting from 9.0.0, for BDD projects from version 8.x to work as before, you also need to include the following @BeforeTestCase listener:
@BeforeTestCase
def beforeTestCase(TestCaseContext context) {
CucumberGlueGenerator.addDefaultPackages();
}

Run feature files

Katalon Studio allows you to run the feature files instantly by itself to make sure it works properly.

To do so, go to Include > features and open the desired feature file. Click the Run button on the main toolbar, as shown below:

Steprun feature files in Katalon Studio.

Set up Cucumber hooks

Create Cucumber feature files

To apply hooks in the Cucumber BDD test, you need to create a Cucumber feature file first, and its corresponding step definitions.
  1. To create a Cucumber feature file, go to File > New > BDD Feature File.
    new BDD feature file

    Tick the Generate sample Feature template option for a sample feature file.

    The script below is a generated sample feature template:
    #Sample Feature Definition Template
    @tag
    Feature: Title of your feature
    I want to use this template for my feature file

    @tag1
    Scenario Outline: Title of your scenario outline
    Given I want to write a step with <name>
    When I check for the <value> in step
    Then I verify the <status> in step

    Examples:
    | name | value | status |
    | name1 | 5 | success |
    | name2 | 7 | Fail |
  2. To create step definitions, go to File > New > Groovy Script.
    Create step definition
    Attention:
    • In previous versions, you can use custom keywords in BDD tests when the step definition belongs to the default package. From version 9.0.0, you cannot run BDD tests with custom keywords. This will be fixed in upcoming releases.

    Tick the Generate sample @Given, @When, @Then steps for sample step definitions.

    The script below is a generated sample @Given, @When, @Then steps:
    class Sample {

    /**
    * The step definitions below match with Katalon sample Gherkin steps
    */
    @Given("I want to write a step with (.*)")
    def I_want_to_write_a_step_with_name(String name) {
    println name
    }

    @When("I check for the (\\d+) in step")
    def I_check_for_the_value_in_step(int value) {
    println value
    }

    @Then("I verify the (.*) in step")
    def I_verify_the_status_in_step(String status) {
    println status
    }
    }

Add Cucumber hooks

To add Cucumber hooks, do as follows:
  1. Create another step definition or a custom keyword that includes the Cucumber hooks as shown below:
    Step definitions with Cucumber hooks in Katalon Studio.
  2. Enter Cucumber hooks into the new step definition.
    For example, to add @Before and @After scenario hooks, copy and paste the following script into the feature file:
    class SampleTestHook {
    @Before
    public void beforeScenario(Scenario scenario) {
    println 'This is a before scenario method: ' + scenario.getName()
    }

    @After
    public void afterScenario(Scenario scenario) {
    println 'This is a after scenario method: ' + scenario.getName()
    }
    }