一. Monkeyrunner
http://developer.android.com\guide\developing\tools\monkeyrunner_concepts.html
http://developer.android.com\guide\developing\tools\MonkeyDevice.html
http://developer.android.com\guide\developing\tools\MonkeyImage.html
monkeyrunner工具提供了一套API,可以在Android代码之外编程控制Android设备和模拟器。可以用Pyhon安装应用或测试包并模拟按键,还可以抓屏。主要设计目的是功能测试和Framwwork测试。
monkeyrunner工具同Monkey工具的差别
Monkey: developer.android.com\guide\developing\tools\monkey.html
Monkey工具直接运行在adb shell中,生成用户或系统的事件流。
monkeyrunner工具则是通过API定义的特定命令和事件控制设备或模拟器。
monkeyrunner的特点
1. 多设备控制:
monkeyrunner API可以跨多个设备或模拟器测试。也可以用程序启动一个配置好的模拟
器,运行一个或多个测试,然后关闭模拟器。
2. 功能测试:
monkeyrunner可以为一个应用执行功能测试。提供按键或触摸事件的输入数值,然后观
察输出的截屏。
3. 回归测试:
monkeyrunner可以运行某个应用,测试应用的稳定性。
4. 可扩展性:
monkeyrunner是一个API工具包,可以基于Python开发一套系统来控制Android设备。此外还可以用标准的Python os和subprocess模块调用ADB工具。
二. 运行Monkeyrunner
运行时需要调用SDK目录的tools子目录下的monkeyrunner命令。如果提供的运行参数是一个文件名,则monkeyrunner将视文件内容为Python程序,并加以运行;否则,它将提供一个交互对话环境。
monkeyrunner的命令语法为:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>
下面实战操作一下:
测试里例子以计算器为例来演示安装测试过程。
先进入%ANDROID_SDK_HOME%\tools目录,因为计算器是内置的APK应用,先拷贝再删除内置的计算器应用
adb pull /system/app/Calculator.apk
adb shell rm /system/app/Calculator.apk
这样Calculator.apk就在%ANDROID_SDK_HOME%\tools下了。
1. 在 %ANDROID_SDK_HOME%\tools下新建一个TestCalculatgor.py文件,里面内容为:
import sys
# 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('./Calculator.apk')
# sets a variable with the package's internal name
package = 'com.android.calculator2'
# sets a variable with the name of an Activity in the package
activity = '.Calculator'
# sets the name of the component to start
runComponent = package + '/' + activity
# Runs the component
device.startActivity(component=runComponent)
print "Wait for 5 seconds"
MonkeyRunner.sleep(5.0)
# 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('./calculotor.png','png')
2. 打开命令行转到%ANDROID_SDK_HOME%\tools目录下运行一下命令:
monkeyrunner TestCalculatgor.py
运行后可以看到截图文件
3.更多的编程函数
可以使用touch()来模拟触屏消息
http://developer.android.com\guide\developing\tools\MonkeyDevice.html
Constants |
|||||||||||
string |
DOWN |
Use this with the type argument ofpress() ortouch()to send a DOWN event. |
|||||||||
string |
UP |
Use this with the type argument ofpress() ortouch()to send an UP event. |
|||||||||
string |
DOWN_AND_UP |
Use this with the type argument ofpress() ortouch()to send a DOWN event immediately followed by an UP event. |
Methods |
|||||||||||
void |
broadcastIntent(string uri,string action,string data,string mimetype,iterable categoriesdictionary extras,component component,iterable flags) Broadcasts an Intent to this device, as if the Intent were coming from an application. |
||||||||||
void |
drag(tuple start,tuple end,float duration,integer steps) Simulates a drag gesture (touch, hold, and move) on this device's screen. |
||||||||||
object |
getProperty(string key) Given the name of a system environment variable, returns its value for this device. The available variable names are listed in thedetailed description of this method. |
||||||||||
object |
getSystemProperty(string key) . The API equivalent of adb shell getprop <key>. This is provided for use by platform developers. |
||||||||||
void |
installPackage(string path) Installs the Android application or test package contained in packageFile onto this device. If the application or test package is already installed, it is replaced. |
||||||||||
dictionary |
instrument(string className,dictionary args) Runs the specified component under Android instrumentation, and returns the results in a dictionary whose exact format is dictated by the component being run. The component must already be present on this device. |
||||||||||
void |
press(string name,dictionary type) Sends the key event specified by type to the key specified by keycode. |
||||||||||
void |
reboot(string into) Reboots this device into the bootloader specified by bootloadType. |
||||||||||
void |
removePackage(string package) Deletes the specified package from this device, including its data and cache. |
||||||||||
object |
shell(string cmd) Executes an adb shell command and returns the result, if any. |
||||||||||
void |
startActivity(string uri,string action,string data,string mimetype,iterable categoriesdictionary extras,component component,flags) Starts an Activity on this device by sending an Intent constructed from the supplied arguments. |
||||||||||
MonkeyImage |
takeSnapshot() Captures the entire screen buffer of this device, yielding aMonkeyImageobject containing a screen capture of the current display. |
||||||||||
void |
touch(integer x,integer y,integer type) Sends a touch event specified by type to the screen location specified by x and y. |
||||||||||
void |
type(string message) Sends the characters contained in message to this device, as if they had been typed on the device's keyboard. This is equivalent to callingpress() for each keycode inmessage using the key event typeDOWN_AND_UP. |
||||||||||
void |
wake() Wakes the screen of this device. |