實現android自動化測試部署與運行Shell指令碼分享_linux shell

來源:互聯網
上載者:User

我的配置是linux 64, android4.2.2的sdk。 實現的細節都在代碼注釋裡了,變數名以及echo的內容也是說明的一部分。

主流程為:

1.檢測是否指定連接埠的模擬器已經運行,若有則關閉
2.建立模擬器
3.啟動模擬器
4.用ant build包,通過檢查bin目錄下有沒有apk來確定是否成功
5.檢查指定連接埠的模擬器是否已正常運行
6.用adb安裝apk
7.啟動apk的activity,讓程式自己跑
8.用shell ps迴圈檢測程式是否已退出,若已退出,檢查是否sdcard是是否已成功產生報告檔案
9.若報告檔案不完整,即程式被中止/崩潰,重啟activity
10.檢測是否逾時

複製代碼 代碼如下:

#!/bin/bash
# by hursing 2013-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 "close emulator window"
 kill -9 $emulatorPID
fi

# 建立模擬器,force選項能覆蓋已存在的模擬器,即sdcard內容也抹掉。echo no是回答不需要自訂硬體設定
echo "create avd"
emulatorName="Test"
echo no | android create avd --name $emulatorName --force --target android-17 --sdcard 300M --abi armeabi-v7a

# 啟動模擬器,port選項指定tcp連接埠。&是另起進程執行
echo "start avd"
emulator -avd $emulatorName -port $portNumber &

# 沒有認證簽名,所以是debug
echo "build apk"
android update project -p .
ant debug
if [ $? -ne 0 ]; then
 echo "build failed, try again now..."
 ant clean
 ant debug
 if [ $? -ne 0 ]; then
  echo "can NOT build apk!"
  exit 5
 fi
fi
apkFile=`find ./bin/*.apk | grep -v "unaligned"`
if [ -z "$apkFile" ]; then
 echo "no apk file generated!"
 exit 7
fi

# wait-for-device能block住指令碼直到不顯示offline
echo "check emulator ready..."
emulatorSerialNumber="emulator-$portNumber"
ready=`adb devices | grep "$emulatorSerialNumber"`
if [ -z "$ready" ]; then
 echo "emulator can NOT launch."
 exit 3
else
 ready=`adb devices | grep "$emulatorSerialNumber.*offline"`
 if [ -n "$ready" ]; then
  echo "wait-for-device"
  adb -s $emulatorSerialNumber wait-for-device
  sleep 10
 fi
fi

# 當adb可以安裝時,模擬器未必已經初始化完成,還是會失敗的,所以要檢測error,試3遍。
echo "install apk"
let maxTryTimes=3
while [ $maxTryTimes -gt 0 ]; do
 result=`adb -s $emulatorSerialNumber install $apkFile`  # 'ant installd' can NOT specify device
 check=`echo "$result" | grep -o "Error"`
 if [ -z "$check" ]; then
  echo "successfully install apk"
  break;
 else
  echo "$result"
  echo "install failed, try again after sleeping 10 seconds"
  sleep 10
  let maxTryTimes-=1
 fi
done
if [ $maxTryTimes -eq 0 ]; then
 echo "can NOT install apk to emulator."
 exit 4
fi

echo "start MainActivity"
activityName="com.hursing.MainActivity"
packageName=`cat AndroidManifest.xml | grep -o "package=\".*\"" | sed 's/package="//;s/"//'`
adb -s $emulatorSerialNumber shell am start -W -n "$packageName/$activityName"

heartBeat=10
echo "waiting for test finished.check every $heartBeat seconds."
resultFile="/sdcard/result.txt"
successFlag="End at "
sleep $heartBeat
let maxTimeLimit=60*10
while [ $maxTimeLimit -gt 0 ]; do
 running=`adb -s $emulatorSerialNumber shell ps | grep $packageName`
 if [ -n "$running" ]; then
  let maxTimeLimit-=$heartBeat
  echo "waiting, sleep another $heartBeat seconds..."
  sleep $heartBeat
 else
  report=`adb -s $emulatorSerialNumber shell cat "$resultFile"`
  check=`echo "$report" | grep -o "$successFlag"`
  if [ -z "$check" ]; then
   echo "app crash, run activity again..."
   adb -s $emulatorSerialNumber shell am start -W -n "$packageName/$activityName"
  else
   echo "app exit normally"
   break
  fi
 fi
done

echo "close emulator window."
kill -9 `ps -ef | grep "emulator" | grep "port $portNumber" | awk '{print $2;}'`

if [ $maxTimeLimit -eq 0 ]; then
 echo "Test timeout!"
 echo "$report"
 exit 9
else
 echo "Test Finished!"
 echo
 echo "$report"
fi

java代碼裡有一點要注意,如果程式崩潰,彈出了"has stopped”對話方塊,那麼ps還是能查到的,要自己調用

複製代碼 代碼如下:

Thread.setDefaultUncaughtExceptionHandler

,在handler裡直接System.exit(0)才行。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.