13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

可以完成AndroidUI自动化的Python库

uiautomator2

Android Uiautomator2 python Wrapper 这是一个可以完成Android的UI自动化的python库。 该项目还在火热的开发中

公司专注于为企业提供网站设计制作、成都做网站、微信公众号开发、商城建设小程序设计,软件定制网站开发等一站式互联网企业服务。凭借多年丰富的经验,我们会仔细了解各客户的需求而做出多方面的分析、设计、整合,为客户设计出具风格及创意性的商业解决方案,成都创新互联公司更提供一系列网站制作和网站推广的服务。

google提供的uiautomator库功能做起安卓自动化来非常强大,唯独有两个缺点:1. 只能在手机上运行 2. 只能使用java语言。 所以为了能更简单快捷的使用uiautomator,这个项目通过在手机上运行了一个http服务的方法,将uiautomator中的函数开放了出来。然后再将这些http接口,封装成了python库。这里要非常感谢 Xiaocong He ( @xiaocong ),他将这个想法实现了出来,uiautomator2这个项目则是对原有xiaocong的项目 uiautomator 进行了bug的修改,功能进行了加强。具体有以下

虽然我说的很简单,但是实现起来用到了很多的技术和技巧,功能非常强,唯独文档有点少。哈哈

Installation

  1. Install python library

    # Since uiautomator2 is still developing, you have to add --pre to install development version
    pip install --pre uiautomator2
    
    # Or you can install from source
    git clone https://github.com/openatx/uiautomator2
    pip install -e uiautomator2

    Optional, used in screenshot()

    pip install pillow
  2. Push and install (apk, atx-agent, minicap, minitouch) to device

    电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装 uiautomator-apk 以及 atx-agent

    python -m uiautomator2 init

    安装提示 success 即可

Usage 使用指南

下文中我们用 device_ip 这个变量来定义手机的IP,通常来说安装完 atx-agent 的时候会自动提示你手机的IP是多少。

如果手机的WIFI跟电脑不是一个网段的,需要先通过数据线将手机连接到电脑上,使用命令 adb forward tcp:7912 tcp:7912 将手机上的服务端口7912转发到PC上。这个时候连接地址使用 127.0.0.1 即可。

命令行使用

QUICK START

Open python, input with the following code

There are two ways to connect to the device.

  1. Through WIFI (recommend) Suppose device IP is 10.0.0.1 and your PC is in the same network.
import uiautomator2 as u2

d = u2.connect('10.0.0.1') # same as call with u2.connect_wifi('10.0.0.1')
print(d.info)
  1. Through USB Suppose device serial is 123456f
import uiautomator2 as u2

d = u2.connect('123456f') # same as call with u2.connect_usb('123456f')
print(d.info)

If just call u2.connect() with no arguments, env-var ANDROID_DEVICE_IP will first check. if env-var is empty, connect_usb will be called. you need to make sure there is only one device connected with your computer.

一些常用但是不知道归到什么类里的函数

先中文写着了,国外大佬们先用Google Translate顶着

检查并维持uiautomator处于运行状态

d.healthcheck()

连接本地的设备

需要设备曾经使用 python -muiautomator2 init 初始化过

d = u2.connect_usb("{Your-Device-Serial}")

一定时间内,出现则点击

10s内如果出现Skip则点击

clicked = d(text='Skip').click_exists(timeout=10.0)

打开调试开关

用于开发者或有经验的使用者定位问题

>>> d.debug = True
>>> d.info
12:32:47.182 $ curl -X POST -d '{"jsonrpc": "2.0", "id": "b80d3a488580be1f3e9cb3e926175310", "method": "deviceInfo", "params": {}}' 'http://127.0.0.1:54179/jsonrpc/0'
12:32:47.225 Response >>>
{"jsonrpc":"2.0","id":"b80d3a488580be1f3e9cb3e926175310","result":{"currentPackageName":"com.android.mms","displayHeight":1920,"displayRotation":0,"displaySizeDpX":360,"displaySizeDpY":640,"displayWidth":1080,"productName"
:"odin","screenOn":true,"sdkInt":25,"naturalOrientation":true}}
<<< END

