Custom Keywords for Custom Functions

A custom keyword is similar to a function. It takes in information and returns a single result. It would be a repeatable block of code used in several different test cases. In my case, I’ve set up custom keywords to create Contact information.
As outlined previously, the site makes use of Contacts and making them a little more real world would be ideal. I first created a Test Case that set the value of several Global Variables. That worked quite well, and to be honest, I don’t see anything wrong with it. But, as an exercise, I wondered if the same could be accomplished using a Custom Keyword. Turns out it can.
The steps for setting up a Custom Keyword can be found here:
https://docs.katalon.com/display/KD/Define+custom+keywords
And while that shows exactly how to do it, it doesn’t really explain why. But, after a few experiments, things are little clearer to me. To that end, here is some code for my Custom Keyword – getPhoneNumber()
To give context, I created a new package called, createUserDetails.
My Keywords are stored in, contactInformation.
The first step is to mark the code as a Keyword.
The next is to give the Keyword a name.
After that is the code the Keyword will run when called. Keep in mind, it can only return a single value.

@Keyword
def getPhoneNumber() {
//Generate phone number
int areaCode=Math.abs(new Random().nextInt(799)) + 200;
int numPrefix=Math.abs(new Random().nextInt(899)) + 100;
int numSuffix=Math.abs(new Random().nextInt(9000)) + 1000;
String phoneNumber=String.valueOf(areaCode) + String.valueOf(numPrefix) +String.valueOf(numSuffix)
return phoneNumber
}

For my case, I need a name, company, street address, phone, etc. To that end, I created Custom Keywords for the following:

/*This package is used to generate Customer Keywords to populate Contact and Prospect Information
* The keywords used are:
* getStateAbbr() – Pick a 2 letter state abbreviation
* getPhoneNumber() – Generate a 10 digit phone number
* getCityName() – Pick a City Name
* getStreetName() – Generate a street address such as 1313 Mockingbird Ln
* getProperName() – Pick a Full Name for the user
* getCompanyName() – Pick a Company Name for the user
* createEmail(String companyName, String fullName) – Takes 2 arguments, companyName and fullName to create a “valid” email address for the user
* getZipCode() – Generates a random 5 digit zip code
*/
Those are defined in the Custom Keyword package. With that done, within the Test Case, the Keywords are called in the following manner.

userFullName=CustomKeywords.'createUserDetails.contactInformation.getProperName'()
log.logWarning(userFullName)

streetAddress=CustomKeywords.'createUserDetails.contactInformation.getStreetName'()
log.logWarning(streetAddress)

cityName=CustomKeywords.'createUserDetails.contactInformation.getCityName'()
stateAbbr=CustomKeywords.'createUserDetails.contactInformation.getStateAbbr'()
zipCode=CustomKeywords.'createUserDetails.contactInformation.getZipCode'()
log.logWarning(cityName + ' ' + stateAbbr + ', ' + zipCode)

Each variable will be set to the result of the Custom Keyword. In the first example, userFullName will be set to the text that makes up a valid looking first and last name.
The getStreetName() Keyword does several things and put several pieces of information together, even though it only returns a single answer. It generates a number for the address, name of the street, adds a suffix, then concatenates all the pieces together to create a single text string like 1313 MockingBird Ln.
Like a Test Case, a Custom Keyword can have parameters passed to it. In my code, I create a name and company for a user. To make it even more valid, I pass those as variables to my createEmail Keyword.

emailAddress=CustomKeywords.'createUserDetails.contactInformation.createEmail'(companyName, userFullName)

The emailAddress Keyword takes the name and parses it into two strings. The “.” is put between the names. The company and the “@” are appended to the name. A domain suffix is added and the string is converted to lowercase.
Granted, the whole thing is less than 15 lines of code, but it’s 15 lines of code I only have to type and update once. From now on, a single line does all that work for me. And if I want to add suffixes or change my methodology, it’s all in one place. That’s pretty powerful.

Other articles of interest:

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments
  • Working with Dates and Date Formatting in Katalon Studio (4)
    • Don Pedro: import groovy.time.TimeCategory currentDate = new Date() println currentDate use( TimeCategory ) { after30Mins = currentDate + 30.minutes } println after30Mins https://stackoverflow.com/ques tions/21166927/incrementing...
    • ricky julianto nugroho: hay can u gave me an example for plus a hour in the time ? example : now is 15 pm i want to print 16 in the text thank you
    • Don Pedro: formattedDate=mydate.format(&# 8220;MMM”) – Nov formattedDate=mydate.format(&# 8220;MMMM”) – November
    • Ann: This was a huge help. Thank you ! Do you have something for capturing the month spelled out? and abreiveiated? Example November Nov Thanks !
  • Boostnote for Code Snippets (1)
    • Manual: Hi Don, Regarding the code snippets and examples that you have created. Is there any GitHub repo or something that can be shared? Thanks
  • Using TypeIt4Me with Katalon Studio (3)
    • Manual: That’s great thanks for sharing.
    • Don Pedro: Added a screenshot to show some examples of how I have shortcuts configured for Groovy code. TypeIt4Me supports groups, so you can create shortcuts related to Groovy, Selenium, and other topics to keep them organized. You can...
    • Manual: Hi Don, I like the idea of using the TypeIt4Me for scripting test code. Could you please share how I can create. I also like the idea of creating code templates to use with code editors. Thanks, Manual
  • Another success with Katalon Studio (2)
    • Don Pedro: Since Katalon is free, a very practical demonstration of what it can would be to create the code needed to fill in forms. That was one of my first tasks as Katalon could fill in a lengthy form in seconds versus the minute or...
  • How To Disable the Quicken Registration Prompt (18)
    • Susan Long: I have Quicken personal 2012 which I only use to enter and retrieve data. It suits my needs perfect. Now they have blocked my access and want me to re register. I don’t want to upgrade as it I am comfortable with this...
    • Stewart: I have been using Quicken 200 basic since it was introduced but I now have a new PC using windows 10 and can’t get rid of the registration prompt. I have tried the above control/shift trick but to no avail. Any ideas
    • Dave: Works for Quicken 2007 too. Thanks, Gina!
    • Charlie Fellenbaum: Yay, thanks, and that worked for Quicken 2006 as well.
  • Conditional Statements – IF .. ELSE IF in Katalon Studio (1)
    • Allyson: I used this : elementVisible=WebUI.verifyEle mentPresent… but it failed because “FAILED because (of) Unable to verify object ‘Object Repository/Homepage/b_vaolopho c’ is present (Root cause:...
  • Quasi Performance/Load Testing with Katalon Studio (1)
    • Anthony: I have done this with 100 plus machines at the same time and it worked well and provided some useful feedback.