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 ...
随机推荐
- 二、sql新增后返回主键|sql 使用 FOR XML PATH实现字符串拼接|sql如果存在就修改不存在就新增
一.sql新增后返回主键 1,返回自增的主键: INSERT INTO 表名 (字段名1,字段名2,字段名3,…) VALUES (值1,值2,值3,…) SELECT @@IDENTITY 2,返回 ...
- 第十章 Ingress
一.资料信息 Ingress-Nginx github 地址:https://github.com/kubernetes/ingress-nginx Ingress-Nginx 官方网站:https: ...
- zxing opencv
- 利用PHP和百度ai实现文本以及图片的审核
步骤: 首先打开百度ai 开发平台 注册一个账号: 注册账号,进入控制台 创建自己的应用,获取apikey 和秘钥 进入文档页 文本审核: 图像审核: 代码实例: class Sentive { pr ...
- numpy.unique
Find the unique elements of an array. Returns the sorted unique elements of an array. There are thre ...
- springbootboot 语句
-- ------------------------------ Table structure for `user`-- ----------------------------DROP TABL ...
- shell 以指定字符拆分字符串
string="hell|ttt|sss|2222" OLD_IFS="$IFS" IFS="|" array=($string) IFS= ...
- STM32串口USART通信总结
一.GPIO设置USART的初始化 /**************************实现函数******************************************** *函数原型: ...
- 【dart学习】-- Dart之JSON
概述 现在很难想象移动应用程序不需要与后台交互或者存储结构化数据.现在开发,数据传输方式基本都是用JSON,在Flutter中是没有GSON/Jackson/Moshi这些库,因为这些库需要运行时反射 ...
- 77 geometry process
0 引言 记录几何方面的一些处理技术. 1 任意多边形面积计算:包含凸多边形和凹多边形 转载了JustDoIT https://www.cnblogs.com/TenosDoIt/p/4047211. ...