Cross-platform testing is essential for ensuring a seamless user experience across mobile operating systems. By using efficient locator strategies and test structuring techniques, you can run a single test case on both iOS and Android apps, reducing duplication and maintenance efforts. This guide outlines key approaches to achieve effective cross-platform test automation.
Table of Contents
Native Applications
Approach 1: Assign a common accessibility id
-
Ensure Accessibility identifiers are assigned
- iOS: First, verify that your app under test (AUT) for iOS has accessibilityIdentifier assigned to the required elements. (Refer to this page for instructions. You may need to consult an iOS developer to implement it.)
- Android: Ensure that contentDescription is set for the corresponding elements. (Refer to this page for instructions. You may need to ask an Android developer to assign it.)
-
Use consistent Accessibility ID values
- The contentDescription in Android should match the accessibilityIdentifier in iOS.
- When scanning UIs from the iOS and Android apps in MagicPod, ensure elements share the same accessibility ID across both platforms.
- The accessibilityIdentifier and contentDescription may be assigned different values depending on the device language. In such cases, a single locator can be used by combining a locator with a variable and a multilingual data pattern.
Approach 2 [Advanced]: Improve XPath locators
While XPath can slow down test execution, it can flexibly locate elements based on the app’s structure. This makes it a valuable alternative when assigning a common accessibility ID is not possible. Let’s explore how to refine XPath for better cross-platform compatibility using the following input field as an example:
Initial XPath Suggestions
When scanning the UI, MagicPod might suggest the following XPaths:
-
iOS:
//XCUIElementTypeTextField[@name='Name *']
-
Android:
//android.widget.EditText[@text='Name *']
Each XPath selects a text field element based on its attributes:
- The iOS XPath looks for an
XCUIElementTypeTextField
element anywhere on the screen that has aname
attribute with the value"Name *"
. - The Android XPath follows a similar logic but looks for an
EditText
element with atext
attribute set to"Name *"
.
Since element names and attribute names differ between iOS and Android, these XPaths are not interchangeable. To create a single, cross-platform locator, we need a way to unify them.
Optimized Cross-Platform XPath
By generalizing the differences between iOS and Android, we can rewrite the XPath as:
By using a variable for attribute names, this XPath works seamlessly across both platforms. See here for more information about using locators with variables in MagicPod.
Since we absorb platform differences using the textAttribute
variable, we can set its value (for example, as a shared variable) as:
-
iOS:
textAttribute = name
-
Android:
textAttribute = text
This allows the same XPath to function correctly on both iOS and Android by dynamically adjusting the attribute used for element selection.
Approach 3: Use a Branch Condition
If Approaches 1 and 2 are not feasible, or if sharing the same test steps for both your iOS and Android apps is not possible, you can use an operating system variable (see Special variables) to implement a conditional branch for iOS and Android.
This ensures platform-specific execution while maintaining a unified test structure.
Hybrid Applications
Use the Enable WebView scan feature to capture the UI. This will allow you to treat the WebView portion as an HTML page, enabling the use of the same locators for both iOS and Android. See here for more details.
Limitations
The self-healing feature is not supported when running a single test case across both iOS and Android.
For example, if you create a test case using a UI captured on iOS and then run it on Android, and the UI element is not found, self-healing will not be triggered. Instead, you will see the following message:
This UI cannot be healed because it was scanned on a different OS.