Create a Dynamic Object at Runtime

I'm not quite at the point to need to make an object outside of the Object Repository, but I've seen reference to it multiple times and wanted to put together a simple example because who knows when it might come up. There have been several comments about keeping the size of the Object Repository small so it's better to programmatically create a one-off object rather than commit it to the project.

Katalon Studio allows creating objects during runtime through the TestObject library.

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

The object is created by giving it a name and associating a property to it. In the very simple example below, "xpath" is set with the location of a tab on the page.

TestObject is created with the name "dynamicObject"

The "dynamicObject" is given an xpath value that equals the contents of the xpath String

Once the object has been created, it can then be clicked.

The main work is done through this command:

TestObject dynamicObject = new TestObject('dynamicObject').addProperty('xpath', ConditionType.EQUALS, xpath, true)

xpathOfObject="//a[contains(text(),'Contacts')]"
TestObject dynamicObject = new TestObject('dynamicObject').addProperty('xpath', ConditionType.EQUALS, xpathOfObject, true)

WebUI.click(dynamicObject)

There is one important thing to note, since this object is not part of the project Object Repository, the call to manipulate it is slightly different. Note there is no, findTestObject, or the path of the object as part of the command. If the object were part of the project, the command would look like this:

WebUI.click(findTestObject('Project/Customer Profile/Tabs/tab-Contacts'))

The same would be true if this object were passed to a Custom Keyword:

WebUiCommonHelper.findWebElement(objectReference,5)
vs

WebUiCommonHelper.findWebElement(findTestObject(objectReference,5))

Again, I don't know that I have a use case for this scenario, but others have brought it up, and Katalon fully supports it. A dynamic object is quite an easy thing to create and the only real change is how the object is referenced by telling Katalon not to look in the Object Repository, but "locally" if you will.

Other articles of interest:

2 Responses to Create a Dynamic Object at Runtime

  • Jeremy Brien says:

    I appreciate this! I saw this post on LinkedIn this morning and was able to find a use case for it! I found that defining my xpath with an iterable variable allows me to loop through and capture text from tables created from nested and tags that Angular Materials auto generates.

    int i = 0
    xpathOfObject= ‘//*[@id=”mat-expansion-panel-header-‘+ i +'”]/span[1]/mat-panel-title/b’
    TestObject dynamicObject = new TestObject(‘dynamicObject’).addProperty(‘xpath’, ConditionType.EQUALS, xpathOfObject, true)

    x = WebUI.getText(dynamicObject)

    Just stick it in a loop and you can easily save each item to an array or whatever

  • Saish says:

    How to add shadow root parent to this runtime object..

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments
  • How To Disable the Quicken Registration Prompt (21)
    • Susan Long: I bought my quicken disc in the beginning and it came with a registration number. I rang the helpline and they gave me the code to put in and talked me through it. It you downloaded your version then you don’t own it...
    • Peter: You might consider running your old version of Quicken on an ancient computer. This is what I have done for years. The newer versions are fraught with problems- criminal, in my opinion. One version made mathmatical errors when...
    • Roslyn Chamberlain: Can I stop the countdown in quicken 2001 says only 8 sessions left. and what will happen after?
  • What is Katalon Studio? A Distro of Selenium, Groovy and Eclipse (1)
    • Mahesh: Looking for more posts on katalon studio.your katalon stuffs are always exiting
  • Simple wildcard searches for pattern matching (2)
    • Don Pedro: For that scenario it seems .contains would be your choice. For example, variable.contains(‘amazo n.com’) to see if the url had amazon.com. In that case, www.amazon.com, forum.amazon.com, retail.amazon.com would all...
    • Jony: Hi, How can I use a wild card to assert a URL is the one I want. I just want to verify the domain ==expected but not anything after it. Tried * but not working and only works when I have full URL.
  • Create a Dynamic Object at Runtime (2)
    • Saish: How to add shadow root parent to this runtime object..
    • Jeremy Brien: I appreciate this! I saw this post on LinkedIn this morning and was able to find a use case for it! I found that defining my xpath with an iterable variable allows me to loop through and capture text from tables created...
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (2)
    • Don Pedro: That would be things like: log.logWarning(‘The import date listed on the site is: ‘ + importDate) log.logWarning(‘The value from the site is: ‘ + salesFigure) log.logWarning(‘Filter Results for ‘ +...
    • Prashant Pednekar: Thanks for the informative article but can we also put some variable values to make it more exciting.
  • Setting up a repeatable Search Method in Katalon Studio (1)
    • RJ: Thanks a lot! This is very helpful 🙂
  • Filling forms with random numbers in Katalon Studio (3)
    • Patrick Clough: We created a handy random string generator custom keyword. The method takes in a string for the type of string you want, and an integer for the length. Looks like this: static String RANDOM_ALPHANUM =...
    • Don Pedro: randomNumber is the variable that holds the result of generating a new random number. Depending on how things work on your site, you might need to convert the number to a String before it’s recognized correctly....
    • Seth: When you are setting up randomNumber is this a new keyword or a new variable? I am trying to randomly generate unique socials and can’t seem to connect my generator to my Set Text.
  • Working with Dates and Date Formatting in Katalon Studio (5)
    • Ann: THANK YOU ! That worked perfectly !