BDD Testing is a testing methodology that focuses on the user’s experience and the expected behavior of the software or a system. In Katalon Studio, BDD testing is done using feature files, supported by Cucumber. So first we need to add a feature file in the Katalon Studio project. In the Test Explorer window, expand folder “Include”, right click on “features”, then “New” > “ New Feature File”. Give the feature file a name, and it should end with “.feature”. And you can choose to generate a sample Feature template, then click “OK”.
Feature Files Examples and Syntaxes
In the new Feature file we have created, we can learn some syntaxes:
- “@tag” : Using “@” symbols we can add tags to the feature and scenarios. The tags can be used when calling scenarios to run in the Test Case.
- “#” : The hashtags are used to add comment into the feature file. To add multiple lines of comment, block all the lines you want to comment, then press CTRL + SHIFT + “/”.
- “Feature” : This is used to describe our feature file.
- “Scenario Outline” : This is the name of the test scenario, and includes some test data to run with.
- “Examples” : When using “Scenario Outlines: ” this implies that we have some test data we want to run for this scenario. This is an example of how “Examples” is used:
Scenario Outline: User logs in using a valid username and password
Given User is in the login page
And User input a valid username <username>
And User input a valid password <password>
When User click on the button login
Then User successfully logged in
Examples:
| username | password |
| student | Password123 |
- “Scenario” : This is the name of the test scenario. Followed by some preconditions, test steps, and expected results. Just like in the “Scenario Outline” example above, the “Scenario” is similar without using “Examples”.
- “Given” : is equal to the preconditions in the test case
- “When” : is equal to the test steps of the test case
- “Then” : is equal to the expected result of the test case
- “And” : if the preconditions, steps, or expected results are more than one, we can use “And”. For example:
When User checks on the “keep me logged in” checkbox
And User clicks on the login button
This means the user checks the “Keep me logged in” checkbox, then clicks the button login.
Creating the Scripts for a Feature File
After creating a new Feature File, we’re making a script to accommodate it. The Script is where all the steps are being defined. To create a new Script, in the Test Explorer window, expand folder “Include”, right click on “script” folder, then “New” > “Step Definitions ”. Give it a name and click “OK”.
For example, my Feature File is as follows:
Scenario Outline: User logs in using a valid username and password
Given User is in the login page
And User input a valid username <username>
And User input a valid password <password>
When User click on the button login
Then User successfully logged in
Examples:
| username | password |
| student | Password123 |
Then the script will be as follows:
class LoginSteps() {
@Given("User is in the login page")
def User_is_in_the_login_page() {
WebUI.openBrowser("<https://practicetestautomation.com/practice-test-login/>")
WebUI.maximizeWindow()
}
@And("User input a valid username (.*)")
def User_input_a_valid_username(String username) {
WebUI.setText(findTestObject("Login/Textbox_username"), username)
}
@And("User input a valid password (.*)")
def User_input_a_valid_password(String password) {
WebUI.setText(findTestObject("Login/Textbox_password"), password)
}
@When("User click on the button login")
def User_click_on_the_button_login() {
WebUI.click(findTestObject("Login/Button_submit"))
}
@Then("User successfully logged in")
def User_successfully_logged_in() {
WebUI.verifyTextPresent("Logged In Successfully", false)
}
}
Some things to note for the Feature Files and Scripts:
- The steps defined in all Scripts are reusable in any Feature File. Therefore it has to be noted that all steps should be unique.
- If a step is defined as “Given”, for example. I find that we are still able to use it in a feature file as an “And”, “When” or “Then”.
- The “Examples” in the “Scenario Outlines” will be run one row at a time. For example below, we have 2 rows of test data. This means that when running this scenario, it will first use the first row of data, close the browser, and run again using the second row of data.
Examples:
| username | password |
| user001 | Test123! |
| user002 | Test456! |
- To use more than one variables in a single step is as follows:
The feature file:
Scenario Outline: User logs in using a valid username and password
Given User is in the login page
And User input a valid credential "<username>" "<password>"
When User click on the button login
Then User successfully logged in
Examples:
| username | password |
| user001 | Test123! |
| user002 | Test456! |
The script for using more than one variables in a step:
@And("User input a valid credential "([^"]*)" "([^"]*)")
def User_input_a_valid_username(String username, String password) {
WebUI.setText(findTestObject("Login/Textbox_username"), username)
WebUI.setText(findTestObject("Login/Textbox_password"), password)
}
This will help to separate the variables so that they can each be identified.
- It should also be noted that the data in the “Examples” are defined as strings.
Run Feature Files in a Test Case
To run a feature file in a test case, create a new test case by right clicking on the “Test Case” folder and choose New Test Case, then give the Test Case a name. In manual view, click on arrow down “Add” button, choose “Cucumber Keyword”, then look for “Run Feature File” (There are also other options to run the feature file with tags or in a specific folder). Then double click on the input column, and give the value of the feature file directory, for example “/include/features/LoginSteps”.
In the script view it should look something like this:
CucumberKW.runFeatureFile('/include/features/LoginSteps')
