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
  • A Try Catch example in Katalon Studio (3)
    • Venkatesh: Hi Don Pedro Can you share me sample project ….which consist of basic to advanced stuff…If your github or gitlab url aso present please share… As I am beginer to this I need to learn lot
    • Don Pedro: Hi Lucas. Thank you for the kind comments. I would be happy to help. Let me know how I could be of assistance.
    • Lucas: Thanks for sharing a lot of Katalon tips and tricks. The Katalon team highly appreciate your support and feedback. To make Katalon Studio better and be the best free automation solution for testing community, we wish to coordinate...
  • How To Disable the Quicken Registration Prompt (13)
    • Neil Murphy: I discovered that early on. I’m using 2001.
    • Jeremy: Thank you so so so much. I am still using Quicken 2010 because Intuit didn’t want to properly support users abroad anymore (never mind that they only link to US banks for transaction downloads; they didn’t want my...
    • Richard: I have Quicken 2016 Home & Business and when I use Shift + CTRL I can’t see any Tools option; All I can see is the original menu items of File & About. Might you be able to offer any advice?
    • Jack Woltz: Worked great but now I get update nag which is worse
  • Securely storing passwords and login details with Set Encrypted Text in Katalon Studio (1)
    • Agus: Hi Don, Is there a way to set the encrypted text to be used in the ‘WebUI.authenticate̵ 7; method? Basically I am trying to encrypt the password that is passed to the ‘WebUI.authenticate̵ 7; method. Thank you.
  • Katalon Studio – Manual View – The start of a test script (2)
    • ben: Hello Don Great Website. Keep up the work. I learnt a lot. cheers Ben.
    • Lucas: Hi Don Pedro, Thanks for sharing a lot of great articles related to Katalon Studio and Selenium. I am Lucas from Katalon team. From your point of view, we’ve learnt alot to improve our product and add more valuable features...
  • Another success with Katalon Studio (1)
    • Mahesh Joshi: Hi Don, I just started following your blog in regards to katalon. I want to introduce katalon studio at my work we do manual testing at work at the moment. What approach I should use for proof of concept with katalon for...
  • What went wrong with Crime City? (34)
    • G D: One thing I have noticed is the awkwardness of items and the attack points and how you can earn them. I was pretty pathetic with attack points and then I played that limited time boss and got weapons that had attack points in the...
  • Disable the Quicken 2012 Registration Prompt (1)
    • Scott: Thanks this worked great and is better then having to upgrade to a subscription!
  • Time to throw away Freemake (1)
    • Rob Crombie: Which program are you using instead, now ?
  • How To Download Movies From Hulu Using Freemake Video Downloader (1)
    • Doomups: I often use allavsoft to download Videos from Hulu on Mac and Windows. And it also can batch and Direclty Download Hulu Videos to MP4, WMV, MOV, AVI, FLV, etc.