Clone this repo:

Branches

  1. d002712 Fix Pyrefly and Java Code Clarity issues (#89) by Kolin Lu · 9 days ago main
  2. cf556b2 Feat: Add accessibility_click() bypass to UiObject2 (#88) by Kolin Lu · 9 days ago
  3. 4e88833 Fix Pyrefly type-checking issues in uidevice and byselector (#87) by Kolin Lu · 9 days ago
  4. aa7567a Fix Pytype errors in BySelector regarding recursive mapping types (#86) by Kolin Lu · 10 days ago
  5. c66673a Add a new method any_exists to the _Wait class in snippet_uiautomator.uidevice. (#85) by shuoqian · 11 days ago

Snippet UiAutomator

Latest release APK Build Status Python Build Status

This is the Python wrapper based on Mobly Snippet Lib for calling the AndroidX UiAutomator APIs.

Requirements

  • Python 3.7+
  • Android 8.0+ (SDK 26+)

Installation

pip install snippet-uiautomator

Initial Mobly Android Controller

  • Inside Mobly Test

    To use in a Mobly test, the Android device needs to be initialized in the Mobly base test.

    from mobly import base_test
    from mobly.controllers import android_device
    
    class MoblyTest(base_test.BaseTestClass):
      def setup_class(self):
        ad = self.register_controller(android_device)[0]
    
  • Outside Mobly Test

    If not using in a Mobly test, such as when running in a Python terminal, the Android device can be initialized with its serial number.

    from mobly.controllers import android_device
    
    ad = android_device.AndroidDevice('GOOG1234567890')
    

To learn more about Mobly, visit Getting started with Mobly.

Launch UiAutomator Service

[!WARNING] UiAutomation is a service that can only be used by one app at a time. If this service is already being registered by another app, Snippet UiAutomator will not be able to start. To resolve this issue, stop the app holding the UiAutomation service before calling Snippet UiAutomator.

Snippet UiAutomator supports launching as one of Mobly Android Device Service.

from snippet_uiautomator import uiautomator

ad.services.register(
    uiautomator.ANDROID_SERVICE_NAME, uiautomator.UiAutomatorService
)

ad.ui(text='OK').click()

Test

Run unit tests for Python part:

pytest

Run tests for Snippet part:

cd snippet_uiautomator/android
./gradlew test

Set up pre-commit hooks, this will run some automated checks during each git commit and possibly update some files that require changes:

pip install pre-commit
pre-commit install

API Usage