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
  • How To Disable the Quicken Registration Prompt (21)
    • Susan Long: I bought my quicken disc in the beginning and it came with a registration number. I rang the helpline and they gave me the code to put in and talked me through it. It you downloaded your version then you don’t own it...
    • Peter: You might consider running your old version of Quicken on an ancient computer. This is what I have done for years. The newer versions are fraught with problems- criminal, in my opinion. One version made mathmatical errors when...
    • Roslyn Chamberlain: Can I stop the countdown in quicken 2001 says only 8 sessions left. and what will happen after?
  • What is Katalon Studio? A Distro of Selenium, Groovy and Eclipse (1)
    • Mahesh: Looking for more posts on katalon studio.your katalon stuffs are always exiting
  • Simple wildcard searches for pattern matching (2)
    • Don Pedro: For that scenario it seems .contains would be your choice. For example, variable.contains(‘amazo n.com’) to see if the url had amazon.com. In that case, www.amazon.com, forum.amazon.com, retail.amazon.com would all...
    • Jony: Hi, How can I use a wild card to assert a URL is the one I want. I just want to verify the domain ==expected but not anything after it. Tried * but not working and only works when I have full URL.
  • Create a Dynamic Object at Runtime (2)
    • Saish: How to add shadow root parent to this runtime object..
    • Jeremy Brien: I appreciate this! I saw this post on LinkedIn this morning and was able to find a use case for it! I found that defining my xpath with an iterable variable allows me to loop through and capture text from tables created...
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (2)
    • Don Pedro: That would be things like: log.logWarning(‘The import date listed on the site is: ‘ + importDate) log.logWarning(‘The value from the site is: ‘ + salesFigure) log.logWarning(‘Filter Results for ‘ +...
    • Prashant Pednekar: Thanks for the informative article but can we also put some variable values to make it more exciting.
  • Setting up a repeatable Search Method in Katalon Studio (1)
    • RJ: Thanks a lot! This is very helpful 🙂
  • Filling forms with random numbers in Katalon Studio (3)
    • Patrick Clough: We created a handy random string generator custom keyword. The method takes in a string for the type of string you want, and an integer for the length. Looks like this: static String RANDOM_ALPHANUM =...
    • Don Pedro: randomNumber is the variable that holds the result of generating a new random number. Depending on how things work on your site, you might need to convert the number to a String before it’s recognized correctly....
    • Seth: When you are setting up randomNumber is this a new keyword or a new variable? I am trying to randomly generate unique socials and can’t seem to connect my generator to my Set Text.
  • Working with Dates and Date Formatting in Katalon Studio (5)
    • Ann: THANK YOU ! That worked perfectly !