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:
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.

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.



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