python+uiautomator2+adb

uiautomator2

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

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

环境搭建

1、已安装python及adb环境

2、安装uiautomator2:

python 复制代码
pip install -U uiautomator2

3、连接安卓设备:设备需开启开发者选项,adb devices确认设备已连接

4、安卓设备安装ATX并进行初始化:

python 复制代码
python -m uiautomator2 init
python -m uiautomator2 init --mirror --serial $SERIAL

初始化内容

安装minicap、minitouch、atx-agent,启动atx-agent

(负责屏幕显示、屏幕控制、监控uiautomator2的运行)

python 复制代码
[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

python 复制代码
pip install -U weditor

6、打开weditor

python 复制代码
python -m weditor 或 weditor

7、连接设备

选择对应系统,输入设备序列号进行连接,开启实时刷新

左侧可以看到设备实时页面,中间是定位元素的信息,右侧是自动生成的代码区、代码运行、控制台日志等

问题及解决方法

atx初始化失败

python 复制代码
python -m uiautomator2 init


在浏览器打开网址,可以看到报错403,这是因为网络限制无法访问该地址

解决方法是避开内网限制:

1、设置外网()

2、设备开启USB网络共享

3、笔电可以连接可访问外网的网络

python 复制代码
python -m uiautomator2 init --mirror --serial $SERIAL
python 复制代码
# 出现以下报错,去掉--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安装失败

指定版本进行安装

python 复制代码
pip install weditor==0.6.4
相关推荐
巽星石21 分钟前
【Blender Python】7.一些运算、三角函数以及随机
python·blender·三角函数·随机·环形阵列
CSXB9922 分钟前
一、Python(介绍、环境搭建)
开发语言·python·测试工具·集成测试
Mopes__25 分钟前
Python | Leetcode Python题解之第461题汉明距离
python·leetcode·题解
EterNity_TiMe_1 小时前
【机器学习】智驭未来:探索机器学习在食品生产中的革新之路
人工智能·python·机器学习·性能优化·学习方法
Mopes__1 小时前
Python | Leetcode Python题解之第452题用最少数量的箭引爆气球
python·leetcode·题解
AI视觉网奇2 小时前
pymeshlab 学习笔记
开发语言·python
纪伊路上盛名在2 小时前
如何初步部署自己的服务器,达到生信分析的及格线
linux·运维·服务器·python·学习·r语言·github
计算机源码社2 小时前
分享一个餐饮连锁店点餐系统 餐馆食材采购系统Java、python、php三个版本(源码、调试、LW、开题、PPT)
java·python·php·毕业设计项目·计算机课程设计·计算机毕业设计源码·计算机毕业设计选题
汤兰月2 小时前
Python中的观察者模式:从基础到实战
开发语言·python·观察者模式
西柚与蓝莓4 小时前
【开源开放体系总结】
python