Creation of Test Object in Memory


#1

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

#2

It doesn’t touch the OR. It should say

Creation of TestObject in Memory at Runtime


#3

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)


#4

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


#5

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
}

#6

@ 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.