Can I put a testObj properties in a variable and use it dynamically?

I’m sure there is probably one answer out there…but unable to figure it out. :face_exhaling:

So I wonder if it’s possibly to change a testObj properties on demand… maybe in a variable ?:smiley:

So i have this testObject -

And let’s say I want to change the text inside that link on demand - example: Text 1, Text 2, Text 3 - whenever i need to. basically want to put that text into a variable. instead of creating a new testObj everytime with new text… prefer a local variable instead of global so i can just put the text i need when running the test within the tc.

does that make sense?

String xpath = "(//a[contains(text(),''Text 1'))]". ← i was rtying to do something like this within the TC… and i’d just manually update the “Text 1” to whatever would be the new text for that web element.

1 Like

You can do like you want with either parameterization or creating objects in code using CSS or XPath.

Note: If you go with creating objects in code, you will need a different method name for one or the other. You cannot have one method name to do “both”–that will be an error.

Edit: sample of parameterization

WebUI.verifyElementVisible(findTestObject('myPage/label_GeneralUsage',
			['text': "Email address"])

sample of in-code using xpath

text1 = "Email address"
// note the double quotes must be the exterior ones making up the pathway; single quotes can be inside of the double
myItem = com.Tools.makeTO("//a[contains(text(),'${text1}')]") 
WebUI.verifyElementVisible(myItem)
1 Like

An sample Test Case that generates parameterized TestObjects on the fly:

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

/**
 * Katalon API Javadoc - TestObject is here
 * https://api-docs.katalon.com/com/kms/katalon/core/testobject/TestObject.html
 * 
 */

TestObject makeTestObjectForAnchorWithText(String text) {
	TestObject tObj = new TestObject(text)
	tObj.addProperty("xpath", ConditionType.EQUALS, "//a[contains(.,'" + text + "']")
	return tObj
}

TestObject tObj1 = makeTestObjectForAnchorWithText("Text1")
println tObj1.getProperties()[0].getValue()

TestObject tObj2 = makeTestObjectForAnchorWithText("Text2")
println tObj2.getProperties()[0].getValue()

TestObject tObjX = makeTestObjectForAnchorWithText("TextX")
println tObjX.getProperties()[0].getValue()

The output:

2023-11-15 09:10:05.699 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-11-15 09:10:05.700 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/TC4
//a[contains(.,'Text1']
//a[contains(.,'Text2']
//a[contains(.,'TextX']
2023-11-15 09:10:05.882 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC4

The document worked as instructed - your example of codeblock helped as well. I just used a variable for the text in xpath.

hhid = "John Smith"

WebUI.click(findTestObject('Page_Household Index', ['myLink': hhid]))

this line didn’t work though… would like to get this method right as well. any idea?

I fixed the link for the xpath method in my response above to the in-code method of @kazurayam that uses xpath.

You create a Keyword method, called "makeTO’ (or whatever you want to call it), like:

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

@Keyword
def static makeTO(xpath) {
	TestObject tObj = new TestObject(xpath)
	tObj.addProperty('xpath', ConditionType.EQUALS, xpath)
	return tObj	
}

@Keyword
def static makeTO(id, xpath) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty('xpath', ConditionType.EQUALS, xpath)
	return tObj	
}

Now, you use the pathway to your Keyword in place of my own pathway. My pathway is “com.Tools.makeTO”; yours can be different. Also, since the method is static, you do not need the “@Keyword” phrase.

I should add that the sole parameter added to the TestObject() constructor here is the name of the test object. That name is what will show up on your test case logs. Make sure that you make it something that you/other test devs will recognize

ya now that makes sense… i thought i was missing some import or your code got cut off.

Another sample Test Case that generates parameterized Testobjects on the fly:

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

/**
 * Katalon API Javadoc - TestObject is here
 * https://api-docs.katalon.com/com/kms/katalon/core/testobject/TestObject.html
 *
 */

TestObject makeTestObject(String xpathTemplate, Map<String, String> variables) {
	def groovyscript = '"""' + xpathTemplate + '"""'
	def shell = new GroovyShell(new Binding(variables))
	String expr = shell.evaluate(groovyscript)
	TestObject tObj = new TestObject(expr)
	tObj.addProperty("xpath", ConditionType.EQUALS, expr)
	return tObj
}

String xpathTemplate = '//a[contains(., "${content}")]'

TestObject tObj1 = makeTestObject(xpathTemplate, ['content': 'Text1'])
println tObj1.getProperties()[0].getValue()

TestObject tObj2 = makeTestObject(xpathTemplate, ['content': 'Text2'])
println tObj2.getProperties()[0].getValue()

TestObject tObjX = makeTestObject(xpathTemplate, ['content': 'TextX'])
println tObjX.getProperties()[0].getValue()

The makeTestObject(String template, Map<String, String> variable) does the same as what Katalon’s Parameterized TestObject does.

Output

2023-11-15 15:30:57.094 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-11-15 15:30:57.095 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/TC5
//a[contains(., "Text1")]
//a[contains(., "Text2")]
//a[contains(., "TextX")]
2023-11-15 15:30:57.381 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC5

thanks everyone for all the methods! You guys rock!