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