Sample test case for creating Katalon compatible Test Objects in memory Tip

This sample test case logs into ‘CURA Healthcare Service’ using Katalon compatible TestObjects created in memory. Copy and paste into any Katalon Studio test case and execute.

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI


WebDriver driver = DriverFactory.getWebDriver()
def MakeAppointmentXpath = '//*[@id="btn-make-appointment"]'
TestObject MakeAppointment = WebUI.convertWebElementToTestObject(driver.findElement(By.xpath(MakeAppointmentXpath)))

def UserNameXpath = '//*[@id="txt-username"]'
TestObject UserName = WebUI.convertWebElementToTestObject(driver.findElement(By.xpath(UserNameXpath)))
WebUI.setText(UserName, 'John Doe')

def PasswordxPath = '//*[@id="txt-password"]'
TestObject Password = WebUI.convertWebElementToTestObject(driver.findElement(By.xpath(PasswordxPath)))
WebUI.setText(Password, 'ThisIsNotAPassword')

def LoginxPath = '//*[@id="btn-login"]'
TestObject Login = WebUI.convertWebElementToTestObject(driver.findElement(By.xpath(LoginxPath)))

//Uses by.xpath 
def CommentFieldxPath = '//*[@id="txt_comment"]'
TestObject CommentField = WebUI.convertWebElementToTestObject(driver.findElement(By.xpath(CommentFieldxPath)))
WebUI.setText(CommentField, 'Xpath Comment')

//Uses by.cssSelector
def CommentFieldCss = '#txt_comment'
TestObject CommentField2 = WebUI.convertWebElementToTestObject(driver.findElement(By.cssSelector(CommentFieldCss)))
WebUI.setText(CommentField2, 'CSS Comment')

def CommentFieldId = 'txt_comment'
TestObject CommentField3 = WebUI.convertWebElementToTestObject(driver.findElement(
WebUI.setText(CommentField3, 'ID Comment')

The Java Code Convesions "9. Naming Conventions " states


Except for variables, all instance, class, and class constants are in mixed case with a lower case first letter.

Therefore I would advise one thing.

def MakeAppointmentXpath = '//*[@id="btn-make-appointment"]'

should rather be

def makeAppointmentXpath = '//*[@id="btn-make-appointment"]'

if you respect the Conventions.

When do you use object spy, for global or common objects? I am so happy you told me about using objects in memory. If you do this is there a way you save the objects in memory you create so someone else can use them as well?

It depends on how you create the objects in memory. If you create them in-line of your code, then that is the only place they exist. Unless you share that “code” like a called Test Case, then they cannot be shared. Also, if you use the object multiple times throughout your Test Case, then there will be an issue with maintenance of your pathway should it change over time. Then, maybe use the OR or a called subroutine/method that returns the element’s pathway. The pathway is only in one location so maintenance is easier–but what is the difference between this and the OR.

If you create the pathway for the object in a Keyword class, then others that share the Keyword can create the object in memory just by using the pathway like a called subroutine/method.

yourItem = com.Tools.creatTestObject('id("yourTestElement")')


yourItem = com.Tools.creatTestObject(getTestElement())

public String getTestElement() {
  return 'id("yourTestElement")'

Thanks Mike @grylion54

when I get to this i will for sure let you know, It may not be until a few months but I will.

Thank you