$ show openatx/uiautomator2
This repository provides the Python library `uiautomator2` for Android UI automation. It allows scripts to control Android devices by sending HTTP/JSON-RPC requests to an on-device server (either a Java-based UiAutomator2 jar or the older atx-agent). The core functionality includes element selection via selectors and XPath, gesture simulation (click, swipe, drag), text input via an IME, screenshot capture, UI hierarchy dumps, and app management. It also ships a CLI tool (`u2cli`) with subcommands for device inspection, app operations, and an agent server that exposes a persistent API for LLM/agent-based interactions. The architecture follows a client-server pattern where the Python client communicates over ADB-forwarded ports. The repository includes extensions for HTML reports, performance monitoring, and device info lookups, as well as archived modules for OCR and image recognition. It is widely used for automated testing of Android applications.
The LLM identified 4 files worth highlighting for reuse:
A type-checked settings class with built-in deprecation warnings for outdated options. Implements a clean pattern for managing configuration with validation that could be extracted into any Python project requiring validated settings with version migration support.
Implements a background thread that monitors for UI popups and automatically triggers click actions. Provides a declarative API for defining watch conditions (selector-based) and actions (click, press back, etc.). The pattern is reusable for any automation scenario requiring asynchronous UI interference handling.
Contains `DeviceRegistry` which manages a pool of Android device connections, enabling re-use and automatic rebinding when a device goes offline. Also implements `hierarchy_to_text()` that converts XML UI hierarchy into a compact, grep-friendly textual representation. The registry pattern is cleanly separated and could be adapted for other resource-connection management scenarios.
Provides a fluent XPath-based element selector with support for logical operators (`&` for AND, `|` for OR), attribute matching with regex and SQL-like patterns, and child traversal. The design encapsulates XPath expression building in a type-safe manner and is a good example of a DSL for querying hierarchical XML data, potentially reusable in other XML processing contexts.