In addition to built-in keywords, users can define custom keywords to extend the capability 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 will be displayed. Enter the name for your package and click OK.
- A new package is created under Keywords of Katalon Studio accordingly.
Create a Custom Keyword
Select File > New > Keyword from the main menu. The New Keyword dialog will be displayed. Enter the name for your keyword and specify a package for the keyword. Click OK.
Since version 5.4, Katalon Studio provides user option to generate sample custom keywords for Web, Mobile and API Testing.
Refer to this guide: https://docs.katalon.com/x/3QHR
By default, a class name cannot start with a number, contain spaces, or have special characters. The Java naming convention suggests the user to create a class name using a noun or a noun phrase, with the first letter of each word capitalized, to better manage the project.
A new keyword is created under the specified package accordingly.
Enter the following block of code in your class to define one custom keyword:
Item Description @Keyword The annotation to indicate that the block of code below is the definition of a keyword. keywordName The name that you would like to use for your custom keyword parameters The list of parameters that would be used in the custom keyword
- Save the Keyword file when you're done.
Custom keywords in Manual view
Follow the steps below in order to use your defined custom keywords in Manual view of a Test Case:
- Open a test case in Manual view, then select to add Custom Keyword from command toolbar.
- 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 in order to use your defined custom keywords in Scripting 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:
- The Content Assist function will be invoked immediately after you type the dot character. Content Assist provides users with the 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 may prove useful when working with custom keywords:
|Driver Factory||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|
With custom keywords, you can connect to a database as well as perform other data queries.
Below is a sample demonstrating how to create custom keywords for:
- Establishing database connection
- Executing data query
- Closing the connection
You can add the sample code above to your keyword file and modify the details as appropriated.