A simple, easy-to-use, and stable Android automation library
Core functionality for connecting to Android devices and managing device state, including device discovery, connection establishment, and device information retrieval.
Connect to Android devices via ADB using serial numbers or automatic device detection.
def connect(serial: Union[str, adbutils.AdbDevice] = None) -> Device:
"""
Connect to Android device.
Parameters:
- serial: Device serial number or AdbDevice instance. If None, uses ANDROID_SERIAL environment variable or auto-detects device.
Returns:
Device instance for automation
Raises:
ConnectError: If device connection fails
"""
def connect_usb(serial: Optional[str] = None) -> Device:
"""
Connect to Android device via USB/ADB.
Parameters:
- serial: Device serial number. If None, auto-detects device.
Returns:
Device instance for automation
Raises:
ConnectError: If device connection fails
"""Usage examples:
import uiautomator2 as u2
# Auto-connect to available device
d = u2.connect()
# Connect to specific device by serial
d = u2.connect('Q5S5T19611004599')
# Connect using environment variable ANDROID_SERIAL
# export ANDROID_SERIAL=Q5S5T19611004599
d = u2.connect()Access device properties and system information.
class Device:
@cached_property
def serial(self) -> str:
"""Device serial number"""
@property
def info(self) -> Dict[str, Any]:
"""Device information including display size, orientation, package name, etc."""
def window_size(self) -> Tuple[int, int]:
"""Get device screen dimensions (width, height)"""Usage examples:
d = u2.connect()
# Get device serial
print(d.serial) # e.g., "Q5S5T19611004599"
# Get comprehensive device info
info = d.info
print(info)
# {'currentPackageName': 'com.android.launcher', 'displayHeight': 1920,
# 'displayWidth': 1080, 'screenOn': True, 'sdkInt': 27, ...}
# Get screen dimensions
width, height = d.window_size()
print(f"Screen: {width}x{height}")Control device power state and screen orientation.
class Device:
def screen_on(self):
"""Turn device screen on"""
def screen_off(self):
"""Turn device screen off"""
@property
def orientation(self) -> str:
"""Get device orientation: 'natural', 'left', 'right', 'upsidedown'"""
@orientation.setter
def orientation(self, value: str):
"""Set device orientation"""
def freeze_rotation(self, freezed: bool = True):
"""Freeze or unfreeze screen rotation"""Usage examples:
d = u2.connect()
# Screen control
d.screen_on()
d.screen_off()
# Orientation control
print(d.orientation) # e.g., "natural"
d.orientation = "left" # Rotate to landscape
d.freeze_rotation(True) # Prevent rotation
d.freeze_rotation(False) # Allow rotationConfigure device behavior and automation settings.
class Device:
@property
def settings(self) -> Settings:
"""Device settings configuration"""
def implicitly_wait(self, seconds: Optional[float] = None) -> float:
"""Set/get default wait timeout for UI elements"""
class Settings:
def __getitem__(self, key: str) -> Any:
"""Get setting value"""
def __setitem__(self, key: str, value: Any):
"""Set setting value"""Available settings:
wait_timeout: Default timeout for UI element waiting (default: 20.0)operation_delay: Tuple of (before, after) delays for operations (default: (0, 0))operation_delay_methods: Methods that use operation delay (default: ["click", "swipe"])max_depth: Maximum UI hierarchy depth (default: 50)fallback_to_blank_screenshot: Fallback behavior for screenshots (default: False)Usage examples:
d = u2.connect()
# Configure timeouts
d.settings['wait_timeout'] = 10.0
d.implicitly_wait(15) # Deprecated, use settings instead
# Configure operation delays
d.settings['operation_delay'] = (0.5, 1.0) # 0.5s before, 1.0s after
# Configure UI hierarchy depth
d.settings['max_depth'] = 30Configure logging output for debugging and monitoring.
def enable_pretty_logging(level=logging.DEBUG):
"""
Enable formatted logging output.
Parameters:
- level: Logging level (DEBUG, INFO, WARNING, ERROR)
"""Usage examples:
import uiautomator2 as u2
import logging
# Enable debug logging
u2.enable_pretty_logging(logging.DEBUG)
# Enable info logging
u2.enable_pretty_logging(logging.INFO)Install with Tessl CLI
npx tessl i tessl/pypi-uiautomator2@3.2.1