uiautomator2 Documentation

repository·master·Indexed 27 days ago

https://github.com/openatx/uiautomator2

A stable Android automation library for Python that exposes UiAutomator capabilities via an HTTP interface. It supports element interaction using XPath and selectors, gesture control (swiping, dragging, pinching), and application lifecycle management. The library includes support for plugins such as aircv for image-based interactions and Baidu OCR for text recognition, as well as integration with the UIAutoDev inspection tool.

Tokens
22.8K
Snippets
42
Records
155
Agent score
90%

What's inside uiautomator2

  1. Set up local development environment

    master

    To develop locally on the uiautomator2 repository, clone the repository, install dependencies using poetry, and sync the required assets (such as APKs) using make.

    git clone https://github.com/openatx/uiautomator2
    cd uiautomator2
    
    pip install poetry
    poetry install
    
    # download apk to assets/
    make sync
    
    # run python shell after device or emulator connected
    poetry run uiautomator2 console
  2. Useful resources for learning XPath

    master

    When working with the uiautomator2 XPath extension, you can use the following external resources to practice and learn advanced XPath syntax:

    • XPath Playground: An interactive environment to test your XPath expressions.
    • Some Advanced Uses of XPath: A guide for more complex queries.
    • XPath Quicksheet: A reference for common XPath syntax and functions.
  3. Collect performance metrics (CPU, PSS, NET) using uiautomator2.ext.perf

    master

    The uiautomator2.ext.perf plugin allows you to automatically record CPU usage, PSS (memory), and Network (rx/tx bytes) during your tests.

    Setup

    1. Register the plugin using u2.plugin_register('perf', perf.Perf).
    2. Connect to the device.
    3. Configure the package_name and csv_output path on the d.ext_perf object.

    Configuration Options

    • d.ext_perf.package_name: The target package to monitor.
    • d.ext_perf.csv_output: The file path where the CSV data will be saved.
    • d.debug: (Boolean) If True, outputs data to the console as it is collected. Default is False.
    • d.interval: (Float) The data collection interval in seconds. Default is 1.0s. It is recommended not to set this lower than 0.5s as memory collection is resource-intensive.

    Workflow

    1. Call d.ext_perf.start() to begin recording.
    2. Run your test code.
    3. Call d.ext_perf.stop() to end recording.
    4. (Optional) Call d.ext_perf.csv2images() to generate visualization charts from the collected CSV data.
    import uiautomator2 as u2
    import uiautomator2.ext.perf as perf
    
    package_name = "com.netease.cloudmusic"
    u2.plugin_register('perf', perf.Perf)
    
    def main():
        d = u2.connect()
        d.ext_perf.package_name = package_name
        d.ext_perf.csv_output = "perf.csv"
        # d.debug = True
        # d.interval = 1.0
        d.ext_perf.start()
    
        # run ... tests code here ...
    
        d.ext_perf.stop()
        d.ext_perf.csv2images()
    
    if __name__ == '__main__':
        main()
  4. Quick Start with uiautomator2

    master

    To use uiautomator2, ensure an Android device with Developer Options enabled is connected and visible via adb devices.

    Basic usage pattern:

    1. Import uiautomator2.
    2. Connect to the device using u2.connect().
    3. Perform actions like starting apps, waiting for activities, or interacting with elements via XPath.
  5. Use simplified XPath syntax for element selection

    master

    The uiautomator2 XPath extension provides several shorthand rules to speed up script writing:

    • Standard XPath: Start with // (e.g., //android.widget.TextView).
    • Resource ID: Start with @ to target resource-id (e.g., @android:id/list is equivalent to //*[@resource-id="android:id/list"]).
    • Regex: Start with ^ to use regular expressions (e.g., ^.*道了 matches text starting with anything and ending in '道了').
    • SQL-like Wildcards:
      • text%: Starts with text (equivalent to starts-with(text(), 'text')).
      • %text: Ends with text (equivalent to ends-with(text(), 'text')).
      • %text%: Contains text (equivalent to contains(text(), 'text')).
    • Text/Description Match: Using a bare string like 搜索 matches if text, content-desc, or resource-id contains that string.
  6. Use u2cli for Agent workflows

    master

    The u2cli is a lightweight CLI wrapper designed for agent workflows to view and control devices without writing Python code. It uses a local server to maintain device connections and reuse them across multiple commands.

    # Start the u2cli server
    u2cli start-server
    
    # Check server status
    u2cli server-status
    
    # Kill the server
    u2cli kill-server
    
    # Common commands
    u2cli device-info
    u2cli screenshot screen.png
    u2cli dump-hierarchy
    u2cli click --text Settings
  7. Breaking changes in uiautomator2 3.x: Removed functions and properties

    master

    Several functions, properties, and modules have been removed in 3.x. Ensure your code does not rely on:

    Functions to replace/remove:

    • current_app $\rightarrow$ use app_current instead.
    • connect_adb_wifi(str) $\rightarrow$ use connect() instead.
    • connect_wifi() and app_icon() (these relied on atx-agent).
    • set_new_command_timeout(timeout) (no longer needed).
    • open_identify().
    • toast.show(text, duration).
    • service(name: str) (used for atx-agent management).
    • healcheck().
    • d.xpath.apply_watch_from_yaml().

    Properties to remove:

    • d.address (previously used to get atx-agent URL).
    • d.alive (previously used to check atx-agent status).
    • d.uiautomator (previously used as d.uiautomator.stop()).
    • d.http (previously used as d.http.get("/device_info")).
    • d.widget.
    • d.watcher.debug.
    • u2.logger and u2.xpath.XPath.logger.
    • d.settings["xpath_debug"].

    Module and Class removals:

    • Module uiautomator2.ext.xpath is removed.
    • Classes AdbUI, GatewayError, ServerError, UiautomatorQuitError, RequestError, UiaError, JsonRpcError, NullObjectExceptionError, NullPointerExceptionError, StaleObjectExceptionError are removed.
  8. Breaking changes in uiautomator2 3.x: XPath and InputMethod changes

    master

    The following changes affect XPath and InputMethod usage:

    XPath (d.xpath) changes:

    • Removed methods: dump_hierarchy, get_last_hierarchy, add_event_listener, send_click, send_longclick, send_swipe, send_text, take_screenshot, when, run_watchers, watch_background, watch_stop, watch_clear, sleep_watch, and the position method.
    • Behavior change: d.xpath("...").wait() now returns a bool instead of XMLElement|None.

    InputMethod changes:

    • wait_fastinput_ime is deprecated.
    • Use set_input_ime instead of the deprecated set_fastinput_ime.
  9. Migrate from uiautomator2 2.x to 3.x

    master

    When upgrading from version 2.x to 3.x, note the following architectural and dependency changes:

    • Service Model: atx-agent is no longer a resident service. The uiautomator service is now started at runtime on the mobile device.
    • Connection: Direct connection via atx-agent address is no longer supported. The connect() function now only supports local USB devices or devices connected via adb connect.
    • Environment Variables: ANDROID_DEVICE_IP is no longer supported. Use ANDROID_SERIAL to pass the device serial number via environment variables.
    • Dependencies: Python requirement is now minimum 3.8. The project has moved from pbr to poetry and reduced the number of dependency libraries.
    • Logging: Uses the standard logging library. By default, it outputs nothing unless manually enabled.
    • Installation: minicap and minitouch are no longer installed by default.
  10. Connect to an Android device

    master

    You can connect to a device using several methods:

    1. Using Serial Number

    Pass the serial number (from adb devices) to u2.connect(). This is an alias for u2.connect_usb().

    import uiautomator2 as u2
    d = u2.connect('Q5S5T19611004599')

    To connect via a non-default port (default is 9008):

    d = u2.connect('Q5S5T19611004599', port=9009)

    2. Using Environment Variable

    Set the ANDROID_SERIAL environment variable to automatically connect without passing arguments.

    # export ANDROID_SERIAL=Q5S5T19611004599
    python
    >>> import uiautomator2 as u2
    >>> d = u2.connect()

    3. Using transport_id

    If you have the transport_id from adb devices -l, you can use adbutils to connect.

    import adbutils
    import uiautomator2 as u2
    
    dev = adbutils.device(transport_id=6)
    d = u2.connect(dev)