标签:模块 语法规则 tool 常用 dom list 命令工具 ref filter
Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流(如按键输入、触摸屏输入、手势输入等),实现对正在开发的应用程序进行压力测试。Monkey测试是一种为了测试软件的稳定性、健壮性的快速有效的方法。
Monkey测试参数建议
adb shell monkey -p com.package.xxx --throttle 500 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v 1000000>G:\MonkeyTest.log 2>&1 &
Monkey测试方案
单一apk测试: monkey –p <options> -c <options> -s <seed> <限制语句> --throttle <milliseconds> -v 执行次数> G:\MonkeyTest.log
集合apk测试:
黑名单以外的APK: monkey –pkg-blacklist-file /data/local/tmp/blacklist.txt -c <options> -s <seed> <限制语句> --throttle <milliseconds> -v 执行次数>G:\MonkeyTest.log
白名单中APK: monkey –pkg-whitelist-file /data/local/tmp/whitelist.txt -c <options> -s <seed> <限制语句> --throttle <milliseconds> -v 执行次数 > G:\MonkeyTest.log
Monkey测试脚本
Android 的 monkey test 工具提供了 -f scriptfile 参数,可以指定 test 脚本
什么是monkey script
Monkey script是按照一定的语法规则编写有序的用户事件流并适用于monkey命令工具的脚本 adb shell monkey -f <script file> <运行脚本的次数>
在development/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java
源码下有一段注释规定了monkey script的基本规则:
monkey event queue. It takes a script to produce events
sample script format:
type= raw events
count= 10
speed= 1.0
start data >>
captureDispatchPointer(5109520,5109520,0,
230.75429,458.1814,0.20784314,0.06666667
,0,0.0,0.0,65539,0)
captureDispatchKey(5113146,5113146,0,20,0,0,0,0)
captureDispatchFlip(true)
...
*/
脚本的内容示例(源码修正版)
//header
type= raw events
count= 10
speed= 1.0
//line at the end of the header means that below it is the context of script
start data >>
DispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314,
0.06666667,0,0.0,0.0,65539,0)
DispatchKey(5113146,5113146,0,20,0,0,0,0)
DispatchFlip(true)
...
说明:
1)、前3行是脚本头
2)、后3行是脚本内容,每行为一个函数
monkey中提供的函数如下:
1..DispatchPointer(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags)
2..DispatchTrackball(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags) 轨迹球
3..DispatchKey(long downTime, long eventTime, int action, int code, int repeat, int metaState, int device, int scancode)
4..DispatchFlip(boolean keyboardOpen) 实体键盘关闭与打开
5..DispatchPress(int keyCode)
6..LaunchActivity(String pkg_name, String cl_name)
7..UserWait(long sleeptime)
8..LongPress(int keyCode)
key [down|up] keycode
touch [down|up|move] x y
This command injects a MotionEvent into the input system that simulates a user touching the touchscreen (or a pointer event). x and y specify coordinates on the display (0 0 being the upper left) for the touch event to happen. Just like key events, touch events at a single location require both a down and an up. To simulate dragging, send a “touch down”, then a series of “touch move” events (to simulate the drag), followed by a “touch up” at the final location.
trackball dx dy
This command injects a MotionEvent into the input system that simulates a user using the trackball. dx and dy indicates the amount of change in the trackball location (as opposed to exact coordinates that the touch events use)
flip [open|close]
wake
tap x y
press keycode
type string
getvar varname
quit
done
sleep ms
#
常用函数介绍
以下列表提炼于源码MonkeySourceScript.java
DispatchKey(downTime,eventTime,action,code,repeat,metaState,device,scancode)
@param long downTime //键最初被按下的时间
@param long eventTime //事件发生的时间
@param int action //(ACTION_DOWN=0,ACTION_UP=1,ACTION_MULTIPLE=2)
@param int code //键值,比如KEYCODE_DPAD_DOWN(20)
@param int repeat //
@param int metaState //当前按下的meta键的标识
@param int device //事件发生的设备id
@param int scancode //
DispatchPointer(downTime, eventTime,action, x, y, pressure, size, metaState, xPrecision,yPrecision,device, edgeFlags)
@param long downTime //键最初被按下的时间
@param long eventTime //事件发生的时间
@param int action //(ACTION_DOWN=0,ACTION_MOVE=1,ACTION_UP=2,ACTION_CANCEL=3)
@param float x //x坐标
@param float y //y坐标
@param float pressure //当前事件的压力,范围0-1
@param float size //触摸的近似值,范围0-1
@param int metaState //当前按下的meta键的标识
@param float xPrecision //x坐标精确值
@param float yPrecision //y坐标精确值
@param int device //事件来源,范围0-x,0表示不来自物理设备
@param int sedgeFlags //
DispatchTrackball(downTime, eventTime,action, x, y, pressure, size, metaState, xPrecision,yPrecision,device, edgeFlags)
Tap(x,y,duration);
DispatchPress(String key_name)
DispatchFlip(boolean keyboardOpen)
UserWait(long sleeptime)
LaunchActivity(String pkg_name, String cl_name,long alarmTime)
UserWait(long sleeptime)
LongPress()
示例:
type = user
count = 49
speed = 1.0
start data >>
LaunchActivity(com.android.browser,com.android.browser.BrowserActivity)
UserWait(5000)
#back to home
captureDispatchPointer(5109520,5109520,0,1150,330,0,0,0,0,0,0,0);
captureDispatchPointer(5109521,5109521,1,1150,330,0,0,0,0,0,0,0);
UserWait(3000)
#close browser
captureDispatchPointer(5109520,5109520,0,205,31,0,0,0,0,0,0,0);
captureDispatchPointer(5109521,5109521,1,205,31,0,0,0,0,0,0,0);
UserWait(2000)
adb shell monkey –v –v –v –f /sdcard/script.txt –throttle 1500 100 > monkey.txt
Monkey测试结果分析
Android平台应用程序可能产生以下四种Crash:
主要Monkey日志
一般的测试结果分析:
Monkey测试出现错误后,一般的差错步骤为以下几步:
data_app_wtf
,data_app_anr
,data_app_crash
,system_server_watchdog
Monkey测试的延伸
monkey工具不能实现指定模块测试,以下记录两种实现指定模块测试的方法
方法1: from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from random import randint
print("get device")
device = MonkeyRunner.waitForConnection()
package = ‘com.swl.a1vod‘
activity = ‘org.coolx.videoframework.vod.VodDetailsActivity‘
runComponent = package + ‘/‘ + activity
device.startActivity(component=runComponent)
#use commands like device.touch and device.drag to simulate a navigation and open my activity
#with your activity opened start your monkey test
print("start monkey test")
for i in range(1, 1000):
#here i go emulate only simple touchs, but i can emulate swiper keyevents and more... :D
device.touch(randint(0, 1000), randint(0, 800), ‘DOWN_AND_UP‘)
print("end monkey test")
#执行monkeyrunner G:\script\monkeytest.py
方法2:
<activity android:name="MonkeyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
adb shell monkey -p my.package -c android.intent.category.MONKEY -v 500
更多参考资料
1、Monkey
2、android 常见死机问题–log分析
3、分析bugreport
4、如何分析解决Android ANR
5、Android Tombstone Crash的log分析和定位
6、Android中对Log日志文件的分析
7、Watchdog kills system service in system_server
8、android ANR异常及其解决方式
monkey测试===Android测试工具Monkey用法简介(转载)
标签:模块 语法规则 tool 常用 dom list 命令工具 ref filter
原文地址:http://www.cnblogs.com/botoo/p/7906126.html