Python-UiAutomator2实现Android自动化测试
本帖转自搜狗测试公众号
【一、前言】
基于Python-UiAutomator2实现Android自动化测试,小编在Android应用的自动化性能测试中进行了实践。本篇将简单介绍python中使用adb、aapt命令的方法以及Python-UiAutomator2使用,后续文章将对环境搭建以及自动化性能测试进行介绍,希望能给大家一些微薄的帮助。
【二、python中使用adb、aapt命令】
在python中使用adb命令,可以导入os模块
1、简单的adb命令
如:os.system('adb devices)
2、稍微复杂的adb命令
如:os.system('adb shell "dumpsys activity | grep "com.sogou.map.android.***.*""')
3、将adb读出的内容保存(os.system不支持读取,os.popen支持读取)
如:out = os.popen('adb shell "dumpsys activity | grep
"com.sogou.map.android.***.*""').read()


在python中使用aapt命令
aapt即Android Asset Packaging Tool,可以在Android SDK的platform-tools目录下找到该工具。aapt可以查看、创建、更新ZIP格式的文档,也可将资源文件编译成二进制文件。
1、 列出apk包的内容
aapt l[ist] [-v] [-a]file.{zip,jar,apk}
-v 以table形式列出来
-a 详细列出内容
2、 查看apk一些信息
aapt d[ump] [--values]WHAT file.{apk} [asset [asset ...]]
badging Print the label and icon for the app declaredin APK
permissions Print the permissions from the APK.
Resources Print the resource table from the APK.
Configurations Print the configurations in the APK.
Xmltree Print the compiled xmls in the given assets.
Xmlstrings Print the strings of the given compiled xmlassets.
例如:执行aapt d badging **.apk,可以看到其中包含了应用包名、版本号、permission等信息。


【三、Python-UiAutomator2使用】
1、UiAutomator
UiAutomator是google为Android平台开发的自动化测试框架,基本上支持所有的Android事件操作,主要是针对UI的自动化测试,支持Android 4.1以及更高的版本。
UiAutomator提供了以下两种工具来支持UI自动化测试:
(1). uiautomatorviewer:用来分析UI控件的图形界面工具,位于SDK目录下的tools文件夹中。
(2). uiautomator:一个java库,提供执行自动化测试的各种API。
是否能够用更脚本化的语言,例如Python,可以所见即所得地修改测试、运行测试?
非常感谢Xiaocong实现并分享(详见参考2),为Python和UiAutomator架了一座桥。
2、Python-UiAutomator2
python-uiautomator2是一个Android UI自动化框架,支持Python编写测试脚本对设备进行自动化。底层基于Google uiautomator2,允许测试人员直接在PC上编写Python的测试代码,操作手机应用,完成自动化,提高自动化代码编写的效率。原理是在手机上运行了一个http服务器,将uiautomator中的功能开放出来,再将这些http接口,封装成Python库。
支持平台及语言

python-uiautomator2主要分为两个部分,python客户端,移动设备
python端:运行脚本,并向移动设备发送HTTP请求
移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。
整个过程
(1). 在移动设备上安装atx-agent(守护进程),随后atx-agent启动uiautomator2服务(默认7912端口)进行监听;
(2). 在PC上编写测试脚本并执行(相当于发送HTTP请求到移动设备的server端);
(3). 移动设备通过WIFI或USB接收到PC上发来的HTTP请求,执行制定的操作。
python-uiautomator2代码示例

上面代码的作用是启动“搜狗地图”应用,然后点击“身边tab”,代码简洁、高效。

3、python-uiautomator2常用API介绍
3.1 获取机器的信息
d.info
d.window_size()
d.current_app()
d.serial #获取设备序列号
d.wlan_ip #获取无线局域网ip

3.2 屏幕相关的操作
开关屏幕,代码如下
d.screen_off() #打开屏幕
d.screen_on() #关闭屏幕
d.unlock() #解锁屏幕
3.3 按键(软/硬)操作
d.press('back')
d.press('home')
还支持如下按键的操作,
home、back、left、right、up、down、center、menu、search、enter、recent(recent apps)、volume_up、volume_down、volume_mute、camera、power
3.4 手势相关的操作,包括短按/长按/滑动/拖拽
点击操作
d.click(x, y)
双击操作
d.double_click(x,y)
长按操作
d.long_click(x, y)
滑动操作
d.swipe(sx, sy, ex, ey)
d.swipe(sx, sy, ex, ey, steps=10)
拖拽操作
d.drag(sx, sy, ex, ey)
3.5 屏幕相关的操作
获取并设置屏幕的旋转方向
orientation = d.orientation
d.set_orientation("l") # or "left"
d.set_orientation("r") # or "right"
d.set_orientation("n") # or "natural"
冻结/解冻旋转功能
d.freeze_rotation() # 冻结旋转
d.freeze_rotation(False) # 解冻旋转
屏幕截图
d.screenshot("home.png")
获取屏幕层级(hierachy)XML
xml = d.dump_hierarchy()
打开通知栏或快速设置栏
d.open_notification()
d.open_quick_settings()
【四、参考文献】
参考
https://github.com/openatx/uiautomator2
https://github.com/xiaocong/uiautomator#uiautomator
https://blog.csdn.net/jgw2008/article/details/78286469
https://testerhome.com/topics/11357
Python-UiAutomator2实现Android自动化测试的更多相关文章
- Android自动化测试-UiAutomator2环境搭建
Android自动化测试-UiAutomator环境搭建(QQ交流群:490451176) 一.环境准备 1. 安装android sdk,并配置环境变量 2. 安装android studio,国内 ...
- 使用 flow.ci 实现 Android 自动化测试与持续集成
在上篇文章--如何实现 Android 应用的持续部署中,我们使用的是 flow.ci + Github + fir.im 实现 Android 应用的持续部署.对于 Android 开发者,他们可能 ...
- 解放双手——Android自动化测试
解放程序猿宝贵的右手(或者是左手) http://blog.csdn.net/eclipsexys/article/details/45622813 --Android自动化测试技巧 Google大神 ...
- Python appium搭建app自动化测试环境
appium做app自动化测试,环境搭建是比较麻烦的. 也是很多初学者在学习app自动化之时,花很多时间都难跨越的坎. 但没有成功的环境,就没有办法继续后续的使用. 在app自动化测试当中,我们主要是 ...
- Android自动化测试学习路线
最近在整理Android自动化测试的相关资料,大体上把一些知识点梳理了,这里做一个简单的分享! Android里面测试相关的工具和框架太多了.你应该从以下几个方面入手. 编程语言的选择 如果你要学习的 ...
- Android 自动化测试框架
Android常用的自动化测试工具框架: Monkey,MonkeyRunner,UIAutomator,Robotium,Appium,Monkey Talk...... 但这些工具框架都是什么呢有 ...
- 133、 Android 自动化测试(转载)
Android 自动化测试--要点概括http://blog.csdn.net/vshuang/article/details/40595233 A/B测试与灰度发布http://blog.csdn. ...
- 【Mac + Appium + Python3.6学习(五)】之常用的Android自动化测试API总结
Github测试样例地址:https://github.com/appium-boneyard/sample-code/tree/master/sample-code/examples ①定位text ...
- python uiautomator2 watcher的使用方法
该方是基于uiautomator2如下版本进行验证的: PS C:\windows\system32> pip show uiautomator2 Name: uiautomator2 Vers ...
随机推荐
- sqlldr - exit error code 2 in unix (merged)
http://www.orafaq.com/forum/t/146582/ Thank you for your reply. Load has been successful all the tim ...
- <自动化测试>之<selenium API 查找元素操作底层方法>
搜罗了一些查找元素的除标准语句外,另外的语句使用方法,摘自 开源中国 郝云鹏driver = webdriver.Chrome(); 打开测试页面 driver.get( "http://b ...
- AcWing 227. 小部件厂 (高斯消元)打卡
题目:https://www.acwing.com/problem/content/description/229/ 题意:有很多个零件,每个零件的生产时间都在3-9天之间,现在只知道每个工人的生产部 ...
- flutter Could not find the built application bundle
报错信息Could not find the built application bundle at build/ios/iphoneos/Runner.app. Error launching ap ...
- 无法在要求对象展开的函数中使用 __try
解决方案: 单独把try里面的代码封装成一个函数,然后再在try里面调用
- 使用cookie来做身份认证 转载https://www.cnblogs.com/sheldon-lou/p/9545726.html
文章是msdn的官方文档,链接在这里.其实也有中文的文档,这里还是想做一个记录. 文章有asp.net core 2.x 和1.x 版本,我这里就忽略1.x了. 下面先说几点额外的东西有助于理解. A ...
- Cocos2d之运行Test项目
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 打开Test项目 路径为 C:\Dev\cocos2d-x-3.8.1\build 2.设定cpp-tests为启动项(当cpp ...
- MySQL总结01
window删除MySQL服务 cmd下执行 sc delete MySQL 登陆登出 登陆: mysql -uroot -ppasswd -h host 退出登陆 mysqladmin -uroot ...
- vue项目工具文件utils.js javascript常用工具类,javascript常用工具类,util.js
vue项目工具文件utils.js :https://blog.csdn.net/Ajaxguan/article/details/79924249 javascript常用工具类,util.js : ...
- 通过一条很慢的SQL梳理下SQL优化基础