How to get a Mobile Test Object's properties programmatically?

For “regular” TestObjects, I’m able to access the properties programmatically.

Here is some test code, that works fine with the “regular” TestObjects. Excuse the code quality. It’s just some test code for printing out the data to the log.

        @Keyword
	def printObjectInfo(TestObject to) {

		KeywordUtil.logInfo("objectId: ${to.getObjectId()}")
		KeywordUtil.logInfo("selectorMethod: $to.selectorMethod")

		def selectors = to.getSelectorCollection()

		KeywordUtil.logInfo("activeProperties.size(): ${to.activeProperties.size()}")

		KeywordUtil.logInfo("activeXpaths.size(): ${to.activeXpaths.size()}")

		KeywordUtil.logInfo("smartSelectorCollection.size(): ${to.smartSelectorCollection.size()}")

		KeywordUtil.logInfo("selectors.size(): ${selectors.size()}")
		
		String xpath = to.findPropertyValue('xpath')
		
		KeywordUtil.logInfo("xpath: $xpath")

		def results = selectors.find { it.key == to.selectorMethod }

		if (results != null)
			KeywordUtil.logInfo("$results.key: $results.value")

		for (item in selectors) {
			KeywordUtil.logInfo("$item.key: $item.value")
		}

		KeywordUtil.logInfo("properties.size(): ${to.properties.size()}")

		for(item in to.properties) {
			KeywordUtil.logInfo("$item.name: $item.value")
		}

		List<TestObjectXpath> xPaths = to.getXpaths()

		KeywordUtil.logInfo("getXpaths().size(): ${xPaths.size()}")

		for(item in xPaths) {
			KeywordUtil.logInfo("$item.name: $item.value}")
		}

		def searchResults = xPaths.find { it.name == "xpath:attributes" }

		if (searchResults != null)
			KeywordUtil.logInfo(searchResults.value)
	}

I’m having a challenge getting the various properties for a “Mobile” TestObject. When I call this function for a “Mobile” TestObject, I’m not able to access many of the properties. The main property that I’m trying to get out of the object is the selector (e.g. if XPATH, I want to get the actual XPATH string).

Any help would be highly appreciated? I’ve been stuck with this issue for too long.

1 Like

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

I see that there is a mobile version that extends the regular TestObject. Can you try that?
https://api-docs.katalon.com/com/kms/katalon/core/testobject/MobileTestObject.html

1 Like

I have writen a code in the Sample Android mobile tests project in Katalon Studio

Test Cases/printMobileTestObject

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

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

TestObject tObj = findTestObject("Object Repository/Application/android.widget.TextView - Graphics")
MobileTestObject mto = (MobileTestObject)tObj

my.MobileTestObjectPrinter.printMTO(mto)

Keywords/my/MobileTestObjectPrinter

package my

import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.util.KeywordUtil

class MobileTestObjectPrinter {
	static void printMTO(MobileTestObject mto) {
		String locator = mto.getMobileLocator();
		MobileLocatorStrategy strategy = mto.getMobileLocatorStrategy()
		KeywordUtil.logInfo("locator=" + locator)
		KeywordUtil.logInfo("strategy=" + strategy.getLocatorStrategy())
	}
}

Test Object

I modified the Object Repository/Application/android.widget.TextView - Graphics a bit. Its Locator Strategy was given as “Attribute”. I changed it to “XPATH” because @crisf wanted to see XPATH. See the screenshot.

Result

I ran the test case and got the following output in the console

2024-05-17 21:43:50.380 INFO  com.kms.katalon.core.util.KeywordUtil    - locator=//*[@class = 'android.widget.TextView' and (@text = 'Graphics' or . = 'Graphics') and @resource-id = 'android:id/text1']
2024-05-17 21:43:50.397 INFO  com.kms.katalon.core.util.KeywordUtil    - strategy=XPATH

1 Like

Thank you so much for the thoughtful and robust response! You nailed it!

Thank for your prompt response that pointed in the right direction!