A4Q Foundation Level Tester for Appium study materials
A4Q Foundation Level Tester for Appium study resources

Cross-Platform Test Suite Fails on iOS Due to Different Locator Behavior

A4Q Foundation Level Tester for Appium · Put your knowledge to work in context.

All case studies

Cross-Platform Test Suite Fails on iOS Due to Different Locator Behavior

You have a cross-platform Appium test suite that runs on both Android and iOS. The suite uses the same locators (XPath) for both platforms. After updating to Appium 2.0 and the XCUITest driver, 40% of tests fail on iOS with InvalidSelectorException or NoSuchElementException. The same tests pass on Android. The XPath locators are like //android.widget.Button[@text='Submit']. On iOS, the element hierarchy uses XCUIElementTypeButton and the label attribute is name instead of text. The app is built with React Native, so some elements are rendered as accessibility elements. The test suite uses a single page object class for both platforms. You need to make the suite cross-platform compatible without duplicating all tests.

1. How would you adapt the locators to work on both platforms?

Read the worked answer

The issue is that XPath locators are platform-specific. To make them cross-platform, use a locator strategy that abstracts platform differences. One approach is to use accessibility IDs, which work on both platforms if the app sets them consistently. For React Native, you can use testID which maps to resource-id on Android and name on iOS. So, set testID on elements and use AccessibilityId locator. If accessibility IDs are not available, use platform-specific locators in the page object, determined at runtime by checking driver.getPlatformName(). For example, in the page object, define a method getSubmitButton() that returns By.xpath("//android.widget.Button[@text='Submit']") for Android and By.xpath("//XCUIElementTypeButton[@name='Submit']") for iOS. Alternatively, use a custom locator that tries multiple strategies. Also, consider using the -ios class chain or -android uiautomator for better performance. Update the page objects to use these platform-specific locators, and ensure the test logic remains shared.

2. What changes would you make to the page object model to support both platforms efficiently?

Read the worked answer

Introduce a platform-aware locator factory. In the page object, instead of hardcoding locators, use a method that returns the appropriate locator based on the platform. For example, create a Locator class that holds both Android and iOS locators and a method getLocator() that returns the correct one. Alternatively, use a configuration file (e.g., JSON) that maps element names to platform-specific locators, and load it based on the platform. Use inheritance: create a base page object with common methods, and platform-specific subclasses that override locator definitions. However, to avoid duplicating tests, keep the test methods in a base test class and instantiate the correct page object based on the platform. Use dependency injection or a factory to provide the page object. Also, leverage Appium's findElementByAccessibilityId which works on both platforms if testID is set. For React Native, ensure developers set testID on all interactive elements. Finally, add a utility that waits for elements using a generic locator that tries multiple strategies (e.g., accessibility ID first, then platform-specific XPath).