Introduction to Custom Keywords
In addition to built-in keywords, you can define custom keywords to extend the capabilities of Katalon Studio. Once created, custom keywords can be used when implementing test cases, just like other built-in keywords.
Create a Package
A package is a namespace that organizes a set of related classes and interfaces. Because software written in the Java programming language or similar languages can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
-
Select File > New > Package from the main menu. The New Keyword Package dialog is displayed. Enter a name for your package and click OK.
-
A new package is created under the Keywords folder in Tests Explorer accordingly.
Create a Custom Keyword
-
Select File > New > Keyword from the main menu. The New Keyword dialog is displayed. Enter a name for your keyword and specify a package for the keyword. Click OK.
By default, a class name cannot start with a number, contain spaces, or have special characters. The Java naming convention suggests creating a class name using a noun or a noun phrase, with the first letter of each word capitalized, to better manage the project.
You can generate sample custom keywords for Web, Mobile, and API Testing. Refer to this guide.
-
A new keyword is created under the specified package accordingly.
-
Enter the following code snippet in your class to define a custom keyword:
@Keyword (keywordObject = "<category_name>") def keywordName(parameters…) { // enter your code here // you can use either Groovy or Java }
where:
Item Description Required @Keyword The annotation to indicate that the block of code below is the definition of a keyword. Mandatory keywordObject The category of your custom keyword (available from version 7.5.5) Optional keywordName The name that you would like to use for your custom keyword Mandatory parameters The list of parameters that would be used in the custom keyword Mandatory For example: From version 7.5.5, Custom Keywords in Keyword Browser are put in alphabetical order, and you can categorize them. In particular, the category name should be declared via
keywordObject
with the same mechanism as the built-in keywords. The below sample describes a keyword with the “Browser“ category:@Keyword(keywordObject = "Browser") def refreshBrowser() { }
-
Save the Keyword file when you're done.
Use Custom Keywords
In Manual view
Follow the steps below to use your defined custom keywords in the Manual view of a Test Case:
-
Open a test case in the Manual view and select Custom Keyword from the command toolbar.
-
A new test step is added. Select one of your custom keywords.
In Script view
Follow the steps below to use your defined custom keywords in the Script view of a test case:
-
The Class CustomKeywords of Katalon Studio allows you to access all custom keywords. Enter the following syntax into the script editor:
CustomKeywords.
-
The Content Assist function will be invoked right after you type the dot character. Content Assist provides users with 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.
See also:
The following API docs may prove useful when working with custom keywords:
Class | Method | Description |
---|---|---|
DriverFactory | getWebDriver() | Get the current active web driver. |
Test Object | addProperty(String name, ConditionType condition, String value) | Add a new property to the test object |
setProperties(List<TestObjectProperty> properties) | Set the properties of the test object | |
getObjectId() | Get object ID. | |
findPropertyValue(String name, boolean caseSensitive) | Find the value of a property using the property name | |
Keyword Util | logInfo(String message) | Log message as info |
markError(String message) | Mark a keyword to be error | |
markErrorAndStop(String message) | Mark a keyword to be error and stop execution | |
markFailed(String message) | Mark a keyword to be failed and continue execution | |
markFailedAndStop(String message) | Mark a keyword to be failed and stop execution | |
markPassed(String message) | Mark a keyword to be passed | |
markWarning(String message) | Mark a keyword to be warning |