Katalon Studio – Creating Objects

The first thing to understand with Katalon Studio is that it’s object oriented. That means that every item you want to interact with on the page should have an object created for it. For example, if you want to enter text on a form, that input field would be an object referencing the path to find it. If you want to click the OK or Submit button on a page, there should be an object with the path to the button.
At first, that may seem like extra work and perhaps even a waste of time. But in the long run, it’s very beneficial. You have to reference the object anyway, but this method gives it a friendly name and again, if the path to the object changes, it only needs to be changed in one place.
As an example, let’s say you wanted to enter text into a form. Here is an input field on a real Inventory Search page I’m working with.
image

Using Katalon, the input field has a referenced of:
css=input.form-control
//div[@id=’wrap’]/div/div/div/div/input
The first is the CSS method to name the object, while the second is the XPATH notation. In Katalon Studio, under the Object Repository folder, I make a new Test Object and call it:
input-Inventory Search
I then add either the CSS reference, the XPATH reference or both. You can toggle the Detect Object By option as to which reference should be used.

image

With that input field now defined, I can enter the name of the product into the Inventory Search using the following command:
WebUI.setText(findTestObject(‘Page_/Search Inventory/input-Inventory Search’), ‘shingle saw’)
The line above is broken into several parts:
WebUI.setText – The command to enter text into a field
findTestObject(‘Page_/Search Inventory/input-Inventory Search’) – The page to the location of the object within the Object Repository. This is equivalent to the folder and pathname of a file.
‘shingle saw’ – The actual text I want to pass to the setText command. This enters the text, "shingle saw" into the input field.
This is a simple example, but demonstrates the idea.
Now let’s take this New Prospect Form:
image
Each field would be an object. There would also be an object definition for the New Prospect button itself, the GO button, the Save Button and the Cancel link. Within the code I would Click the button reference or setText for the input field reference. And to keep things organized, I would make a folder called Prospect Search in my Object Repository and place all the objects for the page in that folder.
Here is the New Prospect folder within Katalon Studio. Each object maps to a field on the form with the XPATH to the input field or button.
image
To fill in the form, I would use the following code:
//The following fills in the fields of the form
WebUI.click(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/btn-Create New Prospect Button’))

WebUI.setText(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-Company Name’), ‘Amazing New Company’)

WebUI.setText(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-First Name Last Name’), ‘john Smith’)

WebUI.setText(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-Address Line 1’), ‘1313 Mockingbird Ln’)

WebUI.setText(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-City’), ‘Charlotte’)

WebUI.setText(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-State’), ‘NC’)

WebUI.setText(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-Zip Code’), ‘28203’)

WebUI.setText(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-Phone Number’), ‘9805551212’)

WebUI.setText(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-Email Address’), ‘testcustomer@domain.com’)

