Katalon Studio – Custom Keywords

In Katalon Studio version 8.6.x, the custom keyword is a free feature that we can use in our automation. In my knowledge, this feature is not available in Katalon Studio 9.x.x. So in this blog I want to discuss and show a few examples of using custom keywords.

Create a Custom Keyword

To start and create a custom keyword, we need to go to the “Keyword” directory, right click, hover over “New” and choose “Package”. For this example, I’m naming my package as “utilities”. Then right click on hover over the newly created package, hover over “New”, and choose “Keyword”. I want to name my class name as “Randomization”, and click “OK”.

Custom Keyword for Generating a Random String

Now we need to create a method inside the class “Randomization”. I want to create a method that can generate a random string, that can be very helpful throughout my testing. I also wish to have control over how many characters my string is. So this is my method:

import java.security.SecureRandom

@Keyword
def generateRandomString(int length) {
    // Define the characters that can be used in the random string
    String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    SecureRandom random = new SecureRandom()
    StringBuilder result = new StringBuilder(length)

    // Generate the random string
    for (int i = 0; i < length; i++) {
        result.append(chars.charAt(random.nextInt(chars.length())))
    }

    return result.toString()
}

This method accepting an integer called “length”, so that if i want a random string of 5 character, i can call the method and throw the value of “5” as the parameter. Also, before the random string method, I have put a tag called “@Keyword”, this will be useful in our next section. And remember that this method returns a string value.

Call the custom keyword in our Test Case

To call the custom keyword, in manual view, click on the arrow down “Add”, and choose “Custom Keyword”. Here the syntax for calling the Custom Keyword is inputted, and because we have put the tag “@Keyword” on top of the method, we can find the name of the method to select.

We should switch over to Script view now to have more control over what’s going on. We need some variables to help the custom keyword run.

int LengthOfString = 5

String StringOf5RandomChars = CustomKeywords.'utilities.Randomization.generateRandomString'(LengthOfString)

println("Random Email: " + StringOf5RandomChars + "@abc.com")

Because the method is returning a string result, I created a string called “StringOf5RandomChars” to contain the random string. And because the method needs to have the integer length parameter, I have created an integer with the value of 5, and called it inside the custom keyword.

With this custom keyword, I can create a unique and completely random email, by adding the “@abc.com” after my random string. This can be useful in our testing. This random string can be used in many ways. Also, we can create many methods, for many things.

Using ChatGPT to Help Create a Custom Keyword

One of the advantages of using Katalon Studio is its UI, which helps testers create automation. If your coding skills is a little rusty, the good news is AI is here to help. ChatGPT can help us create a method that can be used as a custom keyword in Katalon Studio. In this example, I need ChatGPT’s help to create a method that can generate random numbers. So this is my prompt to ChatGPT:

help me create a method that can generate a random integer, with the parameter to specify how many numbers, in Groovy

ChatGPT’s answer:

import java.security.SecureRandom

int generateRandomInteger(int digits) {
		// Ensure the number of digits is at least 1
		if (digits <= 0) {
			throw new IllegalArgumentException("Number of digits must be greater than 0")
		}
    
		SecureRandom random = new SecureRandom()

		// Calculate the minimum and maximum values for the desired number of digits
		int min = (int) Math.pow(10, digits - 1)
		int max = (int) Math.pow(10, digits) - 1

		// Generate the random integer within the range
		return random.nextInt((max - min) + 1) + min
	}

// Example usage:
int desiredDigits = 5
println generateRandomInteger(desiredDigits)

So we can use ChatGPT’s answer for our custom keyword. If there’s any error just copy and paste the error to ChatGPT and let them help. Here we can see that ChatGPT’s custom keyword works. This can be used for a more advanced examples.