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:

Calling Another TestCase in Katalon Studio

One of the many great features of Katalon Studio, is the ability to call other test cases using the CallTestCase command.

This would make the test case act like a procedure, where it can be called to execute a series of steps, then return to the main program after the work is done.
This means the automation code can be more modular, more dynamic and with the right planning, far more code can be reusable.
I’ve started to bring this into my own tests as seen by the example of reading the tabs of the customer profile. Here is another example to toggle the status of a user.
I made two test cases, one to “lock” a user and another to “unlock” a user. I can then read the text of the button and call to the appropriate test case.

//Call the appropriate function to toggle the status of the user
if (buttonText == 'Unlock') {
log.logWarning('Budget is Locked, running Unlock code')
WebUI.callTestCase(findTestCase('Admin Tasks/Unlock User'), [:], FailureHandling.STOP_ON_FAILURE)
} else {
log.logWarning('Budget is Unlocked, running Lock code')
WebUI.callTestCase(findTestCase('Admin Tasks/Lock User'), [:], FailureHandling.STOP_ON_FAILURE)

A test case can also take parameters when it’s called. If you had another test case that acted on a Name and ID, those would be passed as part of the command:

WebUI.callTestCase(findTestCase('Tasks/LogNameAndID'), [('userID') : '1091504 ', ('userName') : 'Bob Smith'], FailureHandling.STOP_ON_FAILURE)

By using CallTestCase, test cases can be called when needed, not just in a linear fashion. This means they can react to what’s on the page and skip over tests that aren’t applicable. A test can be written once and used multiple times.
As another example, I have a contact generation test case. We have several Contact forms and instead of putting the same text in over an over again, I created a test case that uses Lists, text parsing and random numbers to generate a somewhat unique user details.
I have a list for first names, last names, company and state. I use random numbers for the zip, phone, and street number. When called, a name, address, phone, etc are generated and assigned to the Global Variables I have defined for the project.
It would have code like the following:

def states=['AL','AK','AZ']
def cities=['Dentsville','Woodcreek','Las Lomitas']
//Generate phone number
areaCode=Math.abs(new Random().nextInt(799)) + 200;
numPrefix=Math.abs(new Random().nextInt(899)) + 100;
numSuffix=Math.abs(new Random().nextInt(9000)) + 1000;
GlobalVariable.phoneNumber=String.valueOf(areaCode) + String.valueOf(numPrefix) + String.valueOf(numSuffix)
//Create an email address from first name, last name and company name
//Pick a city
rndCity=Math.abs(new Random().nextInt(cities.size()));

To create a new user, I call the Contact Generation test case then set the text of the input fields to the values of the variables. It works quite nicely.
As time goes by, I will try to use the CallTestCase more frequently and work to my test cases more modular. It’s already starting to happen in a couple of places and it’s a very powerful tool.

Other articles of interest:

A Try Catch example in Katalon Studio

Since Katalon has the markError and markFailed commands, I don’t know if the Try Catch example is really needed. But, I did make use of it when trying to determine if a page loaded or generated an error.

After I click the link to load the page, I set the Try to the WebUI.verifyTextPresent(“Month To Date Sales Detail”, false). I need to determine if the text, Month To Date Sales Detail appears on the page. If it doesn’t, it means the page has an error and not only will the tests fail, but the data used for the page is invalid and needs manual investigation.

The Try is set to the text on the page. If that generates an error, the Catch statement then reads the Title of the page. This is written to the Warning log and I use the Throw command to break out of the test.

//Look for the text, Month To Date Sales Detail on the Page.
//If it's not there, there is a problem loading the page and the test should exit as the rest of the steps will fail'Page_/Sales Dashboard/Monthly Details/Monthly Sales Accordion'))
try {
 elementPresent=WebUI.verifyTextPresent("Month To Date Sales Detail", false)
catch (Exception e) {
 title = WebUI.getWindowTitle()
 log.logWarning('ERROR: The title of the Sales Detail Page is:=' + title)
 throw new AssertionError('ERROR: The Sales Detail Page did not load correctly', e)

While this works and handles the error, I’m not sure if it’s the best method I could have come up with. Would it have been better and more consistent to use the GetText command with the CONTINUE_ON_FAILURE and mark the test as failed if the text didn’t appear? This would be more in line with how I check to see which tabs are available on a customer profile. This test was written quite awhile before my tabs example. This was more a brute force validation method, whereas the other seems a little more elegant and dynamic.

Either way, here is a very simple Try Catch example to determine of a page loaded correctly.

Other articles of interest:

Reading Text from a Disabled Input Field in Katalon Studio

Reading text from a disabled input field is a very specific example, but it took a lot of searching to get the right command. I figured it could be a time saver if I put up a working example.
The table I’m working with looks like this:

As noted, I want to read the Plan value, but it’s disabled at the moment so a standard GetText doesn’t read the value. This is a dynamic table, so I’m using the WebDriver to manipulate it.
Once the import commands are in place, I can use the “getAttribute(“value”) to read the text from this input field.

//Read text from an input field where the text is disabled
plan = driver.findElement(By.xpath('//*[@id=\'customersTable\']/tbody/tr[1]/td[7]/input')).getAttribute("value")

Again, I’m using the wildcard //* to find my customersTable. I then reference the “input” field and use the getAttribute.
From there, I’m able to read $159,112 from the field. After that, it’s the standard commands to replace the $ and write it out to the log file.
This turned out to be a simple solution, but again, it took a lot longer to find and put the answer together than I would have expected. Hopefully, this saves time for someone else.

Other articles of interest:

Working with Tables using the Selenium WebDriver and Katalon Studio

While Katalon is capable of reading a table through an object declaration, it’s possible to do the same using the “Webdriver”, “List” and “findElement” commands. I use this method to count the number of rows in a dynamically generated table. It would be equivalent to the storeXPathCount from the Selenium IDE.

Many of the tables I work with are static, meaning the number of rows doesn’t change. For example, we have a table for months, that’s always 12. We have a table for categories, that’s always 10. However, we have a table for customers, and that’s always different.

I have a couple of tests where I need to know the number of customers in that table. To find out, I use the Webdriver to create a List of table rows, then get the Size of that List.

I’ve also used this method when working with Contacts. We don’t display a count for the number of Contacts, but I can use the WebDriver to get a count of the objects I’m interested in, make a new one, then count them again to make sure it’s increased by one.

These commands come from standard Selenium and can be found in many useful examples. To get started we need a few new import statements.

import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import org.openqa.selenium.By as By
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.annotation.Keyword as Keyword

Additional statements for logging would be:

import com.kms.katalon.core.logging.KeywordLogger as KeywordLogger

KeywordLogger log = new KeywordLogger()

Once those are declared, define the “Webdriver”

WebDriver driver = DriverFactory.getWebDriver()

With those in place, I can use the following code to count the number of items returned in my List.

WebElement Webtable = driver.findElement('sales_dash_table')) // Replace TableID with the Actual Table ID or Xpath reference
//Get the number of rows in the table and turn it into a List
List<WebElement> TotalRowCount = Webtable.findElements(By.xpath('//*[@id=\'sales_dash_table\']/tbody/tr'))
//Display the number of rows in the table for the given sales rep
log.logWarning('No. of Rows in the WebTable: ' + TotalRowCount.size())
//Loop through the table and output the information to Log File
//Read columns 1-8, assign each to a variable, then output the result to the Log File
for (int loop = 1; loop <= TotalRowCount.size(); loop++) {
 customerName= driver.findElement(By.xpath("//*[@id='sales_dash_table']/tbody/tr[" + (loop)+ "]/td[1]")).getText();
 log.logWarning('Customer Name:=' + customerName)
 sales = driver.findElement(By.xpath("//*[@id='sales_dash_table']/tbody/tr[" + (loop)+ "]/td[2]")).getText();
 log.logWarning('Sales Figure Amount:=' + sales)
 plan = driver.findElement(By.xpath("//*[@id='sales_dash_table']/tbody/tr[" + (loop)+ "]/td[3]")).getText();
 log.logWarning('Plan Value:=' + plan)

In my example, the WebElement is set to the variable WebTable, which corresponds to the ID “sales_dash_table”. The table I’m looking for has an ID of “sales_dash_table” defined within the CSS code. I want to look for that table, then count the number of rows in that table that are returned with the List command.

The By.xpath(“//*[@id=’sales_dash_table’]/tbody/tr” is the actual definition I want to count, in this case, the TR[].

The //* is a wildcard for all the div commands that are in front of the definition.

This is the standard Selenium method for manipulating a table and it works just the same in Katalon. In most cases I’m able to use an object definition, but when the table has an unknown length, it can be easily traversed with the WebDriver.

Other articles of interest:

Recent Comments