Android Monkeyrunner Test
作者:网络转载 发布时间:[ 2013/12/30 15:48:32 ] 推荐标签:Android 自动化
关于Android自动化测试,研究了Monkey,Robotium 这次来看下 Monkeyrunner.
具体实践靠谱的当然还是官网资料了。
http://developer.android.com/tools/help/monkeyrunner_concepts.html
这里简单记录下实践过程,Monkeyrunner 需要用Python来编写,对于曾未学过Python的童鞋来说也没关系,因为Python属于比较好学的一门脚本语言.笔者也未曾学过Python,但有其他编程基础如:PHP,Java,Peal,还是能够很好理解Python的。
一、monkeyrunner 介绍
monkeyrunner 提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.
二、monkeyrunner 测试类型
多设备控制、功能测试、回归测试
三、实例(测试MyAndroid.apk)
1. 新建一个 monkeyrunnerTest.py

# Import the monkey runner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connects to current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Installs the Android package
device.installPackage("./MyAndroid.apk")
# Runs the component
device.startActivity(component = 'com.luwenjie.android/.MyAndroidActivity')
#Presses the Menu button
device.press('KEYCODE_MENU','DOWN_AND_UP')
#Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('./shot1.png','png')
2. 运行在 %Android_HOME% ools 目录下运行一下命令
monkeyrunner monkeyrunnerTest.py
四、API参考
MonkeyRunner:http://developer.android.com/tools/help/MonkeyRunner.html
MonkeyDevice:http://developer.android.com/tools/help/MonkeyDevice.html
MonkeyImage:http://developer.android.com/tools/help/MonkeyImage.html

sales@spasvo.com