Last active
November 8, 2023 08:09
-
-
Save matpag/f467aa07347f2ece0c3bf5c30976604e to your computer and use it in GitHub Desktop.
Script to run test on a random-generated android emulator with a specific API level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Usage: | |
#You need to have installed the API images you are going to start or you can change the createGoogleEmulator function to start the image you want | |
#For example to start an Android API 28 emulator and perform the app test with Google Apis you just need to call | |
#./run_test_emulator.sh 28 :app:connectedCheck | |
#unlock for debugging purposes | |
# set -ex | |
#global vars | |
#set needed android env folders, we just need the ANDROID_SDK_ROOT set | |
TOOLS=$ANDROID_SDK_ROOT/tools/bin | |
PTOOLS=$ANDROID_SDK_ROOT/platform-tools | |
EMULATOR=$ANDROID_SDK_ROOT/emulator | |
AVD_NAME='' | |
DEVICE_SERIAL='' | |
SECONDS=0 | |
function createGoogleEmulator() { | |
#generate a random string (openssl need to be installed in the machine) | |
AVD_NAME=$(openssl rand -hex 6) | |
#create the emulator with the generated name and the passed api version | |
echo no | $TOOLS/avdmanager create avd --force --name $AVD_NAME --abi google_apis/x86_64 --package "system-images;android-$1;google_apis;x86_64" >/dev/null 2>&1 | |
#check creation result | |
if [ $? -eq 1 ]; then | |
exit 1 | |
fi | |
} | |
function launchEmulator() { | |
#for now use the headless version which has a little performance improvements | |
$EMULATOR/emulator-headless -avd $AVD_NAME -no-skin -no-snapshot -no-boot-anim -no-audio -selinux permissive -prop avdid=$AVD_NAME >/dev/null 2>&1 & | |
#launch the emulator (do not delete, use the line below if the headless version stop working) | |
# $EMULATOR/emulator -avd $AVD_NAME -no-skin -no-snapshot -no-boot-anim -no-audio -no-window -selinux permissive -prop avdid=$AVD_NAME >/dev/null 2>&1 & | |
} | |
function findEmulatorSerial() { | |
while [[ -z "$DEVICE_SERIAL" ]]; do | |
for device in $($PTOOLS/adb devices | awk '$2=="device"{print$1}') | |
do | |
local serial=$device | |
AVDID=$($PTOOLS/adb -s $serial shell getprop avdid | tr -d '\r\n') | |
#ignore emulators which don't have avdid property set | |
if [ -z "$AVDID" ]; then | |
echo "device $serial skipped because no avdid found" | |
continue | |
fi | |
if [ "$AVDID" == "$AVD_NAME" ]; then | |
#wait for device and unlock it | |
echo "device $serial : waiting device boot completed event" | |
$PTOOLS/adb -s $serial wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done; input keyevent 82' | |
DEVICE_SERIAL=$serial | |
break | |
fi | |
done | |
sleep 1; | |
done | |
} | |
#----------- | |
if [ -z "$1" ] || [ -z "$2" ]; | |
then | |
echo "No arguments supplied, you need to provide the Android api version and the task to execute" | |
exit 1 | |
fi | |
createGoogleEmulator $1 | |
launchEmulator | |
findEmulatorSerial | |
#perform tests setting the emulator to use by ./gradlew tests with | |
export ANDROID_SERIAL=$DEVICE_SERIAL | |
#$2 should be something like :app:connectedCheck | |
./gradlew $2 | |
#save the exit code of tests | |
testExitCode=$? | |
#close emulator | |
$PTOOLS/adb -s $DEVICE_SERIAL emu kill | |
#delete the avd | |
$TOOLS/avdmanager delete avd -n $AVD_NAME >/dev/null 2>&1 | |
#line took from here: https://unix.stackexchange.com/a/340156/303526 | |
echo "Elapsed: $(($SECONDS / 3600))hrs $((($SECONDS / 60) % 60))min $(($SECONDS % 60))sec" | |
#pass the test result value to the caller | |
exit $testExitCode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script has a lot of improvement points but it's a good starting base for customizations