Creation of Test Object in Memory


This is a companion discussion topic for the original entry at https://docs.katalon.com/katalon-studio/docs/creation-of-test-object-in-object-repository-in-runtime.html
1 Like

It doesn’t touch the OR. It should say

Creation of TestObject in Memory at Runtime

The above code is out of date. TO will be created but will not be usable. Correct way:
EDIT: The code works correctly, my mistake. Another way to achieve the same would be:

TestObject createTestObjectXpath(String name, String xpath){
TestObject to = new TestObject(name)
to.getSelectorCollection().put(SelectorMethod.XPATH, xpath)
to.setSelectorMethod(SelectorMethod.XPATH)
return to
}

Similarly for CSS-based objects
P.S. You can avoid giving TO a name, but I highly suggest it as it will help when debugging (the printed error message will mention object’s name / id)

Not true.

    'Nav to Katalon "About us" page'
    WebUI.navigateToUrl("https://www.katalon.com/about-us/")
  
    WebUI.waitForPageLoad(10)
  
    WebUI.verifyLinksAccessible(["https://www.katalon.com/resources-center/blog/", "https://www.katalon.com/partners-and-contributors"])
    
    'Navigate to Katalon home page using makeTO'
    TestObject to = makeTO(".navbar-header a.navbar-brand")
    WebUI.click(to)
    
    'Check makeTO sent us to the correct page'
    String sURL = WebUI.getUrl()
    if(sURL != "https://www.katalon.com/") {
      KeywordUtil.markFailedAndStop("Something went wrong")
    }
        

QED

You can create a TestObject instance with XPath just similarly.

@Keyword
static TestObject makeTOwithXPath(String xpath) {
	TestObject to = new TestObject()
	to.addProperty("xpath", ConditionType.EQUALS, xpath)
	return to
}
1 Like

@ kazurayam
@ Russ_Thomas
I stand corrected. I confirm both of the above snippets working correctly.
It was a while ago that I tried to create TO like this but it would not work (katalon would try to use an empty locator, not xpath provided by me, as if this was an empty TO).
Thank you for your answers and the provided snippets.

Hello,

Im new katalon (previously using QTP, selenium).
Im interested with this “on the fly” object creation.
I tried to do it, but somehow im getting this error.
Im not sure what am i doing wrong here, so would be thankful if someone can explain a bit. Thanks :slight_smile:

Hi,

import for ContidionType class is missing. Press Ctrl + Shift + O to organize imports.

1 Like

@marek

Thanks, it worked!

Silly me, i never thought issue is just about the import haha.

I got thrown off by the error message “Groovy:Apparent variable ‘ConditionType’ was found in a static scope but doesn’t refer to a local variable, static field or class. Possible causes:”

Anyway, thanks!

You still miss ConditionType import - you can add it manually.

import com.kms.katalon.core.testobject.ConditionType

1 Like

Thanks, its fine now. No more errors, as i have used your suggestion Ctrl + Shift + O.

On a side note though, can I pls clarify.
What are other possible values for the part “xpath” in the line "to.addProperty(“xpath”, ConditionType.EQUALS, xpath)?

Can i use text? for example in this screenshot.
I would like to create the object “Sign in”, then click it during runtime

So i was planning something like

String signintext = “Sign in”
TestObject to = new TestObject()
to.addProperty(“text”, ConditionType.EQUALS, signintext )
WebUi.click(to)

will this work? thanks!

Yes, it should work as long as you use valid property name.

I was able to successfully implement makeTOwithXPath, but when running the Test Case, the log shows a blank object name, ex. Object '' is present. Is there a way to modify this to pass a string such that a given name is shown in the log? Ex. something like WebUI.verifyElementPresent(makeTOwithXPath('//div/a[text()="' + it + '"]', it + ' row link'), 5, FailureHandling.CONTINUE_ON_FAILURE) (where it = 'info') results in Object 'info row link' is present

Edit: After a bit of fiddling, I’ve found an answer to my own question.

static TestObject makeTOwithXPath(String xpath, String toName) {
	TestObject to = new TestObject(toName)
	to.addProperty('xpath', ConditionType.EQUALS, xpath)
	return to
}

This will let you specify a name for the object, such as Object 'info row link' is present mentioned above. Alternatively, you can just use the provided xpath to show in the log:

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

This will show in the log as something like Object '//div/a[text()="info"]' is present.

1 Like