一、背景
当应用发现app在启动的时候会出现问题,但是需要手动测试很多次才可以复现的问题,可以考虑写一个自动化测试脚本。
二、Android启动和关闭app命令
1、启动应用需要知道应用的包名和main activity类名
adb shell am start 包名/activity类名
2、关闭应用
adb shell am force-stop 包名
三、在shell中获取adb命令执行后的结果
可以通过如下方式将adb命令的结果赋值给变量
pid=`adb shell pidof 包名`
四、运行app和关闭app的代码
当检测到应用crash会自动停止运行
for((i = 1; i <= 1000; i++ ));
do
# 启动应用
adb shell am start 包名/activity类名
echo "Run the app ${i} time."
# 判断应用是否启动,如果未启动等待5秒后重新启动
for((j = 1; j <= 1000; j++ ));
do
pid=`adb shell pidof 包名`
if [ ! -n "$pid" ];
then
# 重试10次重新启动
if [ $j -gt 10 ];
then
adb shell am start 包名/activity类名
fi
sleep 0.5s
else
break
fi
done
# 等待5秒启动activity
sleep 5s
# 根据进程是否存在判断是否crash
pid=`adb shell pidof 包名`
if [ ! -n "$pid" ];
then
echo "start crash"
break
else
echo "start res: ${pid}"
fi
# 关闭应用
adb shell am force-stop 包名
sleep 1s
done
更多推荐
Android shell脚本自动启动app,关闭app,遇到crash自动停止
发布评论