MonkeyRunner工具使用Jpython(使用Java编程语言实现的Python)写出来的,MonkeyRunner提供了多个API,通过MonkeyRunner API可以编写一个Python程序来模拟操作控制Android设备App,测试其稳定性并通过截屏记录出现的问题。MonkeyRunner工具主要设计用于在功能/框架级测试应用程序和设备。MonkeyRunner在Android_sdk中的位置:tools目录下。
2、MonkeyRunner的功能多设备控制:API可以跨越多个设备,一次启动全部模拟器来实施测试套件。功能测试:为应用自动执行一次功能测试,然后观察输出结果的截屏。可扩展自动化:因为MonkeyRunner是一个API工具包,可以在这个基础上开发基于Python模块的整个系统来控制Android设备。3、MonkeyRunner与Monkey的区别MonkeyRunner和Monkey没有什么直接的关系。Monkey通过在设备直接运行adb shell命令来生成随机事件进行测试。MonkeyRunner通过API发送特定的命令和事件来控制设备。4、MonkeyRunner环境搭建安装好Android SDK后,配置MonkeyRunner环境变量(将MonkeyRunner的路径放入Path)。在cmd中输入MonkeyRunner出现如下显示内容说明安装成功,使用Ctrl+D退出。
环境.png5、MonkeyRunner API介绍官方文档:http://www.android-doc.com/tools/help/monkeyrunner_concepts.html#
MonkeyRunner主要分为三个类:
提供连接真机和模拟器、输入、暂停、警告框等方法
常用方法:
from com.android.monkeyrunner import Monkeyrunner as mrprint("connect devices...")device=mr.waitForConnection(5,'127.0.0.1:62001')
5.2、MonkeyDevice类提供安装和卸载程序包、开启activity、发送按键和点击事件、运行测试包等
常用方法:
installPackage(string path)removePackage(string package)startActivity(string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, flags)
touch(integer x, integer y, integer type)
touch参数说明:
integer x:x坐标值integer y:y坐标值integer type:key event类型(如: DOWN:按下事件 UP:弹起事件 DOWN_AND_UP:按下弹起事件 )drag(tuple start, tuple end, float duration, integer steps)
drag参数说明:
tuple start:拖拽起始位置,为tuple类型的(x,y)坐标点。tuple end,拖拽终点位置,为tuple类型的(x,y)坐标点。float duration,拖拽手势持续时间,默认为1.0s。integer steps,插值点的步数,默认值为10。5.3、MonkeyImage类用来保存各种格式的测试截图,并可以进行图像对比
常用方法:
takeSnapshot():屏幕截图writeToFile():保存图像文件到指定的文件路径6、MonkeyRunner案例6.1、测试场景使用MonkeyRunner连接设备,进行自动安装并启动APP,完成登录功能,并截图保存到指定目录下
6.2、思路分析步骤:
1、连接设备
2、安装App
3、启动App
4、点击用户名,密码输入框,完成输入
5、点击登录
6、截图
from com.android.monkeyrunner import MonkeyRunner as mrfrom com.android.monkeyrunner import MonkeyDevice as mdfrom com.android.monkeyrunner import MonkeyImage as mi#启动服务print("-----connect devices-----")device=mr.waitForConnection()print("-----succeed start-----")print("-----install app-----")device.installPackage(r"F:\kaoyan3.1.0.apk")print("-----succeed install-----")print("-----start App-----")package="com.tal.kaoyan"activity="com.tal.kaoyan.ui.activity.SplashActivity"runComponent=package+"/"+activitydevice.startActivity(component=runComponent)mr.sleep(3) #等待#点击取消更新按键print("-----click cancel update-----")device.touch(726.2,1096.4,"DOWN_AND_UP") #对取消更新按钮执行点击弹起操作mr.sleep(3)#点击跳过print("---click skip button---")device.touch(982,69,"DOWN_AND_UP") #对跳过按钮执行点击弹起操作mr.sleep(3)#输入账号print("input username and password")device.touch(194,381,"DOWN_AND_UP") #对用户名输入框执行点击弹起操作mr.sleep(1)device.type("zxw1234") (桃花劫是什么意思?桃花运是命理术数名词,又叫桃花煞。民间说法为“犯桃花”的坏情形,是中国文化中用来形容一个人出现爱情纠葛、异性缘变佳的情形。桃花可分为犯到好