WebUI.click(findTestObject(‘Page_/Prospect Page Objects/New Prospect Fields/Prospect-Save Button’))
The first link Clicks the button for New Prospect.
The following lines enter information for each field corresponding to name, address, city, state, etc.
The last line in the code clicks the Save button.
The code reads far better and makes more sense than a slew of references like:
//div[@id=’add-prospect’]/form/input[3] or
name=add1
In my projects, I have objects defined for
Buttons
Tables cells
Links
Headers
Input fields
Labels
Checkboxes
Links
And just about everything else.
The main way to reference an object is using:
ID
Xpath
Name
CSS
As a final comment, it took me forever to find how to reference items in a table using variables. For example, I have the following "table" of numbers. I want to read the text from each cell and do something with it. I don’t have to define each "cell" as an object. I can substitute a ‘variable’ into the XPATH to make it more dynamic.
image
The XPATH for the table looks like: //div[@id=’wrap’]/div/div[3]/div[${Variable}]/div/p
Note the Variable reference in the braces. This should look familiar for those who used the Selenium IDE to reference variable names. It can be any name, I simply chose the word Variable.
To read through the 9 numbers that make up the cells, I can use the following code:
for (loop = 1; loop <= 9; loop++) {
    dailySalesDetails = WebUI.getText(findTestObject(‘Page_/Sales Dashboard/Daily Details/Daily Sales Details Sales Header’,[(‘Variable’) : loop]))
The getText should look familiar, but it’s the part at the end that creates the magic. It passes the value of ‘loop’ which is my counter for the FOR loop, to the defined object at the location where Variable is defined. In this case, it’s for the DIV.
As the loop progresses, the line above would become:
//div[@id=’wrap’]/div/div[3]/div[1]/div/p
//div[@id=’wrap’]/div/div[3]/div[2]/div/p
//div[@id=’wrap’]/div/div[3]/div[3]/div/p

//div[@id=’wrap’]/div/div[3]/div[9]/div/p
The object oriented aspect of Katalon is actually extremely handy. It makes the code more readable and if something changes, I don’t have to change it in every test. Every test referencing the object will automatically use the new path.
Variables can be added to XPATH references which makes it efficient for reading tables and columns of data. This was a buried nugget of information, so hopefully others will find it useful.

Other articles of interest:

Leave a Reply

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

Recent Comments
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (6)
    • Saranya: Good One. Could you plz share link to access all your katalon related blogs. ThankQ
    • Rodrigo Calabretta: I’m using the KeywordUtil.markFailed or KeywordUtil.markError and my test stops is being shown as Error and If I use in the @afterTestCase the testCaseContext.getTestCaseSta tus() to show the status test case...
  • Working with Dates and Date Formatting in Katalon Studio (14)
    • Don Pedro: That could be a little tricky. First, is the text/format always going to be the same? You will need to do some parsing. Separate the first but of text at 2019. After that, split the text again to get rid of Central Standard...
    • Don Pedro: It should be of the same Date type as today. You could then do some calendar trickery with Calendar /*Calendar c = Calendar.getInstance(); //c.set(Calendar.MONTH, 1) //Set the month, 0=January, 11=December println...
    • josh: I’m testing a page with a string like “Updated November 21, 2019 16:25:32 PM Central Standard Time.” How might I capture the date and time and compare it to current time to verify that the update time was within...
    • zakir hussain: today = new Date() lastWeek = (today – 1.week) can u please help me in understanding what is the return type of lastweek? and I wanted to set nextyear date how can i do it
    • ALLAN FORD: Useful !
    • Don Pedro: Not sure I understand what you are trying to do.
    • Don Pedro: formattedDate = mydate.format(“M/dd/yyy& #8221;)
  • Setting up Environment Profiles in Katalon Studio (2)
    • Don Pedro: Not quite sure I understand. The term “Execution Profile” is normally used now, but they are still GlobalVariables and are created in the same way for version 7 as previous. You make a new execution profile, then...
    • Aparna: Hello, Your documentation is excellent and has helped me understand many things. But this article above seems outdated and I need help in creating my own Global variables and all the documentation out there seems to be point to...
  • Here is my drive cluster (2)
    • Don Pedro: While I still have those drives, the drive cluster looks nothing like that now. Almost all of my external drives have had the cases removed and they have been installed into a series of MediaSonic 8 Bay Drive Enclosures. I...
    • Marin Boucher: Hi! Verry funny blog with “go to the point” on many post! I spent good time reading some of your post. Regarding this one about NAS, “many NAS” in fact, I would be curious to see a screenshot of...
  • How To Disable the Quicken Registration Prompt (27)
    • debra: didn’t work for me… my 2007 – which I am keeping on my older mac.
  • Adding entries to an open Excel spreadsheet during runtime (1)
    • IanG: Hi: Seems like nobody else has described (in detail) how Katalon can be configured to test an API by reading the first entry from a multi-row multi column XLSX spreadsheet, executing a test, writing the result to another column (or...