Locator strategies for Mobile objects
Katalon Studio fully supports locator strategies supported by Appium, except for Android Data Matcher, including:
Accessibility ID: Read a unique identifier for a UI element. For XCUITest, it is the
accessibility-id
attribute of the object. For Android, it is thecontent-desc
attribute of the object.Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.ACCESSIBILITY)
mobileObject.setMobileLocator("ImageView")Class name: For iOS it is the full name of the XCUI element and starts with XCUIElementType; for Android it is the full name of the UIAutomator2 class (e.g. android.widget.TextView).
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.CLASS_NAME)
mobileObject.setMobileLocator("General")ID: Native element identifier.
resource-id
for android;name
for iOS.Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.ID)
mobileObject.setMobileLocator("General")Name: Name of the object.
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.NAME)
mobileObject.setMobileLocator("General")XPath: Search the app XML source using XPath. Use XPath locators only if there is no other way to locate the given element due to its performance issues.
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.XPATH)
mobileObject.setMobileLocator("//XCUIElementTypeApplication/XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeTable[1]/XCUIElementTypeCell[2]/XCUIElementTypeStaticText[1]")- Image: Locate an object by matching its image with a Base64-encoded string.Important:
- You need to set up your mobile project for imaged-based testing. Refer to Image-based Testing for Mobile.
The element's image:
The Base64-encoded string:
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.apache.commons.io.FileUtils
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.IMAGE)
byte[] fileContent = FileUtils.readFileToByteArray(new File(imageFilePath))
mobileObject.setMobileLocator(Base64.getEncoder().encodeToString(fileContent)) Android UiAutomator: This is an Android specific locator strategy that uses the UI Automator API to locate objects.
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.ANDROID_UI_AUTOMATOR)
mobileObject.setMobileLocator('new UiSelector().className("android.widget.TextView").text("General").resourceId("android:id/title").packageName("com.android.settings").enabled(true).clickable(false).longClickable(false).checkable(false).checked(false).focusable(false).focused(false).scrollable(false).selected(false).index(0)')Android View Tag: Locate an element by its view tag.
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.ANDROID_VIEWTAG)
mobileObject.setMobileLocator("MY VIEW TAG")iOS Predicate String: This strategy is mapped to the native XCTest predicate locator. See iOS Predicates.
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.IOS_PREDICATE_STRING)
mobileObject.setMobileLocator("type == 'XCUIElementTypeStaticText' AND enabled == 1 AND label == 'General' AND name == 'General' AND name == 'General'")iOS Class Chain: For more details on how to build this locator, see Locate an object by iOS Class Chain.
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.IOS_CLASS_CHAIN)
mobileObject.setMobileLocator("**/XCUIElementTypeStaticText[`enabled == 1 AND label == 'General' AND name == 'General' AND value == 'General'`]")Custom: You can learn more about locating elements by custom strategy in the Appium document: Building Appium plugins.
Sample codeimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.MobileTestObject
import com.kms.katalon.core.testobject.MobileTestObject.MobileLocatorStrategy
MobileTestObject mobileObject = findTestObject("Object Repository/New Mobile Object")
mobileObject.setMobileLocatorStrategy(MobileLocatorStrategy.CUSTOM)
mobileObject.setMobileLocator("foo")