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.
Maintain feature files
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:
Option | Description |
---|---|
Pretty Format | Re-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 steps | Recalculate 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 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.
Set the default package for step definitions
- Starting with Katalon Studio 9.0.0 and later, 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']
}
}
@BeforeTestCase
listener:@BeforeTestCase
def beforeTestCase(TestCaseContext context) {
CucumberGlueGenerator.addDefaultPackages();
}
The glue should be assigned to the step definition packages of the feature files, not the packages of the Cucumber runner.
- If the glue is incorrectly set to the runner package (such as
glue="com.vv.sailorApp.Runners"
), the test may pass but no steps will be run. To fix this, ensure the glue points to the correct step definition packages, for example:glue = ['com.vv.sailorApp.StepDefinitions', 'otherStepDefinitionPackages']
- Make sure all Cucumber hooks, such as
@Before
and@After
, are placed in the correct step definition folders. For example, move all the hooks from the filesTest Listeners/SetupTestListener
,Keywords/com.vv.listener/InitialSetup.groovy
to the step definition folders, such as:Include/scripts/groovy/CucumberHooksDefinitions/InitialSetup.groovy
Include/scripts/groovy/CucumberHooksDefinitions/SetupTestListener.groovy - Add glue to these hooks in your Cucumber runner as well:
glue = ['stepDefinitionPackage1', 'Include/scripts/groovy/CucumberHooksDefinitions', 'otherStepDefinitionPackages']
- For other Cucumber elements that are not executed via the Cucumber runner, you can add glue in setup steps like this:
@BeforeTestCase
def beforeTestCase(TestCaseContext context) {
CucumberGlueGenerator.addDefaultPackages();
// or
CucumberKW.GLUE = ['<all the step definition packages>']
}
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: