Test object value retrieving from Selected Locator

Hi,

How to retrieve the value of Selected Locator from Test Object.

Kindly help me out to find a solution.

Thanks in advance.

hello,
check this chain

@Divya_T Is this what you’re looking for?

https://docs.katalon.com/javadoc/com/kms/katalon/core/testobject/TestObjectProperty.html#getValue()

Drawn from:

https://docs.katalon.com/javadoc/index.html

Thanks for the response Timo_kuisma & Russ_Thomas.

No, We need to retrieve the selected locator from TestObject Property. Please screenshot more clarification.

Kindly help us to resolve the same.

1 Like

I need this too.

The javadoc of TestObject is published here:

https://docs.katalon.com/javadoc/com/kms/katalon/core/testobject/TestObject.html

(this page was not availabe a year ago, it was just recently published. See API doc does not cover TestObject for the history)

If you read it and write a bit of code, you can see the internal of a TestObject.


The following GitHub project of mine contains a Test Case named “main/howToGetSelectedLocators”

The source of the test case is as follows:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.SelectorMethod
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
/**
 * This test case script shows how to retrieve "Selected Locator" of TestObject
 * 
 * The javadoc of TestObject is here
 * https://docs.katalon.com/javadoc/com/kms/katalon/core/testobject/TestObject.html
 */
TestObject testObjectAttribute = findTestObject("Page_CURA Healthcare Service/a_Make Appointment_BASIC")
String selectedLocatorAttribute = testObjectAttribute.getSelectorCollection()[SelectorMethod.BASIC]
WebUI.comment("selectedLocatorAttribute='${selectedLocatorAttribute}'")

TestObject testObjectCSS = findTestObject("Page_CURA Healthcare Service/a_Make Appointment_CSS")
String selectedLocatorCSS = testObjectCSS.getSelectorCollection()[SelectorMethod.CSS]
WebUI.comment("selectedLocatorCSS='${selectedLocatorCSS}'")

TestObject testObjectXPATH = findTestObject("Page_CURA Healthcare Service/a_Make Appointment_XPATH")
String selectedLocatorXPATH = testObjectXPATH.getSelectorCollection()[SelectorMethod.XPATH]
WebUI.comment("selectedLocatorXPATH='${selectedLocatorXPATH}'")

When executed, it emits following output:

...
selectedLocatorAttribute='//a[@id = 'btn-make-appointment']'
...
selectedLocatorCSS='#btn-make-appointment'
...
selectedLocatorXPATH='//a[@id='btn-make-appointment']'
...
1 Like

The line number 96 of https://github.com/kazurayam/TestObjectExtension/blob/master/Keywords/com/kazurayam/ks/testobject/TestObjectExtension.groovy might be interesting for you.

by calling TestObject#getSelectorMethod() you can know which “Selector Method” is chosen in a TestObject.

1 Like

For anyone trying the solutions above, but getting null returned by the getSelectorCollection() method: Make sure your TestObject is of the ‘newer’ kind. It seems older objects don’t have the SelectorCollection saved. So re-save your object (e.g. by making a change, saving, undoing the change, saving again), and then try the getSelectorCollection() again. After doing this, I finally was able to make the above solutions work.

1 Like

You are right. Here is the history.

However, in the Katalon projects created before v5.7, possibly there are many Test Object instances without selectorCollection element. There seems to be some other cases in ver5.7 and later where a Test Object instance is left with empty selectorCollection element.