seed表示monkey运行时生成的一系列随机事件的标志值,
比如monkey第一次运行在app内打开A页面,seed值是111,第二次打开app,运行monkey命令,seed值设置111,运行的结果和上次一样,也是打开A页面。
也就是说,如果一系列操作产生了bug,我们又记不清楚操作路径,那么只需要拿到 seed 值,再去运行,就会重现相同的操作路径,一些必现的 bug是可以找到操作步骤的。
背景:monkey跑完一个seed之后,会停在一个页面上不动。如果我们想在一个 duration 内随机跑很多个seed,在不同的页面运行,则需要跑完一个seed强制退出app,下次再重启app,随机在其他页面跑另外一个seed
//循环跑多个seed
while (System.currentTimeMillis() < monkeyEndTimer) {
runLoop++;
String cmd = "monkey -p " + pkgName + " --throttle 300 -c android.intent.category.MONKEY -c android.intent.category.LAUNCHER -c android.intent.category.DEFAULT --monitor-native-crashes --kill-process-after-error --pct-touch 45 --pct-motion 20 --pct-majornav 5 --pct-appswitch 30 -v -v -v 5000";
System.out.println("cmd is " + cmd);
// 退出app,回到home界面
for (int i = 0; i < 10; i++) {
targetDevice.executeShellCommand("input keyevent 4", log);
Thread.sleep(1000);
System.out.println("Press back key to go home page.");
}
//强制退出app
targetDevice.executeShellCommand("am force-stop " + pkgName , log);
// 开始运行monkey测试
targetDevice.executeShellCommand(cmd, log);
// 结束标志,为了分析log用
targetDevice.executeShellCommand("cat /proc/meminfo", log);
}
电脑只连一个设备时,是没有问题的,但是如果连接了多个设备的话,需要从中找到指定的目标设备
做法是遍历所有连接的设备,每个设备的deviceID和需要跑monkey的deviceID作比对,找到一致的deviceID为 targetDevice
//获取目标设备
IDevice[] mDevice = adb.getDevices();
IDevice targetDevice = null;
for (int i = 0; i < mDevice.length; i++) {
System.out.println(mDevice[i].getSerialNumber());
if (mDevice[i].getSerialNumber().equals(deviceID)) {
targetDevice = mDevice[i];
}
}
电脑只连一个设备时,先“获取目标设备”,再“循环跑多个seed”,在预定时间内是可以不断循环跑的。
但是连接多个设备时,指定设备跑完一个seed之后,跑下一个循环时,就找不到设备了。
开始在网上查的以为是端口被占用,修改了端口多次尝试没有解决(这里记一个坑:在修改端口后,adb启动默认的还是原来的端口,猜测原因可能是修改的时候,原来连接的设备没有断开,服务没有杀死再重启,待验证)
最后找到了解决方案,将获取目标设备封装成一个方法,在“循环跑多个seed”里面,跑下一个seed前每次调用这个方法,去重新获取一下设备
//识别目标设备
public static IDevice getDevice(String deviceID,AndroidDebugBridge adb){
IDevice[] mDevice = adb.getDevices();
IDevice targetDevice = null;
for (int i = 0; i < mDevice.length; i++) {
System.out.println(mDevice[i].getSerialNumber());
if (mDevice[i].getSerialNumber().equals(deviceID)) {
targetDevice = mDevice[i];
}
}
if (targetDevice == null) {
System.out.println("Can't find the target device " + deviceID);
System.exit(0);
}
return targetDevice;
}
//循环跑多个seed
while (System.currentTimeMillis() < monkeyEndTimer) {
runLoop++;
//循环每跑完一个seed查找并连接一次设备
IDevice targetDevice = getDevice(deviceID,adb);
String cmd = "monkey -p " + pkgName + " --throttle 300 -c android.intent.category.MONKEY -c android.intent.category.LAUNCHER -c android.intent.category.DEFAULT --monitor-native-crashes --kill-process-after-error --pct-touch 45 --pct-motion 20 --pct-majornav 5 --pct-appswitch 30 -v -v -v 5000";
System.out.println("cmd is " + cmd);
// 退出app,回到home界面
for (int i = 0; i < 10; i++) {
targetDevice.executeShellCommand("input keyevent 4", log);
Thread.sleep(1000);
System.out.println("Press back key to go home page.");
}
//强制退出app
targetDevice.executeShellCommand("am force-stop " + pkgName , log);
// 开始运行monkey测试
targetDevice.executeShellCommand(cmd, log);
// 结束标志,为了分析log用
targetDevice.executeShellCommand("cat /proc/meminfo", log);
}