{"id":78,"date":"2025-09-06T09:16:46","date_gmt":"2025-09-06T09:16:46","guid":{"rendered":"https:\/\/blog.kerjarapi.com\/?p=78"},"modified":"2025-09-06T09:16:46","modified_gmt":"2025-09-06T09:16:46","slug":"katalon-studio-custom-keywords","status":"publish","type":"post","link":"https:\/\/blog.kerjarapi.com\/?p=78","title":{"rendered":"Katalon Studio &#8211; Custom Keywords"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Custom Keyword<\/h2>\n\n\n\n<p>To start and create a custom keyword, we need to go to the \u201cKeyword\u201d directory, right click, hover over \u201cNew\u201d and choose \u201cPackage\u201d. For this example, I\u2019m naming my package as \u201cutilities\u201d. Then right click on hover over the newly created package, hover over \u201cNew\u201d, and choose \u201cKeyword\u201d. I want to name my class name as \u201cRandomization\u201d, and click \u201cOK\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"418\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-15-1024x418.png\" alt=\"\" class=\"wp-image-79\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-15-1024x418.png 1024w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-15-300x122.png 300w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-15-768x313.png 768w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-15.png 1312w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Custom Keyword for Generating a Random String<\/h2>\n\n\n\n<p>Now we need to create a method inside the class \u201cRandomization\u201d. 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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.security.SecureRandom\n\n@Keyword\ndef generateRandomString(int length) {\n    \/\/ Define the characters that can be used in the random string\n    String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'\n    SecureRandom random = new SecureRandom()\n    StringBuilder result = new StringBuilder(length)\n\n    \/\/ Generate the random string\n    for (int i = 0; i &lt; length; i++) {\n        result.append(chars.charAt(random.nextInt(chars.length())))\n    }\n\n    return result.toString()\n}\n<\/code><\/pre>\n\n\n\n<p>This method accepting an integer called \u201clength\u201d, so that if i want a random string of 5 character, i can call the method and throw the value of \u201c5\u201d as the parameter. Also, before the random string method, I have put a tag called \u201c@Keyword\u201d, this will be useful in our next section. And remember that this method returns a string value.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Call the custom keyword in our Test Case<\/h2>\n\n\n\n<p>To call the custom keyword, in manual view, click on the arrow down \u201cAdd\u201d, and choose \u201cCustom Keyword\u201d. Here the syntax for calling the Custom Keyword is inputted, and because we have put the tag \u201c@Keyword\u201d on top of the method, we can find the name of the method to select.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"397\" height=\"105\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-1-5.png\" alt=\"\" class=\"wp-image-80\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-1-5.png 397w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-1-5-300x79.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/figure>\n\n\n\n<p>We should switch over to Script view now to have more control over what\u2019s going on. We need some variables to help the custom keyword run.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int LengthOfString = 5\n\nString StringOf5RandomChars = CustomKeywords.'utilities.Randomization.generateRandomString'(LengthOfString)\n\nprintln(\"Random Email: \" + StringOf5RandomChars + \"@abc.com\")\n<\/code><\/pre>\n\n\n\n<p>Because the method is returning a string result, I created a string called \u201cStringOf5RandomChars\u201d 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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"193\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-2-4-1024x193.png\" alt=\"\" class=\"wp-image-81\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-2-4-1024x193.png 1024w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-2-4-300x57.png 300w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-2-4-768x145.png 768w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-2-4.png 1392w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>With this custom keyword, I can create a unique and completely random email, by adding the \u201c@abc.com\u201d 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.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using ChatGPT to Help Create a Custom Keyword<\/h2>\n\n\n\n<p>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\u2019s help to create a method that can generate random numbers. So this is my prompt to ChatGPT:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>help me create a method that can generate a random integer, with the parameter to specify how many numbers, in Groovy<\/p>\n<\/blockquote>\n\n\n\n<p>ChatGPT\u2019s answer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.security.SecureRandom\n\nint generateRandomInteger(int digits) {\n\t\t\/\/ Ensure the number of digits is at least 1\n\t\tif (digits &lt;= 0) {\n\t\t\tthrow new IllegalArgumentException(\"Number of digits must be greater than 0\")\n\t\t}\n    \n\t\tSecureRandom random = new SecureRandom()\n\n\t\t\/\/ Calculate the minimum and maximum values for the desired number of digits\n\t\tint min = (int) Math.pow(10, digits - 1)\n\t\tint max = (int) Math.pow(10, digits) - 1\n\n\t\t\/\/ Generate the random integer within the range\n\t\treturn random.nextInt((max - min) + 1) + min\n\t}\n\n\/\/ Example usage:\nint desiredDigits = 5\nprintln generateRandomInteger(desiredDigits)\n<\/code><\/pre>\n\n\n\n<p>So we can use ChatGPT\u2019s answer for our custom keyword. If there\u2019s any error just copy and paste the error to ChatGPT and let them help. Here we can see that ChatGPT\u2019s custom keyword works. This can be used for a more advanced examples.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"188\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-3-2-1024x188.png\" alt=\"\" class=\"wp-image-82\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-3-2-1024x188.png 1024w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-3-2-300x55.png 300w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-3-2-768x141.png 768w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-3-2.png 1396w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-en","category-katalon-auto"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":1,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions\/83"}],"wp:attachment":[{"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}