How to call the "Creation of Test Object in Object Repository in Runtime" keyword

I found this keyword but I am not sure how to call it.
https://docs.katalon.com/katalon-studio/docs/creation-of-test-object-in-object-repository-in-runtime.html

/**
* Construct a Katalon-compatible TestObject in memory.
* @param css (String) The CSS selector used to find the target element.
* @return (TestObject) The constructed TestObject. 
*/

//I changed 'css' to 'xpath' for my purposes
@Keyword
static TestObject makeTO(String xpath) {
	TestObject to = new TestObject()
	to.addProperty("xpath", ConditionType.EQUALS, xpath)
	return to
}

Test-Case:
I have created 2 runtime TestObjects (MakeAppoinment & UserName)
My question is, how can I create the objects using the keyword from above?

//This case uses http://demoaut.katalon.com/ for the AUT
import com.kms.katalon.core.testobject.ConditionType as ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

//Start of test
WebUI.openBrowser('')
WebUI.navigateToUrl('http://demoaut.katalon.com/')

//Creates TestObject in memory
TestObject MakeAppoinment = new TestObject()
MakeAppoinment.addProperty('xpath', ConditionType.EQUALS, '//*[@id="btn-make-appointment"]')
WebUI.click(MakeAppoinment)

//Creates TestObject in memory
TestObject UserName = new TestObject()
UserName.addProperty('xpath', ConditionType.EQUALS, '//*[@id="txt-username"]')
WebUI.setText(UserName, 'John Doe')
TestObject MakeAppoinment = makeTO('//*[@id="btn-make-appointment"]')

Thanks for the fast reply :slight_smile:

WebUI.openBrowser('')

//Creates to in memory
WebUI.navigateToUrl('http://demoaut.katalon.com/')

//TestObject MakeAppoinment = new TestObject()
//MakeAppoinment.addProperty('xpath', ConditionType.EQUALS, '//*[@id="btn-make-appointment"]')
TestObject MakeAppoinment = makeTO('//*[@id="btn-make-appointment"]')
//WebUI.click(MakeAppoinment)

My result:

05-21-2019 12:29:21 PM Test Cases/03 Sample Cases/01 Create new Katalon Object at runtime

Elapsed time: 4.497s

Test Cases/03 Sample Cases/01 Create new Katalon Object at runtime FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: Script1558457802259.makeTO() is applicable for argument types: (java.lang.String) values: [//*[@id="btn-make-appointment"]]
Possible solutions: wait(), main([Ljava.lang.String;)
	at 01 Create new Katalon Object at runtime.run(01 Create new Katalon Object at runtime:11)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1558466959995.run(TempTestCase1558466959995.groovy:21)

It’s not clear from your code if you made a keyword (file and class) and then imported it.

public class my_keywords {
 
  static TestObject makeTO(String xpath) {
    TestObject to = new TestObject()
    to.addProperty("xpath", ConditionType.EQUALS, xpath)
    return to
  }
}

In your test case:

import static my_keywords.*
// import others

WebUI.openBrowser('')

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

TestObject MakeAppoinment = makeTO('//*[@id="btn-make-appointment"]')

// ...

This now works… Thank you Russ!

Keyword

package tools
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject

public class MyKeywords {

	static TestObject makeTO(String xpath) {
		TestObject to = new TestObject()
		to.addProperty("xpath", ConditionType.EQUALS, xpath)
		return to
	}
}

Test Case:

import static tools.MyKeywords.*
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

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

// Make Appointment
TestObject MakeAppoinment = makeTO('//*[@id="btn-make-appointment"]')
WebUI.click(MakeAppoinment)
// UserName
TestObject UserName = makeTO('//*[@id="txt-username"]')
WebUI.setText(UserName, 'John Doe')
//other stuff
WebUI.delay(5)
WebUI.back()
2 Likes