Notes:In below examples, we use d represent the uiautomator2 connect object

Table of Contents

TODO

Basic API Usages

This part show the normal actions of the device through some simple examples

Retrive the device info

d.info

Below is a possible result:

{ 
    u'displayRotation': 0,
    u'displaySizeDpY': 640,
    u'displaySizeDpX': 360,
    u'currentPackageName': u'com.android.launcher',
    u'productName': u'takju',
    u'displayWidth': 720,
    u'sdkInt': 18,
    u'displayHeight': 1184,
    u'naturalOrientation': True
}

Key Event Actions of the device

You can find all key code definitions at Android KeyEvnet

Gesture interaction of the device

Note: click, swipe, drag support percent position. Example:

d.long_click(0.5, 0.5) means long click center of screen

Screen Actions of the device

Push and pull file

App management

Include app install, launch and stop

App install

Only support install from url for now.

d.app_install('http://some-domain.com/some.apk')

App launch

d.app_start("com.example.hello_world") # start with package name

App stop

# perform am force-stop
d.app_stop("com.example.hello_world") 
# perform pm clear
d.app_clear('com.example.hello_world')

App stop all the runnings

# stop all
d.app_stop_all()
# stop all app except com.examples.demo
d.app_stop_all(excludes=['com.examples.demo'])

Selector

Selector is to identify specific ui object in current window.

# To seleted the object ,text is 'Clock' and its className is 'android.widget.TextView'
d(text='Clock', className='android.widget.TextView')

Selector supports below parameters. Refer to UiSelector java doc for detailed information.

Child and sibling UI object

Get the selected ui object status and its information

Perform the click action on the seleted ui object

Gesture action for the specific ui object

Watcher

You can register watcher to perform some actions when a selector can not find a match.

另外文档还是有很多没有写,推荐直接去看源码 init .py

Global settings

# set delay 1.5s after each UI click and click
d.click_post_delay = 1.5 # default no delay

# set default element wait timeout (seconds)
d.wait_timeout = 30.0 # default 20.0

中文字符的输入

这种方法通常用于不知道控件的情况下的输入。第一步需要切换输入法,然后发送adb广播命令,具体使用方法如下

d.set_fastinput_ime(True) # 切换成FastInputIME输入法
d.send_keys("你好123abcEFG") # adb广播输入
d.set_fastinput_ime(False) # 切换成正常的输入法

测试方法

$ adb forward tcp:9008 tcp:9008
$ curl 127.0.0.1:9008/ping
# expect: pong

$ curl -d '{"jsonrpc":"2.0","method":"deviceInfo","id":1}' 127.0.0.1:9008/jsonrpc/0
# expect JSON output

Uiautomator与Uiautomator2的区别

  1. api不同但也差不多
  2. Uiautomator2是安卓项目,而Uiautomator是java项目
  3. Uiautomator2可以输入中文,而Uiautomator的java工程需借助utf7输入法才能输入中文
  4. Uiautomator2必须明确EditText框才能向里面输入文字,Uiautomator直接指定父类也可以在子类中输入文字
  5. Uiautomator2获取控件速度快写,而Uiautomator获取速度慢一些;

常见问题

  1. 提示 502 错误

    尝试手机连接PC,然后运行下面的命令

    adb shell am instrument -w -r  -e debug false -e class com.github.uiautomator.stub.Stub \
    	com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner

    如果运行正常,启动测试之前增加一行代码 d.healthcheck()

    如果报错,可能是缺少某个apk没有安装,使用下面的命令重新初始化 python -m uiautomator2 init --reinstall

尝鲜功能

手机 python -muiautomator2 init 之后,浏览器输入 <手机IP:7912>,会发现一个远程控制功能,延迟非常低噢。^_^

ABOUT

项目重构自 https://github.com/openatx/atx-uiautomator

CHANGELOG

Auto generated by pbr: CHANGELOG

依赖项目

Contributors

Others contributors

LICENSE

Under MIT


分享文章:可以完成AndroidUI自动化的Python库
本文URL:http://cdbrznjsb.com/article/dhgodgp.html

其他资讯

让你的专属顾问为你服务