幾個關鍵命令
1.查找一個已經連接上電腦的設備(當然了,手機得是開啓了調試模式的狀態)
firstDeviceName=`adb devices|sed -n '2p'|awk '{print $1}'`
2.如果沒有找到連接的設備,則自動開啓模擬器
firstExistDeviceName=`android list avd -c|sed -n '1p'`
3.啓動模擬器
emulator -netdelay none -netspeed full -avd $firstExistDeviceName
4.安裝的命令應該是很容易理解了
adb -s $firstDeviceName install -r $lastAPK
5.啓動apk
adb -s $firstDeviceName shell am start -n "你的應用的包名/第一個activity全路徑" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
6.啓動monkey
adb -s $firstDeviceName shell monkey -p 你的包名 自動發生事件的次數
再看看完整代碼
#! /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.jian.test/com.jian.xxx.activities.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 "$code"
fi
exit "0"