python-uiautomator2 安装以及连接手机调试

1、简介
python-uiautomator2是一个自动化测试开源工具,仅支持Android平台的原生应用测试。

2、支持平台及语言
python-uiautomator2封装了谷歌自带的uiautomator2测试框架,提供便利的python接口。他允许测试人员直接在PC上编写Python的测试代码,操作手机应用,完成自动化,大大提高了自动化代码编写的效率。

3、python-uiautomator2主要分为两个部分
python端:运行脚本,并向移动设备发送HTTP请求
移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。


安装

pip3 install --pre -U uiautomator
pip3 install pillow

#升级

pip3 install --upgrade --pre uiautomator2

设备安装atx-agent(注意手机端,会提示安装atx-agent)

macname@MacdeMacBook-Pro testenv3.7 % python3 -m uiautomator2 init
* daemon not running; starting now at tcp:5037
* daemon started successfully
macname@MacdeMacBook-Pro testenv3.7 % ls
Pipfile    file.json    include
bin    file.yaml    lib
macname@MacdeMacBook-Pro testenv3.7 % cd ~/Desktop

通过usb连接,打开usb文件传输模式,打开开发者选项USB调试模式

获取设备序列号

macname@MacdeMacBook-Pro ~ % adb devices
List of devices attached
62C6J10232191223100411276    device

macname@MacdeMacBook-Pro ~ %


编写测试脚本

macname@MacdeMacBook-Pro Desktop % cat test.py 
import uiautomator2 as u2

d = u2.connect_usb('62C6J10232191223100411276')
d.toast.show("Hello world", 3)
d.screenshot("home.jpg")
print(d.info)
macname@MacdeMacBook-Pro Desktop %

运行测试脚本
macname@MacdeMacBook-Pro Desktop % python3 test.py
[W 210109 14:59:40 __init__:203] atx-agent has something wrong, auto recovering
[D 210109 14:59:40 __init__:287] device 62C6J10232191223100411276 is online
[I 210109 14:59:40 init:155] uiautomator2 version: 2.12.2.dev1
[I 210109 14:59:40 init:352] Install minicap, minitouch
minitouch |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 34.1 KB/34.1 KB
minicap.so |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 28.4 KB/28.4 KB
minicap |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 526.5 KB/526.5 KB
[I 210109 14:59:48 init:367] Install com.github.uiautomator, com.github.uiautomator.test 2.3.1
app-uiautomator.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 2.1 MB/2.1 MB
[I 210109 14:59:58 init:332] - app-uiautomator.apk installed
app-uiautomator-test.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 1.2 MB/1.2 MB
[I 210109 15:00:03 init:332] - app-uiautomator-test.apk installed
[I 210109 15:00:03 init:340] Install atx-agent 0.9.5
atx-agent_0.9.5_linux_armv7.tar.gz |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 4.1 MB/4.1 MB
[I 210109 15:00:06 init:377] Check atx-agent version
Successfully init AdbDevice(serial=62C6J10232191223100411276)
[D 210109 15:00:06 __init__:600] kill process(ps): uiautomator
[D 210109 15:00:07 __init__:619] uiautomator-v2 is starting ... left: 40.0s
[D 210109 15:00:08 __init__:619] uiautomator-v2 is starting ... left: 39.0s
[I 210109 15:00:08 __init__:583] uiautomator back to normal
{'currentPackageName': 'com.huawei.android.launcher', 'displayHeight': 2163, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 748, 'displayWidth': 1080, 'productName': 'HMA-AL00', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}
macname@MacdeMacBook-Pro Desktop %

安装weditor
安装

pip3 install --pre weditor

运行

python3 -m weditor

运行上述命令后,自动打开浏览器,然后在浏览器中,
输入设备序列号 62C6J10232191223100411276
进行连接。

原文地址:https://www.cnblogs.com/sea-stream/p/14257619.html