Mobile Automation: Unified Approaches to Locate Controls for both iOS & Android
Mobile automation testing is trendy and has a massive impact on the product’s quality. It becomes significantly valuable when it is aligned with the frequent application updates, bug fixes, the newly added features, and the support for new devices, versions, capabilities, … etc.
Assume that you are a Mobile Automation Test Engineer, and let’s imagine the upcoming scenario:
- You are required to automate certain test cases in a mobile app.
- Tests should be uniformly written for both iOS and Android.
(E.g.: One script that is executable on both platforms).
- You started to prepare everything you need:
✓ The packages (.apk for Android and .ipa for iOS)
✓ Appium Server,
✓ The automation tool you will use,
✓ The emulators or real devices for debugging and execution,
✓ Your test structure (framework), and
✓ Your solution seed to build the scripts.
But before you start typing any test, you need to inspect both apps to locate the controls that are involved in your tests, and define them in your POM (Page-Object Model). Moreover, you need to find a unified approach to locate those controls despite the difference in either the platform or the environment.
We are ready to begin our Automation Marathon!
When you start to use any inspector, you will find out that the Layout structure for both Android and iOS is pure XML. One challenge here is that they have different structure and different tag names; as XML is user-defined markup language and the standard XML structure to build Android apps differs from that one in iOS.
iOS Screen and its corresponding XML
Android Screen and its corresponding XML
The question is how could we write the same script with the same locator for both versions despite the difference in XML structure?
In this article, we will be discussing different ways to unify your locators for both Android and iOS. The result will be one test script that could be successfully executed on both distributions.
As far as we know, there could be three possible ways to uniformly locate the same control in both Android and iOS:
1. Content-Desc and Accessibility ID
2. The if-else statement inside the locator (based on the platform).
3. The beauty XPath approach.
Let's take a brief description about each technique ..
- Content-Desc and Accessibility ID:
Different attributes but same value and easy to call in both. Preparation to implement them is needed to ease the interaction with the controls among app screens. Collaboration from mobile developers is required to achieve it. Once it is done, it will be super easy to locate the control on both platforms based on one unified value. It will be stored under Accessibility ID for iOS. The corresponding attribute in Android is Content-Desc. - The If-else statement:
Mobile developers are neither available or collaborative. What should we do?
The second and third approach might help us with this issue. The second approach focuses on the if-else statement, which is implemented in the POM for each control. In this approach we will locate the control based on XML.
Simply, locate the control based on the platform. More coding efforts and more time consuming on both platforms, but still unified approach to locate the same control. This could be simply done as follows:
WebElement loginButton () {
if(iOS) {
return findBy
(XML_Locator_presents_loginButton_in_iOS_structure);
}
else {
return findBy
(XML_Locator_presents_loginButton_in_Android_structure);
}
} - XPath approach:
As shown before in the previous screenshots, the login button may have the following displayed text: Sign in.
Since XPath is basically designed to work with XML and later used in HTML, we can locate the controls based on the displayed text. This should be working on both Android and iOS despite the difference in the XML structure between them. The syntax could be as follows:
WebElement controlContainsText(textValue) {
return findBy
("Xpath", "//*[@*[contains(.,'" + textValue + "')]]");
}
Conclusion:
In this article, three effective methods were presented to write more organized, structured, unified test scripts for both iOS and Android. By applying any, code redundancy will be avoided in most scripts. The simpler the script is, the easier to refactor and maintain.