Skip to main content

Locator strategies for Mobile objects

Katalon Studio fully supports locator strategies supported by Appium, except for Android Data Matcher, including:

  1. 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 the content-desc attribute of the object.

    Sample code
    import 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")
  2. 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 code
    import 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")
  3. ID: Native element identifier. resource-id for android; name for iOS.

    Sample code
    import 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")
  4. Name: Name of the object.

    Sample code
    import 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")
  5. 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 code
    import 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]")
  6. Image: Locate an object by matching its image with a Base64-encoded string.
    Important:

    The element's image:

    The Base64-encoded string:



    Sample code
    import 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))
  7. Android UiAutomator: This is an Android specific locator strategy that uses the UI Automator API to locate objects.

    Sample code
    import 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)')
  8. Android View Tag: Locate an element by its view tag.

    Sample code
    import 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")
  9. iOS Predicate String: This strategy is mapped to the native XCTest predicate locator. See iOS Predicates.

    Sample code
    import 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'")
  10. iOS Class Chain: For more details on how to build this locator, see Locate an object by iOS Class Chain.

    Sample code
    import 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'`]")
  11. Custom: You can learn more about locating elements by custom strategy in the Appium document: Building Appium plugins.

    Sample code
    import 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")
Was this page helpful?