一、使用安卓手机,配置环境连接adb工具,确保adb连接成功后
具体步骤可参考ADB的安装及常用命令_weixin_44240224的博客-CSDN博客
二、因测试多针对一个特定的APP包,所以需要知道需要测试包的包名;可以直接问开发,也可以通过 adb shell pm list packages 命令来查看手机本地所有的package包。
三、对特定APP包进行测试的命令为adb shell monkey -p <pakage.name>
参数解释:
#package_name:被测应用的包名
#times:想要monkey模拟的事件次数,具体事件是什么请参考monkey概念中提到的,辅助了解
# -p:此参数后面附包名,如果不指定该参数,monkey测试会随机选取手机系统应用测试
# -v :此参数代表monkey测试过程中生成日志的详细程度,详细程度分为3个等级,具体对应参数写法为:-v;-vv;-vvv
具体场景以手机中的阅读app为例参考,返回的信息如下:
lemon@lemondeMacBook-Air ~ % adb shell monkey -p com.duokan.reader -v 100
bash arg: -p
bash arg: com.duokan.reader
bash arg: -v
bash arg: 100
args: [-p, com.duokan.reader, -v, 100]
arg: "-p"
arg: "com.duokan.reader"
arg: "-v"
arg: "100"
data="com.duokan.reader"
:Monkey: seed=1645781479183 count=100
:AllowPackage: com.duokan.reader
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Event percentages:
// 0: 15.0%
// 1: 10.0%
// 2: 2.0%
// 3: 15.0%
// 4: -0.0%
// 5: -0.0%
// 6: 25.0%
// 7: 15.0%
// 8: 2.0%
// 9: 2.0%
// 10: 1.0%
// 11: 13.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.duokan.reader/.DkReaderActivity;end
// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.duokan.reader/.DkReaderActivity } in package com.duokan.reader
// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.duokan.reader/.DkReaderActivity } in package com.duokan.reader
:Sending Touch (ACTION_DOWN): 0:(507.0,1163.0)
:Sending Touch (ACTION_UP): 0:(510.75403,1138.7877)
:Sending Touch (ACTION_DOWN): 0:(609.0,2029.0)
:Sending Touch (ACTION_UP): 0:(600.02954,2029.0245)
:Sending Touch (ACTION_DOWN): 0:(349.0,1765.0)
:Sending Touch (ACTION_UP): 0:(335.8255,1765.1145)
:Sending Touch (ACTION_DOWN): 0:(1030.0,833.0)
:Sending Touch (ACTION_UP): 0:(1024.7245,831.83484)
:Sending Touch (ACTION_DOWN): 0:(882.0,971.0)
:Sending Touch (ACTION_UP): 0:(885.56946,970.6212)
:Sending Touch (ACTION_DOWN): 0:(523.0,1803.0)
:Sending Touch (ACTION_UP): 0:(524.204,1796.2349)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,-2.0)
:Sending Touch (ACTION_DOWN): 0:(477.0,1372.0)
:Sending Touch (ACTION_UP): 0:(477.838,1385.8202)
:Sending Trackball (ACTION_MOVE): 0:(1.0,1.0)
:Sending Touch (ACTION_DOWN): 0:(348.0,1948.0)
:Sending Touch (ACTION_UP): 0:(302.77643,1995.5365)
:Sending Touch (ACTION_DOWN): 0:(562.0,2158.0)
:Sending Touch (ACTION_UP): 0:(576.36725,2175.0)
:Sending Trackball (ACTION_MOVE): 0:(-1.0,1.0)
:Sending Touch (ACTION_DOWN): 0:(151.0,1360.0)
:Sending Touch (ACTION_UP): 0:(155.80309,1360.2347)
:Sending Trackball (ACTION_MOVE): 0:(3.0,-5.0)
:Sending Touch (ACTION_DOWN): 0:(601.0,432.0)
:Sending Touch (ACTION_UP): 0:(612.4258,436.2944)
:Sending Trackball (ACTION_MOVE): 0:(-1.0,-4.0)
:Sending Trackball (ACTION_UP): 0:(0.0,0.0)
:Sending Touch (ACTION_DOWN): 0:(559.0,432.0)
// Rejecting start of Intent { act=android.intent.action.VIEW dat=https://privacy.mi.com/... cmp=com.android.browser/.BrowserActivity } in package com.android.browser
// Rejecting start of Intent { act=android.intent.action.VIEW dat=https://privacy.mi.com/... cmp=com.android.browser/.BrowserActivity } in package com.android.browser
Events injected: 100
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=352ms (0ms mobile, 0ms wifi, 352ms not connected)
// Monkey finished
这时连接的手机就会随机自动操作了