xref: /aosp_15_r20/external/mobly-snippet-lib/examples/ex4_uiautomator/README.md (revision ae5b1ec8a57d9cd6259556f14d3f2bb4f9fb3a85)
1# UIAutomator Snippet Example
2
3The UiAutomator API, which allows developers to automate UI interactions on an
4Android device, is now available in Python on
5[google/snippet-uiautomator](https://github.com/google/snippet-uiautomator).
6This makes it possible for developers to use UiAutomator in Mobly tests without
7having to write Java.
8
9The `snippet-uiautomator` package is a wrapper around the AndroidX UiAutomator
10APIs. It provides a Pythonic interface for interacting with Android UI elements,
11such as finding and clicking on buttons, entering text into fields, and
12scrolling through lists.
13
14To use the `snippet-uiautomator` package, developers simply need to install it
15from PyPI and import it into their Python code. Once imported, they can use the
16package's API to automate any UI interaction.
17
18Here is an example of how to use the `snippet-uiautomator` package to automate a
19simple UI interaction:
20
21```Python
22from mobly.controllers import android_device
23from snippet_uiautomator import uiautomator
24
25# Connect to an Android device.
26ad = android_device.AndroidDevice(serial)
27
28# Load UiAutomator service.
29uiautomator.load_uiautomator_service(ad)
30
31# Find the "Login" button.
32button = ad.ui(res='com.example.app:id/login_button')
33
34# Click on the button.
35button.click()
36```
37