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

来源:互联网 发布:erp软件购买ufsky 编辑:程序博客网 时间:2024/05/29 15:57

直接上代码了

#! /bin/bashcd ~/Desktop/release_apkmkdir -p logecho "查找正在运行的设备"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"fiecho "安装apk"lastAPK=`ls -t *.apk | sed -n '1p'`adb -s $firstDeviceName install -r $lastAPK > log/installAPK.logcode=$?if [ "$code" = "0" ];    then        echo "安装apk成功"    else        echo "安装apk失败code:$code"        exit "$code"fiecho "启动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.LAUNCHERcode=$?if [ "$code" = "0" ];    then        echo "启动apk成功"    else        echo "启动apk失败code:$code"        exit "$code"fiecho "启动monkey测试"# --ignore-crashes --ignore-timeouts --monitor-native-crashes  --pct-appswitch 70adb -s $firstDeviceName shell monkey -p com.topglobaledu.uschool 500code=$?if [ "$code" = "0" ];    then        echo "启动monkey成功"    else        echo "启动monkey失败code:$code"        exit "$code"fiexit "0"
1 0