实现android自动化测试部署与运行Shell脚本分享
我的配置是linux64,android4.2.2的sdk。实现的细节都在代码注释里了,变量名以及echo的内容也是说明的一部分。
主流程为:
1.检测是否指定端口的模拟器已经运行,若有则关闭
2.创建模拟器
3.启动模拟器
4.用antbuild包,通过检查bin目录下有没有apk来确定是否成功
5.检查指定端口的模拟器是否已正常运行
6.用adb安装apk
7.启动apk的activity,让程序自己跑
8.用shellps循环检测程序是否已退出,若已退出,检查是否sdcard是是否已成功生成报告文件
9.若报告文件不完整,即程序被中止/崩溃,重启activity
10.检测是否超时
#!/bin/bash #byhursing2013-10-28 #指定port,可以不干扰其它正在运行的模拟器。根据sdk文档,port可以在5554至5584之间,最好是偶数 portNumber=5578 emulatorPID=`ps-ef|grep"emulator"|grep"port$portNumber"|awk'{print$2;}'` if[-n"$emulatorPID"];then echo"closeemulatorwindow" kill-9$emulatorPID fi
#创建模拟器,force选项能覆盖已存在的模拟器,即sdcard内容也抹掉。echono是回答不需要自定义硬件配置 echo"createavd" emulatorName="Test" echono|androidcreateavd--name$emulatorName--force--targetandroid-17--sdcard300M--abiarmeabi-v7a
#启动模拟器,port选项指定tcp端口。&是另起进程执行 echo"startavd" emulator-avd$emulatorName-port$portNumber&
#没有证书签名,所以是debug echo"buildapk" androidupdateproject-p. antdebug if[$?-ne0];then echo"buildfailed,tryagainnow..." antclean antdebug if[$?-ne0];then echo"canNOTbuildapk!" exit5 fi fi apkFile=`find./bin/*.apk|grep-v"unaligned"` if[-z"$apkFile"];then echo"noapkfilegenerated!" exit7 fi
#wait-for-device能block住脚本直到不显示offline echo"checkemulatorready..." emulatorSerialNumber="emulator-$portNumber" ready=`adbdevices|grep"$emulatorSerialNumber"` if[-z"$ready"];then echo"emulatorcanNOTlaunch." exit3 else ready=`adbdevices|grep"$emulatorSerialNumber.*offline"` if[-n"$ready"];then echo"wait-for-device" adb-s$emulatorSerialNumberwait-for-device sleep10 fi fi
#当adb可以安装时,模拟器未必已经初始化完成,还是会失败的,所以要检测error,试3遍。 echo"installapk" letmaxTryTimes=3 while[$maxTryTimes-gt0];do result=`adb-s$emulatorSerialNumberinstall$apkFile` #'antinstalld'canNOTspecifydevice check=`echo"$result"|grep-o"Error"` if[-z"$check"];then echo"successfullyinstallapk" break; else echo"$result" echo"installfailed,tryagainaftersleeping10seconds" sleep10 letmaxTryTimes-=1 fi done if[$maxTryTimes-eq0];then echo"canNOTinstallapktoemulator." exit4 fi
echo"startMainActivity" activityName="com.hursing.MainActivity" packageName=`catAndroidManifest.xml|grep-o"package=\".*\""|sed's/package="//;s/"//'` adb-s$emulatorSerialNumbershellamstart-W-n"$packageName/$activityName"
heartBeat=10 echo"waitingfortestfinished.checkevery$heartBeatseconds." resultFile="/sdcard/result.txt" successFlag="Endat" sleep$heartBeat letmaxTimeLimit=60*10 while[$maxTimeLimit-gt0];do running=`adb-s$emulatorSerialNumbershellps|grep$packageName` if[-n"$running"];then letmaxTimeLimit-=$heartBeat echo"waiting,sleepanother$heartBeatseconds..." sleep$heartBeat else report=`adb-s$emulatorSerialNumbershellcat"$resultFile"` check=`echo"$report"|grep-o"$successFlag"` if[-z"$check"];then echo"appcrash,runactivityagain..." adb-s$emulatorSerialNumbershellamstart-W-n"$packageName/$activityName" else echo"appexitnormally" break fi fi done
echo"closeemulatorwindow." kill-9`ps-ef|grep"emulator"|grep"port$portNumber"|awk'{print$2;}'`
if[$maxTimeLimit-eq0];then echo"Testtimeout!" echo"$report" exit9 else echo"TestFinished!" echo echo"$report" fi