Is there a method to create an Object in the Object Repository using Groovy

I want to create objects in the Object Repository ‘on the fly’ using groovy script. Simply creating an object and assigning Properties to it does not appear to work. If I do it like this it does:

TestObject to = findTestObject(‘Object Repository/GenericObject’)
to.addProperty(“id”, ConditionType.EQUALS, ‘foobar’)

But the Generic Object must exist in the Object Repository with at least one configured property or an error is thrown.

In documentation you suggest:
myNewObject = new TestObject(“TheObjectName”) and adding properties to it, this is resulting in a null object.

Can you provide an explicit example of creating an object & adding properties to it ?

Thank you,
Mark

I did have to adjust the punctuation for it to run in my environment:
WebUI.openBrowser(’’)
WebUI.navigateToUrl( ‘katalon.com’ )
TestObject to = new TestObject(‘dynamic’)
to.addProperty(“class”, ConditionType.EQUALS, ‘btn kat-btn-default btn-lg’)
to.addProperty(“tag”, ConditionType.EQUALS, ‘a’)
to.findProperty(“class”).setActive(true)
WebUI.click(to)
WebUI.closeBrowser()

But it works well, Thank you for all of your help!

This is not working for me, So I set up a simple test:
WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘https://www.katalon.com/’)

WebUI.click(findTestObject(‘mpjunk/Page_Katalon Studio/a_Download now’))

WebUI.closeBrowser()

This works Great, clicks the download button.

So I tried this:

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘https://www.katalon.com/’)

TestObject to = new TestObject(“dynamic”)

to.addProperty(‘class’, ConditionType.EQUALS, “btn kat-btn-default btn-lg”)

to.addProperty(‘tag’, ConditionType.EQUALS, “a”)

WebUI.click(findTestObject(‘to’))

WebUI.closeBrowser()

I have tried different values for “dynamic” & “to” but no luck.
it does appear to add the object and values in the log but when used it fails.

Errors and Warnings:

warning - Test object with id ‘Object Repository/to’ does not exist
warning - Test object with id ‘Object Repository/’ does not exist

error - Unable to click on object (Root cause: java.lang.IllegalArgumentException: Object is null)
error - Test Cases/mptest2 FAILED because (of) Unable to click on object (Root cause: java.lang.IllegalArgumentException: Object is null)

If you could please make a working example, I would appreciate it.

Thanks Mark

Hi there,

You don’t need to use findTestObject when you use your created test object as test object parameter in built-in keyword. You only need to pass in the parameter without using findTestObject.You will also need to set a property as active one for detection. Below is an example:

WebUI.openBrowser(")

WebUI.navigateToUrl('https://www.katalon.com/')

TestObject to = new TestObject("dynamic")

to.addProperty('class', ConditionType.EQUALS, "btn kat-btn-default btn-lg")

to.addProperty('tag', ConditionType.EQUALS, "a")

to.findProperty('class').setActive(true)

WebUI.click(to)

WebUI.closeBrowser()

1 Like

Hi there,

You can create TestObject ‘on the fly’ using the following example:

TestObject to = new TestObject("dynamic")

dynamic.addProperty("id", ConditionType.EQUALS, 'foobar')

The ‘dynamic’ value is the test object’s ID you want to create. You can freely use any other value as you want. This solution doesn’t require you to pass in existing test object in Object Repository.

I am trying to use same with mobile using xpath… that is creating object run time with passing xpath to it and then trying to gettext() from it… but its no working for me…
here is my keyword
def static TestObject makeTO(String xpath) {
TestObject to = new TestObject(“Note”)
to.addProperty(“xpath”, ConditionType.EQUALS, xpath)
to.findProperty(“xpath”).setActive(true)
Mobile.getText(to, 5)
return to
}

And calling this keyword in another keyword below :
(new insight.VerifyObjectRepository()).makeTO("//*[@class = ‘android.widget.TextView’ and (text() = ‘GlobalVariable.NoteValue’)]")

Here i am passing text property value from Global Variable.
I am getting error as :
Reason:
com.kms.katalon.core.exception.StepFailedException: Object Note not found
at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:37)
at com.kms.katalon.core.mobile.keyword.internal.MobileKeywordMain.stepFailed(MobileKeywordMain.groovy:40)