首页 程序猿 软件测试 浏览内容
手机安卓测试之猴子测试
简单的说,猴子测试即为monkey测试
命令如下:
adb shell monkey --help ** Error: Unknown option: --help usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...] [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...] [--ignore-crashes] [--ignore-timeouts] [--ignore-security-exceptions] [--monitor-native-crashes] [--ignore-native-crashes] [--kill-process-after-error] [--hprof] [--pct-touch PERCENT] [--pct-motion PERCENT] [--pct-trackball PERCENT] [--pct-syskeys PERCENT] [--pct-nav PERCENT] [--pct-majornav PERCENT] [--pct-appswitch PERCENT] [--pct-flip PERCENT] [--pct-anyevent PERCENT] [--pkg-blacklist-file PACKAGE_BLACKLIST_FILE] [--pkg-whitelist-file PACKAGE_WHITELIST_FILE] [--wait-dbg] [--dbg-no-events] [--setup scriptfile] [-f scriptfile [-f scriptfile] ...] [--port port] [-s SEED] [-v [-v] ...] [--throttle MILLISEC] [--randomize-throttle] [--profile-wait MILLISEC] [--device-sleep-time MILLISEC] [--randomize-script]
运行monkey可以采用两种方式:系统默认方式和script方式
一、默认运行方式:
adb shell monkey -p package.name -v 30
其中: 可以继续添加一个或者两个 -v 参数, -v参数越多,输出的日志越详细
最后的数字表示,触发的事件次数
为了更好的查看日志,可以将输出的日志信息重定向到文件中
如:adb shell monkey -p package.name -v 30 > log.txt
二、脚本方式
Android 的monkey test 工具提供了 -f scriptfile 参数,可以指定 test 脚本。在 monkey 的源码 MonkeySourceScript.java 中有一小段注释,里面给了一个不到 10 行例子:
/*** 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)* ...*/
monkey中提供的函数如下:
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) 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) DispatchKey(long downTime, long eventTime, int action, int code, int repeat, int metaState, int device, int scancode) DispatchFlip(boolean keyboardOpen) DispatchPress(int keyCode) LaunchActivity(String pkg_name, String cl_name) UserWait(long sleeptime) LongPress(int keyCode)首先本地编写需要的测试的事件 命名为 monkey.script (文件格式无要求),将文件push到手机或模拟器的sdcard中如:adb push lujing/monkey.script /sdcard/然后执行脚本:
adb shell monkey -v -f /sdcard/monkey.script
附 Example:
type= usercount= 49speed= 1.0start data >>LaunchActivity(com.example.android.notepad, com.example.android.notepad.NotesList)DispatchPress(KEYCODE_DPAD_DOWN)LongPress(KEYCODE_DOWN)DispatchPress(KEYCODE_BACK)
其中 type值可以任意,源码中没有对该值做任何处理。
count值,在此无效,还是需要在命令行输入需要执行的次数。因为命令行的count值是必填项