Eleviant is to Elevate & Scale. Eleviant Tech symbolizes business transformation and reinforces our mission to help clients elevate and scale their business.
Android UI Automation Using Python Wrapper for UI Automator
What is UI Automator
During Android application testing, I explored cross-app functional UI testing and came across a tool, labeled ‘UI Automator.’
The tool allows the user to test user interactions across applications and helps a tester to verify how the app behaves with respect to the corresponding user inputs through Menus, Keyboards, Dialogs etc. and navigates into a system or other applications.
UI Automator, a Java library, is available as a part of ‘Android SDK’ from API level 18 (Android OS 4.3 or above).
UI Automator Viewer
UI Automator Viewer is a UI tool that helps you to analyze the UI components available on the screen. Using the tool, a user can inspect the layout hierarchy and the properties of the individual UI components available on the screen.
Python Wrapper of Android
UI Automator requires a fundamental knowledge of Java to write UI automation tests which I was not familiar with. So, I shifted my focus towards Python wrapper written for UI Automator testing framework and I used the same to test UI interfaces in Android, since I was comfortable with Python.
Down below, I’m going to demonstrate how the UI Automator can be set up and how to run the sample test snippet in Python. Here you go.
- Python ‘ in this demo I have used Python 2.7.10
- Android SDK (API level above 18)
- Pre-installed UI Automator library (Python wrapper)
- pip install uiautomator (command to install uiautomator)
- Android Device/Emulator (OS 4.3 above) connected to system using ADB
- IDE for Python, currently using pycharm
Now, let me guide you through how to launch the ‘Google Clock’ application and set an alarm using the UI Automator & UI Automator Viewer, which is used to amass information of UI components available on the target device screen.
Follow the steps below to access the ‘Clock’ application from the device using UI Automator Viewer.
- Launch the Uiautomatorviewer
- Generate the screen dump
- Access the targeted application and note down the UI details required for automation
Similarly, use the Uiautomatorviewer tool and get the UI components of the ‘Clock’ application and generate the script for accessing ‘Clock’ to set an alarm.
importing the device object as 'me' from uiautomator library from uiautomator import device as me Using Selectors to identify specific UI object in current window, here textMatches,descriptionMatches and resourceIdMatches are selectors. Any one of the Selectors (e.g.: text, resource-id, description, etc) is sufficient to automate the Android applications. me(textMatches="Clock").click() me(descriptionMatches="Add alarm").click() me(resourceIdMatches="android:id/hours").click() me(descriptionMatches="6").click() me(resourceIdMatches="android:id/minutes").click() me(descriptionMatches="0").click() me(textMatches="OK").click() After setting the alarm pressing back to reach home screen me.press.back()
If the testcase is executed successfully, you can mark it as ‘Passed’ and if it fails, with the help of try and except blocks in Python we can mark it as ‘Failed.’ Additionally, you can also add a report function to save the test case execution results in a CSV to verify the output.
The above code snippet should be saved as a ‘.py file’ by adding required functions to execute. A saved Python file can be executed as ‘Python ‘
In the above script, I have just showed you how to access a single application, but you can practically access more than one application using UI Automator with ease.
- Multiple applications can be tested using UI Automator.
- In Python wrapper, a jar file to run tests isn’t mandatory; whereas in Java we need to push a jar file to the device to run the tests.
I hope this post helped you learn how to launch and execute UI automation for Android using UI Automation Viewer.