Test Automation with Katalon: Optimize Script Reusability By Using a Custom Keyword

How You Can Use Custom Keywords via Katalon To Improve Test Automation

Most applications nowadays are made up of several repeated steps in their navigation. These repeated steps may cause some toil in Automation testing as we are required to write or record the steps all over again. Another case would be automating the required background process before executing the test case, such as connecting to the Database or parsing API responses. Katalon provides us with a feature called Custom Keywords to simplify the processes into functions that are available to utilize across its framework.

Testing is an extremely important process that should be done well. Automation testing can not only improve the quality of testing but also efficiency. To optimize test case coverage, you can use equivalence partitioning & boundary values.

Initialization

Before we get into the main topic of the discussion please make sure that we have installed the appropriate requirements such as Katalon and its environment. The steps below will guide you through the basic introduction to Katalon.

Download Katalon Studio

Katalon Studio is available for free, and supports Windows and macOS. Visit the link below to download the latest version.

Environment Configuration

Please make sure your computer meets the System Requirements. If you are doing Web UI Testing, make sure the required browsers are installed. For Mobile Testing, there are also OS requirements and additional environments to be installed. Please check the links below for the System and Environment Requirements.

Creating Test Case

There are two ways to create a Test Case in Katalon. The first one is using a mobile Recorder, where you can imitate the navigation and gestures of using an application and then recording in the Test Case. The second method is writing the navigations of the application in the script by calling methods on the recorded objects of the application. The links below will give a more detailed guideline on how to create one.

What are Keywords?

Keywords are basically methods provided by the Katalon Library. The commonly used keywords are Web UI Keywords, Mobile Keywords, Web Service Keywords & Windows Keywords. These keywords are modified methods taken from its root framework (Selenium, Appium, etc) and then simplified by Katalon.

The built-in keywords may not fully provide all the possible cases that you need to overcome to verify the testing. Therefore, Katalon provides us with customizable Keywords to extend the testing capabilities. Its main purposes consist of:

  1. Creating reusable scripts
  2. Extending testing capability
  3. Setting up testing projects with a specific pattern

Creating Custom Keywords

We start by creating a package for the Custom Keywords. 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.

  1. Select File > New > Package from the main menu. The New Keyword Package dialogue is displayed. Enter a name for your package and click OK.
Create package
  1. A new package is created under the Keywords folder in Tests Explorer accordingly.
Package directory
  1. Select File > New > Keyword from the main menu. The New Keyword dialogue 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.

Class name
  1. A new keyword is created under the specified package accordingly.
Keyword directory
  1. Enter the following code snippet in your class to define a custom keyword:
Custom keyword

The Custom Keywords are generated similarly to a Class File in Java. Within the Keyword Class, the user can write similar codes to it as well, such as: declaring variables, creating functions and even importing external Java or Groovy libraries. It is required to include @Keyword before each function in order for the katalon to be able to call them on the test cases.

How and Where to Use Custom Keywords

Custom Keywords’ primary objective is to be used in the Test Case, but actually, there are several other ways they can be used. Since Custom Keywords can import libraries, it’s possible to use the other features in Katalon such as Object Repository, Data Files, etc. Please note that Custom Keywords are just a standalone Class File, so it’s not able to run on itself, it requires the test cases to call on them. I will be mentioning several sample collaborations of Custom Keywords with other features in Katalon below!

Test Case

The only way to make the functions within the Custom Keywords run is by calling them from the test case. 

  1. For example, we have a function that outputs a String value.
Functions example
  1. Then we can call and print this on the console log from the Test Case
Console print
  1. Let’s combine them with the built-in keywords. Assume we are doing verification of a value, Actual vs Expected simply said.
Combine

It is also possible to write the logic of the function in the test case but it is highly recommended to avoid it to maintain the code readability and reusability as it will be a hassle to copy lines of codes in the test case rather than just calling a one-liner code to trigger the logic.

Object Repository

Custom Keywords can also call the Objects that we have saved using the built-in Keywords since the libraries are automatically imported after creating the Custom Keyword. To put it simply, we can also execute the navigations of the application in the Custom Keywords but it will cause the navigations to be unwritten in the report. 

The best sample case for this would be when we want to start testing at a specific point. Assume we execute Test Case A that starts from screen A to K then we continue executing Test Case B that starts from A to L, since Test Case A ends on screen K then we need to reset our starting point again to screen A, this may occur in hundreds of Test Cases. Implementing Custom Keywords for this repeatable process can greatly improve our efficiency.

One of the main purposes of these two integrations is actually when we have Web Service Requests. Web Services have tendencies of dynamic values for the requests and responses and sometimes multiple Web Services are needed to get the wanted response as the values will be passed over them. Custom Keywords are capable of doing such actions because they are able to call the Web Service and then parse and save the response values into variables, from there we can continue to call another Web Service with the new values if desired. A simple example is illustrated below.

Let’s say we want to call a Web Service but that Web Service only accepts the request in encrypted values otherwise it will return an error. We have another Web Service that can encrypt the values. Therefore, we need to encrypt the values first using Web Service A and then pass them onto Web Service B.

  1. First we setup the Web Services with variables to make them parameters so the Custom Keyword can insert the values
Setup variables
Setup values
  1. Create a Custom Keyword to call and process these Web Services. By using built-in Keywords we can use their methods to Request Web Service A.
Request web service
  1. Parse the Response of the Web Service using the JsonSlurper library.
Response parse
  1. Call Web Service B with the parsed values inputted into the Web Service as parameters.

Data Files

The built-in Keywords do not support modifying Data Files. The users can create Custom Keywords to write the Data Files (csv, excel, etc). More information can be found on the following link.

Example Cases

The most commonly used case that we can implement is putting the repeated process into a Custom Keyword, whether on the Front End side or the Backend.

We have a case where the application shows different screens after we launch from the background. It differs depending on if we have logged in or not. If we have logged in with our account and then reopen the application, the application will prompt for credentials. Whereas if we haven’t logged in, the application will immediately display the Home Screen. For this case, we will create a Custom Keyword to indicate such conditions then navigate to the Home Screen.

  1. This application has several conditions in its starting display and we also include a method to start the application.
Start application method
  1. Then we call this Custom Keyword at the beginning of each of our test cases.
Call custom keywords

Another case would be validating values shown in the UI and values returned from API responses. There may also be a condition where the API endpoints need to be accessed from preceding endpoints. Each endpoint can be created on a function within the Keyword, therefore mimicking the flow process of the endpoints.

  1. This Test Case verifies several values between the UI and API responses. We use Keywords to return the values from the API.
  1. The Keyword parsed the responses
Parsed responses

Conclusion

Keywords are highly effective to reuse the processes and functions within them in the Test Cases. This will create a better maintenance experience for the engineers to add and modify the Test Cases. Any logic of the application should be put in the Keywords, the Test Cases should only function as a validation.

1
Share

More Articles

let's talk illustration

Loving what you're seeing so far?

It doesn’t have to be a project. Questions or love letters are fine. Drop us a line

Let's talk arrow right