https://github.com/openatx/uiautomator2
需求
- Android版本 4.4+
- Python 3.6+ (社区反馈3.8.0不支持, 但是3.8.2支持)
准备
- 开启了开发者选项的安卓手机
- 运行pip3 install -U uiautomator2 安装uiautomator2
- 运行python3 -m uiautomator2 init安装包含httprpc服务的apk到手机+atx-agent, minicap, minitouch (在过去的版本中,这一步是必须执行的,但是从1.3.0之后的版本,当运行python代码u2.connect()时就会自动推送这些文件了)
import uiautomator2 as u2
d = u2.connect() # connect to device
print(d.info)
如下输出,证明成功了:
{'currentPackageName': 'net.oneplus.launcher', 'displayHeight': 1920, 'displayRotation': 0, 'displaySizeDpX': 411, 'displaySizeDpY': 731, 'displayWidth': 1080, 'productName': 'OnePlus5', '
screenOn': True, 'sdkInt': 27, 'naturalOrientation': True}
- Install daemons to a device (Optional)
电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch
# init 所有的已经连接到电脑的设备
python -m uiautomator2 init
- Install weditor (可视化工具)
因为uiautomator是独占资源,所以当atx运行的时候uiautomatorviewer是不能用的,为了减少atx频繁的启停,我们开发了基于浏览器技术的weditor UI查看器。https://github.com/openatx/weditor
安装方法(备注: 目前最新的稳定版为 0.1.0)
pip install weditor
# 进入网页
weditor

image.png
# 连接设备
d = u2.connect()
# d = u2.connect_wifi(ip)
# 检查并维持设备端守护进程处于运行状态
d.healthcheck()
# 解锁屏幕
d.unlock()