python+uiautomator2+adb_环境搭建

本文介绍了uiautomator2,一个支持Python编写的安卓UI自动化框架,它提供了与uiautomator相比更便捷的体验。文章详细讲解了环境搭建步骤,包括安装依赖、连接设备、使用weditor工具以及遇到的问题和解决方法,如atx初始化失败和weditor安装问题的处理。
Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

uiautomator2是基于google uiautomator的安卓UI自动化框架,支持python编写测试脚本对设备进行自动化控制。原理是在设备上运行http服务器,将http接口封装成python库

uiautomator只能使用java语言,每次都要上传到设备上运行,uiautomator2可以实现所见即所得的测试

环境搭建

1、前置条件:已安装python及adb环境;
2、安装uiautomator2:

pip install -U uiautomator2

3、连接安卓设备:
设备需开启开发者选项,开放权限

# 检查设备是否已连接
adb devices

4、安卓设备安装ATX并进行初始化:
init指令向设备推送并安装 ATX 所需的服务组件,执行一次就完成初始化,重复执行会检测已安装版本,不会报错。

# 自动识别当前连接的单个设备,从默认源下载并安装 ATX 相关服务
python -m uiautomator2 init

# 使用国内镜像源,下载速度更快,解决国内网络下载失败问题,仅初始化指定序列号的具体设备
python -m uiautomator2 init --mirror --serial $SERIAL

初始化内容:安装minicap、minitouch、atx-agent,启动atx-agent(负责屏幕显示、屏幕控制、监控uiautomator2的运行)

[I 240130 16:49:38 init:156] uiautomator2 version: 2.16.25
[I 240130 16:49:38 init:373] Install minicap, minitouch
......
[I 240130 16:49:38 init:391] Already installed com.github.uiautomator apks
......
[I 240130 16:49:39 init:350] Check atx-agent version

5、安装weditor

pip install -U weditor

6、打开weditor

python -m weditor 
# 或 
weditor

7、连接设备
选择对应系统,输入设备序列号进行连接,开启实时刷新。
左侧可以看到设备实时页面,中间是定位元素的信息,右侧是自动生成的代码区、代码运行、控制台日志等。
在这里插入图片描述

问题及解决方法

ATX初始化失败

python -m uiautomator2 init

在这里插入图片描述
在这里插入图片描述
在浏览器打开网址,可以看到报错403,这是因为网络限制无法访问该地址
解决方法是避开内网限制:
1、设置外网
2、设备开启USB网络共享给PC提供网络
3、笔电连接可访问外网的网络

python -m uiautomator2 init --mirror --serial $SERIAL
# 出现以下报错时去掉--mirror
usage: __main__.py [-h] [-d] [-s SERIAL]
                   {version,init,screenshot,identify,install,uninstall,healthcheck,check,start,stop,current,doctor,console,purge}
                   ...
__main__.py: error: unrecognized arguments: --mirror

# $SERIAL要改成adb devices查询到的序列号
......
  File "D:\Program Files (x86)\python\lib\site-packages\adbutils\_adb.py", line 134, in check_okay
    raise AdbError(self.read_string_block())
adbutils.errors.AdbError: device '$SERIAL' not found

weditor安装失败

如果出现连接失败等问题可以考虑指定weditor版本

# 卸载旧版本
pip uninstall -y weditor
# 指定安装版本
pip install weditor==0.6.4

AttributeError: ‘Device’ object has no attribute ‘address’

 AttributeError: 'Device' object has no attribute 'address'

uiautomator2和weditor版本不匹配,新版 uiautomator2 删掉了 address 属性,旧版 weditor不兼容导致,给uiautomator2降级,重新初始化

pip install uiautomator2==2.10.2
pip install weditor==0.6.4
python -m uiautomator2 init
python -m weditor

AttributeError: ‘MockStdout’ object has no attribute ‘flush’

AttributeError: 'MockStdout' object has no attribute 'flush'

根据python安装路径找到…\Python\Python38\Lib\site-packages\weditor\web\ipyshell-console.py,在文件中添加

def flush(self)
    _stdout.flush()

在这里插入图片描述

WEditor 加载不出画面或者无法实时同步

  1. 手机端「急救操作」
    彻底关闭 ATX 应用:在手机「最近任务」里上滑彻底杀掉 ATX
    重新打开 ATX:手动点击 ATX 图标,允许所有权限(悬浮窗、后台运行、屏幕录制、存储)
    关闭电池优化:设置 → 应用 → ATX → 电池 → 选择「无限制 / 不优化」
    保持 ATX 前台运行:不要把 ATX 切到后台,先开着再刷新 WEditor
  2. 电脑端「一键重置服务」
    在 CMD 里执行这 4 行,强制重置 ATX + 截图服务:
# 1. 杀死旧服务
adb kill-server
# 2. 重启 ADB
adb start-server
# 3. 重新推送 ATX
python -m uiautomator2 init
# 4. 启动 weditor
weditor

执行完后,手机会自动重装 ATX,一定要在手机上点「安装 / 允许」

  1. 第三步:WEditor 端「强制修复」
    关闭所有 WEditor 浏览器标签页,彻底清空缓存,清楚浏览器cookies
    连接设备后,先点「静态」,再切「实时」,不要直接开实时
    如果还是空白,点右上角「Dump Hierarchy」手动拉取一次画面

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值