您的位置:首页 > 运维架构 > Shell

shell脚本实现自动创建模拟器、启动模拟器、自动安装apk并进行monkey测试

2017-03-13 14:26 886 查看
直接上代码了

#! /bin/bash
cd ~/Desktop/release_apk
mkdir -p log
echo "查找正在运行的设备"
firstDeviceName=`adb devices|sed -n '2p'|awk '{print $1}'`
if [ "$firstDeviceName"x = x ] ;
then
echo "没有发现运行中的模拟器,尝试启动或创建一个新的模拟器"
firstExistDeviceName=`android list avd -c|sed -n '1p'`
if [ "$firstExistDeviceName"x = x ] ;
then
echo "本地不存在模拟器,请创建一个模拟器(请手动执行:android avd)"
exit "402"
# 找到第一个可用的镜像
imageID=`android list targets | tr '\n' '@' | sed 's/-@/#/g' | tr '#' '\n' | grep -v 'no ABIs' | grep -v 'Available' | sed 's/id: \([0-9]*\).*/\1/g'|sed -n '1p'`
if [ "$imageID"x = x ] ;
then
echo "没有找到可用设备,也无法创建模拟器,即将退出monkey测试!!!"
exit 402
else
# 创建模拟器
android create avd -n forMonkey -t $imageID
code=$?
if [ "$code" != "0" ] ;
then
echo "创建模拟器失败code:$code"
exit $code
fi
firstExistDeviceName="forMonkey"
fi
else
echo "本地存在模拟器$firstExistDeviceName"
fi
# 启动模拟器,在独立的进程中,将日志信息保存在launchEmulator.log,不向控制台输出
echo "启动模拟器"
emulator -netdelay none -netspeed full -avd $firstExistDeviceName > log/launchEmulator.log &
echo "启动模拟器中,等待30秒后再次尝试获取运行设备名称"
sleep 30
firstDeviceName=`adb devices|sed -n '2p'|awk '{print $1}'`
echo "再次尝试获取运行中的设备名称是:$firstDeviceName"
fi

echo "安装apk"
lastAPK=`ls -t *.apk | sed -n '1p'`
adb -s $firstDeviceName install -r $lastAPK > log/installAPK.log
code=$?
if [ "$code" = "0" ];
then
echo "安装apk成功"
else
echo "安装apk失败code:$code"
exit "$code"
fi

echo "启动apk"
adb -s $firstDeviceName shell am start -n "com.topglobaledu.uschool/com.topglobaledu.uschool.activities.launchactivity.LaunchActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
code=$?
if [ "$code" = "0" ];
then
echo "启动apk成功"
else
echo "启动apk失败code:$code"
exit "$code"
fi

echo "启动monkey测试"
# --ignore-crashes --ignore-timeouts --monitor-native-crashes  --pct-appswitch 70
adb -s $firstDeviceName shell monkey -p com.topglobaledu.uschool 500
code=$?
if [ "$code" = "0" ];
then
echo "启动monkey成功"
else
echo "启动monkey失败code:$code"
exit "<
4000
span class="hljs-variable">$code"
fi
exit "0"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息