Monkey与MonkeyRunner之间的区别
为了支持黑盒自动化测试的场景,Android SDK提供了monkey和monkeyrunner两个测试工具,这两个测试工具除了名字类似外,还都可以向待测应用发送按键等消息,往往容易产生混淆,以下是他俩的不同之处。
1.monkey运行在设备或者模拟器上边,可以脱离PC运行,其运行时如下图所示。
而monkeyrunner运行在PC上,需要通过服务器/客户端的的模式向设备或者模拟器上的android应用发送指令来执行测试,其运行时如下图所示。
2.普遍的做法是将monkey作为一个向待测应用发送随机按键消息的测试工具,验证待测应用在这些随机性的输入面前是否会有闪退或者崩溃。而monkeyrunner则接受一个明确的测试脚本(使用python语言编写的)。
3.虽然monkey也可以根据一个指定的命令脚本发送按键消息,但其不支持条件判断,也不支持读取界面的信息来执行验证操作。而monkeyrunner的测试脚本中有明确 的条件判断等语句,可用来做功能测试。
总结:
实际操作中,monkey由于缺少必要的条件判断等命令,难以在功能测试上有所作为,只能作为生成一些随机事件的工具,测试应用程序的健壮程度,待测应用崩溃后可以根据monkey打印的日志,再用monkey创建一个重现步骤,供开发调试。monkey服务器模式更适合用于黑盒测试,不建议用于自动化测试。
而Monkeyrunner虽然有Python和Java类库的强大支持,但其自身提供的API有限,还得需要插件扩展其功能。
monkey_test_tool.bat 文件代码:
@echo OFF
title Itel OS-Monkey Test
echo 等待设备连接...
adb version
adb kill-server
adb wait-for-device
adb devices
cls
echo 设备已连接...
echo ---------------------------------------------------------
echo 手机型号: %model%
echo 系统版本: Android %androidOS%
echo ROM 版本: %Rom_version%
echo ---------------------------------------------------------
echo ============Monkey测试前准备==============
adb shell settings put system screen_brightness_mode 0
echo 1.关闭屏幕自动调节亮度...
adb shell settings put system screen_brightness 0
echo 2.屏幕亮度设置为0...
adb shell settings put global auto_time 1
echo 3.时间设置为网络自动同步获取...
adb shell settings put system screen_off_timeout 1800000
echo 4.设置屏幕休眠时长为30min...
echo ---------------------------------------------------------
echo 5. 删除旧的/mnt/sdcard/monkeyResult日志
adb shell rm -rf /mnt/sdcard/monkeyResult
echo 6. 创建新的/mnt/sdcard/monkeyResult文件夹
adb shell mkdir /mnt/sdcard/monkeyResult
echo ---------------------------------------------------------
echo 需要手动开启手机离线日志
echo adb shell am start -n com.app.UserLog/com.app.UserLog.UserLogSetting
echo ---------------------------------------------------------
::============各模块包命名==============
set "app1=-p com.zhuishu.reader"
set "app2=-p com.talpa.messagebox"
set "app3=-p com.talpa.weather"
set "app4=-p com.android.gallery3d"
set "app5=-p com.android.dialer"
set "app6=-p com.android.deskclock"
set "app7=-p com.android.battery"
set "app8=-p com.android.netmannager"
set "app9=-p com.android.mms"
set "appA=-p com.transsion.translate"
set "appB=-p tran.com.android.tapla.gamecenter"
set "appC=-p com.android.talpa.gamemode"
set "appD=-p com.android.contacts"
set "appE=-p com.transsion.filemanager"
set "appF=-p com.opera.branding"
set "appG=-p com.facebook.system"
set "appH=-p com.google.android.youtube"
set "appI=-p com.hungama.movie"
set "appJ=-p com.android.chrome"
set "appK=-p com.oupeng.mini.android"
set "appL=-p cn.xender"
set "appM=-p com.UCMobile"
set "appO=-p com.talpa.instore"
::==============================================
echo.【模块列表】
echo.
echo 1.追书宝 2.messagebox 3.天气 4.图库(系统) 5.电话
echo 6.时钟 7.省电管理 8.流量管理 9.信息
echo.
echo A.翻译 B.游戏中心 C.游戏模式 D.联系人(系统) E.文件管理器(系统)
echo F.Opera Brand G.facebook H.youtube I.com.hungama.movie G.chrome
echo K.Opera Mini L.Xender M.UC O.InStore
echo ---------------------------------------------------------
::变量延迟的启动
SetLocal EnableDelayedExpansion
:selectmodel
::设置测试时长
set testTime=980000000
set choice1=0
set choice2=0
set choice3=0
set choice4=0
set choice5=0
set choice6=0
set selmodel= 0
set logtime=%date:~0,4%-%date:~5,2%-%date:~8,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
echo.【测试模式】
echo.
echo [整机测试:1]
echo [单模块:2]
echo [双模块:3]
echo [多模块:4]
echo [OS模块:5]
echo [手动输入单个测试应用包名:6]
echo.
set /p selmodel=》》请选择测试模式:
::echo %selmodel%
echo.
::双模块
if %selmodel% == 3 (
set /p choice1=》》 请选择待测模块1:
set /p choice2=》》 请选择待测模块2:
goto RunMonkey
)
::多模块
if %selmodel% == 4 (
set /p choice1= 》》请选择待测模块1:
set /p choice2= 》》请选择待测模块2:
set /p choice3= 》》请选择待测模块3:
set /p choice4= 》》请选择待测模块4:
goto RunMonkey
)
::整机测试
if %selmodel% == 1 (
goto RunMonkey
)
::单模块
if %selmodel% == 2 (
set /p choice2=》》请选择待测单个模块:
goto RunMonkey
)
::OS模块
if %selmodel% == 5 (
goto RunMonkey2
)
::手动输入单个测试应用包名
if %selmodel% == 6 (
set /p choice6=》》请输入待测完整应用包名:
goto RunMonkey3
)
goto selectmodel
:RunMonkey
echo ---------------------------------------------------------
echo √已经启动Monkey测试,测试模式=%selmodel% 【 !app%choice1%! !app%choice2%! !app%choice3%! !app%choice4%!】 测试时长=%testTime%
echo ---------------------------------------------------------
echo √日志打印存放到手机内置存储/sdcard/monkeyResult文件夹下:error.txt、logcat-xx-xx-xx.txt、Memory-xx-xx-xx.txt
echo ---------------------------------------------------------
adb shell "monkey --pkg-whitelist-file /data/local/tmp/whitelist.txt --throttle 300 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v %testTime% 2>/mnt/sdcard/monkeyResult/error.txt >/mnt/sdcard/monkeyResult/mokeyinfo.txt & logcat -v time >/mnt/sdcard/monkeyResult/Logcat%logtime%.txt & top -m 20 >/mnt/sdcard/monkeyResult/Memory%logtime%.txt &"
::!app%choice1%! !app%choice2%! !app%choice3%! !app%choice4%!
echo.
pause
exit
:RunMonkey2
echo ---------------------------------------------------------
echo √已经启动Monkey测试,测试模式=%selmodel% 测试时长=%testTime%
echo ---------------------------------------------------------
echo √日志打印存放到手机内置存储/sdcard/monkeyResult文件夹下:error.txt、logcat-xx-xx-xx.txt、Memory-xx-xx-xx.txt
echo ---------------------------------------------------------
adb shell "monkey %app3% %app5% %app6% %app7% %app8% %app9% %appA% %appB% %appC% -s 50000 --throttle 300 --pkg-whitelist-file --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v %testTime% 2>/mnt/sdcard/monkeyResult/error.txt >/mnt/sdcard/monkeyResult/mokeyinfo.txt & logcat -v time >/mnt/sdcard/monkeyResult/Logcat%logtime%.txt & top -m 20 >/mnt/sdcard/monkeyResult/Memory%logtime%.txt &"
echo.
pause
exit
:RunMonkey3
echo ---------------------------------------------------------
echo √已经启动Monkey测试,测试模式=%selmodel% 测试时长=%testTime%
echo ---------------------------------------------------------
echo √日志打印存放到手机内置存储/sdcard/monkeyResult文件夹下:error.txt、logcat-xx-xx-xx.txt、Memory-xx-xx-xx.txt
::--pkg-whitelist-file /data/whitelist.txt
echo ---------------------------------------------------------
adb shell "monkey -p %choice6% -s 50000 --throttle 300 --pkg-whitelist-file --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v %testTime% 2>/mnt/sdcard/monkeyResult/error.txt >/mnt/sdcard/monkeyResult/mokeyinfo.txt & logcat -v time >/mnt/sdcard/monkeyResult/Logcat%logtime%.txt & top -m 20 >/mnt/sdcard/monkeyResult/Memory%logtime%.txt &"
echo.
pause
exit