uiautomator2+python UI自动化

本文详细介绍了如何在Python环境中安装和使用uiautomator2进行Android设备的UI自动化测试。首先通过pip安装uiautomator2和相关依赖,然后在设备上安装uiautomator-server、atx-agent等组件,处理可能出现的安装问题。此外,还提到了weditor这款UI可视图工具的安装和使用,以及如何通过adb命令进行卸载和清理。
摘要由CSDN通过智能技术生成
  1. 安装uiautomator2  pip install -i https://pypi.tuna.tsinghua.edu.cn/simple uiautomator2
  2. python -m uiautomator2 init 设备里下载uiautomator 可能报错,可手动单独安装apk,参考下面的45
  3. pip install --pre --upgrade weditor -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  4. 在设备中安装app-uiautomator.apkapp-uiautomator-test.apk,提前adb root ,adb remount
  5. adb install PC上的目录(安装uiautomator APK
  6. adb push atx-agent /data/local/tmp/
  7. adb shell chmod 755 /data/local/tmp/atx-agent/atx-agent 获取权限;注意路径
  8. /data/local/tmp/atx-agent/atx-agent version # 查看版本
  9. adb shell /data/local/tmp/atx-agent/atx-agent server –d  启动atx-agent 
  10. 安装weditor  UI可视图工具,pip install --pre weditor
  11. 打开weditor python -m weditor,后续可直接输入weditor就可以,退出的话就是weditor --quit
12.卸载adb shell rm /data/local/tmp/minicapadb shell rm /data/local/tmp/minicap.soadb shell rm /data/local/tmp/minitouchadb shell /data/local/tmp/atx-agent/atx-agent server --stopadb shell rm /data/local/tmp/atx-agent/atx-agent
adb uninstall com.github.uiautomatoradb uninstall com.github.uiautomator.test1.首先需要安装环境,adb ,python

https://i-blog.csdnimg.cn/blog_migrate/6c4a386e6d9e87995ebeafa12af8ece8.png

adb的话android SDK里platform-tools里自带的,配到环境变量里就行

https://i-blog.csdnimg.cn/blog_migrate/8adc47ab541fe692f217d55422dc5d5b.png

python的话,去官网上下载即可

2.接下来就是主人公uiautomator2

uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库。其底层基于Google uiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作

1).安装(用pip安装,需要外网),首先安装uiautomator2库

pip install --pre uiautomator2  

pip install pillow

2)需要链接设备,在设备里安装一个东西,主要是这俩服务uiautomator-server 、atx-agent,openstf/minicap、openstf/minitouch(这两个mimi的可不安装)

python -m uiautomator2 init

如果安装失败,可以手动push安装,我就遇到安装失败的问题了,我这里报的就是版本的问题

https://i-blog.csdnimg.cn/blog_migrate/df02efdfc1222e0cf37c409e5cf30571.png

 

 

我们可以从git上获取最新安装包,手动安装到设备上

git上链接https://github.com/openatx/android-uiautomator-server/releases/

我当时用的最新版本2.3.1的,这俩都要下载

https://i-blog.csdnimg.cn/blog_migrate/90908506143b352148ff1ac8e4ea49f2.png

atx-agent git下载:https://github.com/openatx/atx-agent/releases

下载第一个就行,下载后解压就行

https://i-blog.csdnimg.cn/blog_migrate/34b4eec105ffc2a67475d2af23b93a6d.png

都下载好,需要操作几条命令

adb root    获取root权限
adb remount    获取权限
adb install E:\fengshuai\test\uiautomator\app-uiautomator.apk    安装apk
adb install E:\fengshuai\test\uiautomator\app-uiautomator-test.apk  安装apk
adb push E:\fengshuai\test\uiautomator\atx-agent /data/local/tmp/     push  atx到设备
adb push E:\fengshuai\test\uiautomator\minicap /data/local/tmp/    可有可无
adb push E:\fengshuai\test\uiautomator\minitouch /data/local/tmp/  可有可无
adb shell chmod 755 /data/local/tmp/atx-agent/atx-agent    获取该目录权限
adb shell /data/local/tmp/atx-agent/atx-agent server –d   开启atx-agent服务

3)服务正常开启如下所示:

https://i-blog.csdnimg.cn/blog_migrate/f4b69692a941c13a544be8cd4c364c08.png

4)因为安装uiautomator2后,开启服务后,安卓SDK自带的可视图工具会发生冲突,所以我们不用这个视图工具了,android-sdk\tools\uiautomatorviewer.bat  ;需要重新安装一个,也是很强大的一个工具

pip install --pre --upgrade weditor

安装后启动python3 -m weditor,会弹出来一个网页

https://i-blog.csdnimg.cn/blog_migrate/fa57ed922627f3a3367bed2e378315f6.png

5)接下来就可以愉快的编写脚本了

https://i-blog.csdnimg.cn/blog_migrate/c8736af3f803e12c730ca5691373b626.png

更多方法可在这里查看https://github.com/openatx/uiautomator2

 

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值