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
  • Working with Dates and Date Formatting in Katalon Studio (4)
    • Don Pedro: import groovy.time.TimeCategory currentDate = new Date() println currentDate use( TimeCategory ) { after30Mins = currentDate + 30.minutes } println after30Mins https://stackoverflow.com/ques tions/21166927/incrementing...
    • ricky julianto nugroho: hay can u gave me an example for plus a hour in the time ? example : now is 15 pm i want to print 16 in the text thank you
    • Don Pedro: formattedDate=mydate.format(&# 8220;MMM”) – Nov formattedDate=mydate.format(&# 8220;MMMM”) – November
    • Ann: This was a huge help. Thank you ! Do you have something for capturing the month spelled out? and abreiveiated? Example November Nov Thanks !
  • Boostnote for Code Snippets (1)
    • Manual: Hi Don, Regarding the code snippets and examples that you have created. Is there any GitHub repo or something that can be shared? Thanks
  • Using TypeIt4Me with Katalon Studio (3)
    • Manual: That’s great thanks for sharing.
    • Don Pedro: Added a screenshot to show some examples of how I have shortcuts configured for Groovy code. TypeIt4Me supports groups, so you can create shortcuts related to Groovy, Selenium, and other topics to keep them organized. You can...
    • Manual: Hi Don, I like the idea of using the TypeIt4Me for scripting test code. Could you please share how I can create. I also like the idea of creating code templates to use with code editors. Thanks, Manual
  • Another success with Katalon Studio (2)
    • Don Pedro: Since Katalon is free, a very practical demonstration of what it can would be to create the code needed to fill in forms. That was one of my first tasks as Katalon could fill in a lengthy form in seconds versus the minute or...
  • How To Disable the Quicken Registration Prompt (18)
    • Susan Long: I have Quicken personal 2012 which I only use to enter and retrieve data. It suits my needs perfect. Now they have blocked my access and want me to re register. I don’t want to upgrade as it I am comfortable with this...
    • Stewart: I have been using Quicken 200 basic since it was introduced but I now have a new PC using windows 10 and can’t get rid of the registration prompt. I have tried the above control/shift trick but to no avail. Any ideas
    • Dave: Works for Quicken 2007 too. Thanks, Gina!
    • Charlie Fellenbaum: Yay, thanks, and that worked for Quicken 2006 as well.
  • Conditional Statements – IF .. ELSE IF in Katalon Studio (1)
    • Allyson: I used this : elementVisible=WebUI.verifyEle mentPresent… but it failed because “FAILED because (of) Unable to verify object ‘Object Repository/Homepage/b_vaolopho c’ is present (Root cause:...
  • Quasi Performance/Load Testing with Katalon Studio (1)
    • Anthony: I have done this with 100 plus machines at the same time and it worked well and provided some useful feedback.