1、MonkeyRunner
1.1、MonkeyRunner介绍
MonkeyRunner是Android SDK中提供的一套API(预先编译.class文件中的函数),就是{sdk安装目录}\tools\lib\monkeyrunner.jar,通过{sdk安装目录}\tools\monkeyrunner.bat脚本对其进行管理和驱使工作。
1.2、MonkeyRunner 组件
MonkeyRunner.jar中包含很多个包,每个包下都有若干个类,但主要API都集中在com.android. mo nkeyrunner包的三个模块中。

1.3、Monkey和Monkeyrunner的关系
a、相同点
1、都是AndroidSDK中自带的测试工具,都是Java工具,名字类似;
2、都可以通过对APP发送事件,对APP进行测试。
b、不同点
1、Monkey只是一条命令,不能使用测试脚本,没有断言;而MonkeyRunner是一个API工具包,可以运行Python编写的测试脚本,支持断言。
2、monkey工具直接运行在设备或模拟器的adbshell中,生成用户或系统的随机事件流,不带有任何主观性。而monkeyrunner工具则是通过预先编辑的API在Android代码之外发送特定的的事件控制控制设备或模拟器。
3、Monkey测试一般是在产品功能稳定之后进行,专擅于对APP作长稳测试;而MonkeyRunner可以在开发过程中或产品功能尚未稳定的时候展开,对APP某模块的交互流程和数据做UI测试。
2、monkeyrunner程序实例
2.1、支持测试用例
MonkeyRunner支持输入输出、流程控制。
1、安装是一个case,如果返回的是True就可以print此用例通过,如果返回的是False就可以print此用例失败。
2、启动入口activity可以设置为一个case,如果返回的是True就可以print此用例通过,如果返回的是False就可以print此用例失败。
3、触摸事件可以设置为一个case,如果返回的是True就可以print此用例通过,如果返回的是False就可以print此用例失败。
4、截图事件可以设置为一个case,如果返回的是True就可以print此用例通过,如果返回的是False就可以print此用例失败。
2.2、方式一:交互式
第一步、Android测试开发环境
使用的是集成式的ADT,步骤见《Android测试开发.docx》
第二步、启动模拟器或连接真机
模拟机或真机都可以,只是两者都有些缺陷。
第三步、准备安装包,DOS里启动monkeyrunner交互窗口

2.3、方式二:测试脚本
第一步、同上
第二步、安装Python

第三步、同上第二步
第四步、准备安装包,准备Python测试脚本,DOS里通过monkeyrunner执行脚本

test.py脚本
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Installs the Android package. Notice that this method returns a boolean, so you can test to see if the installation worked.
device.installPackage('./jinritoutiao_513.apk')
# Runs the component
device.startActivity(component='com.ss.android.article.news/.activity.SplashActivity')
# stop the testing processs
MonkeyRunner.sleep(30)
# touch the button
device.touch(105,48,'DOWN_AND_UP')
# stop the testing processs
MonkeyRunner.sleep(10)
# Presses the Menu button
device.press('KEYCODE_HOME','DOWN_AND_UP')
# stop the testing processs
MonkeyRunner.sleep(10)
# Runs the component
device.startActivity(component='com.ss.android.article.news/.activity.SplashActivity')
# Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('./jinritoutiao.png','png')
# stop the testing processs
MonkeyRunner.sleep(10)
# Uninstalls the Android package.
device.removePackage ('com.ss.android.article.news')
#reboot the device
device.reboot ()
MonkeyRunner是Android SDK中的一个API工具包,用于自动化测试。它与Monkey有所不同,Monkey是命令行工具,而MonkeyRunner支持Python脚本编写测试用例,包括输入输出、流程控制等。本文介绍了MonkeyRunner的组件、与Monkey的区别,并提供了交互式和脚本测试的实例。

9321

被折叠的 条评论
为什么被折叠?



