MonkeyRunner简介-飞
发布时间: 2023-07-06
1、简介

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主要分为三个类:

MonkeyRunner:它是一个MonkeyRunner程序提供工具方法的类,这个类提供了用于连接MonkeyRunner到设备或模拟器的方法,同时还提供了用于创建一个MonkeyRunner程序的用户界面以及显示内置帮助的方法。MonkeyDevice:它是一个设备或模拟器的类,这个类提供了安装和卸载程序包、启动一个活动以及发送键盘或触摸事件到应用程序的方法,同时提供也可以用这个类来运行测试包。MonkeyImage:它是一个截图对象的类,这个类提供了截图、将位图转换成各种格式、比较两个MonkeyImage对象以及写图像到文件的方法。5.1、MonkeyRunner类

提供连接真机和模拟器、输入、暂停、警告框等方法

API功能alert()警告框choice()选项列表框help()API帮助文档input()输入sleep()暂停waitFor Connection()等待设备连接

常用方法:

from com.android.monkeyrunner import Monkeyrunner as mrprint("connect devices...")device=mr.waitForConnection(5,'127.0.0.1:62001')
5.2、MonkeyDevice类

提供安装和卸载程序包、开启activity、发送按键和点击事件、运行测试包等

API功能broadcastIntent()发送广播drag()拖动getProperty()获取当前设备属性getSystemProperty()获取当前设备属性installPackage()安装应用instrument执行测试用例press()按键reboot()重启removePackage()删除指定PackagestartActivity()启动应用touch()点击

常用方法:

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类

用来保存各种格式的测试截图,并可以进行图像对比

API功能convertToBytes()转换图像格式getRawPixel()获取当前坐标像素元组getRawPixellnt()获取当前坐标像素值sameAs()图像对比writeToFile()保存图像文件到指定的文件路径getSubImage()截取子图像takeSnapshot()屏幕截图

常用方法:

takeSnapshot():屏幕截图writeToFile():保存图像文件到指定的文件路径6、MonkeyRunner案例6.1、测试场景

使用MonkeyRunner连接设备,进行自动安装并启动APP,完成登录功能,并截图保存到指定目录下

6.2、思路分析

步骤:
1、连接设备
2、安装App
3、启动App
4、点击用户名,密码输入框,完成输入
5、点击登录
6、截图

6.3、代码(考研帮)
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")    (桃花劫是什么意思?桃花运是命理术数名词,又叫桃花煞。民间说法为“犯桃花”的坏情形,是中国文化中用来形容一个人出现爱情纠葛、异性缘变佳的情形。桃花可分为犯到好

微信