How to create a custom keyword using Katalon Studio
By: Katalon Studio
Tags: Continuous Integration, Software Development, Testing, Programming, Web Development

In addition to the built-in keywords, users can define custom keywords to extend the capability of Katalon Studio. Once created, custom keywords can be used in test cases just like other built-in keywords. This feature allows users to expand keywords and reuse them across projects.

Read more: Top 3 Selenium IDE alternatives for Firefox & Chrome

Create a Package

A package allows you to group custom keywords into a specific category, making keywords easier to search and use.

1. Select File > New > Package from the main menu to open the New Keyword Package dialog. Enter the name for your package and click OK.

2. A new package is created under Keywords of Katalon Studio accordingly.

Create a Custom Keyword

1. Select File > New > Keyword from the main menu to open the New Keyword dialog. Enter the name for your keyword and select a package for the keyword. Click OK.

2. A new keyword is created under the specified package accordingly.

3. Enter the following block of code in your class to define a custom keyword in Java/Groovy:

@Keyword

def keywordName(parameters…) {

// enter your code here

// you can use either Groovy or Java

}

KatalonTutorial-CustomKeyword hosted with ❤ by GitHub

view raw

where:

For example:

package com.example

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint

public class newkeyword {

@Keyword

def myCustomPrint(String msg) {

//print a simple message to console

println 'the message is: ' + msg

}

}

KatalonTutorial-CustomKeyword-2 hosted with ❤ by GitHub

view raw

4. Save the Keyword file when you’re done.

Custom keywords in Manual view

Follow the steps below to use your defined custom keywords in the Manual view of a test case:

1. Open a test case in Manual view, then add Custom Keyword in the script from the command toolbar.

2. A new test step is added with a list of all defined custom keywords. Select your recently created keyword here.

Custom keywords in Scripting view

Follow the steps below to use your defined custom keywords in the Scripting view of a test case:

The Class CustomKeywords allows you to access all custom keywords. Enter the following syntax into the script editor:

CustomKeywords.

The Content Assist function will be invoked immediately after you type the dot character. Content Assist provides context-sensitive suggestions for code completion. Therefore, all the custom keywords defined in your test project will be displayed as below:

Select the recently created keyword and provide all parameters as required.

The following API docs provide details on the functions used to work with custom keywords:

Driver Factory: getWebDriver() - Get the currently active web driver.

Test Object:

addProperty(String name, ConditionType condition, String value) - Add a new property to a test object

setProperties(List<TestObjectProperty> properties) - Set properties of a test object

getObjectId() - Get object ID

findPropertyValue(String name, boolean caseSensitive) - Find the value of a property using its name

Keyword Util:

You can download this project for example regarding custom keyword.

Read more: Top 10 API Testing Tools

Source: How to create a custom keyword