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

WebUI.openBrowser('http://demoaut.katalon.com/')

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

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

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

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

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

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

//Uses by.id
def CommentFieldId = 'txt_comment'
TestObject CommentField3 = WebUI.convertWebElementToTestObject(driver.findElement(By.id(CommentFieldId)))
WebUI.setText(CommentField3, 'ID Comment')
WebUI.waitForJQueryLoad(20)
4 Likes

The Java Code Convesions "9. Naming Conventions " states

Variables

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")')

or

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