Skip to content

Commit ae21e26

Browse files
committed
Ensure emulator is ready before running instrumentation tests
1 parent 49cca52 commit ae21e26

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

scripts/run-android-instrumentation-tests.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,54 @@ if [ -z "$ANDROID_SDK_ROOT" ] || [ ! -d "$ANDROID_SDK_ROOT" ]; then
4545
fi
4646
export ANDROID_SDK_ROOT ANDROID_HOME="$ANDROID_SDK_ROOT"
4747

48+
ADB_BIN="$(command -v adb || true)"
49+
if [ -z "$ADB_BIN" ] && [ -x "$ANDROID_SDK_ROOT/platform-tools/adb" ]; then
50+
ADB_BIN="$ANDROID_SDK_ROOT/platform-tools/adb"
51+
fi
52+
if [ -z "$ADB_BIN" ]; then
53+
ra_log "adb executable not found in PATH or Android SDK" >&2
54+
exit 1
55+
fi
56+
"$ADB_BIN" start-server >/dev/null 2>&1 || true
57+
ra_log "Connected Android devices before selecting target:"
58+
"$ADB_BIN" devices -l || true
59+
60+
DEVICE_SERIAL="${ANDROID_SERIAL:-}"
61+
if [ -z "$DEVICE_SERIAL" ]; then
62+
DEVICE_SERIAL=$("$ADB_BIN" devices | awk 'NR>1 && $2=="device" {print $1; exit}')
63+
fi
64+
if [ -z "$DEVICE_SERIAL" ]; then
65+
ra_log "No booted Android emulator/device detected" >&2
66+
exit 1
67+
fi
68+
69+
ADB_TARGET=("$ADB_BIN" -s "$DEVICE_SERIAL")
70+
adb_target() { "${ADB_TARGET[@]}" "$@"; }
71+
72+
ra_log "Using Android device serial $DEVICE_SERIAL"
73+
adb_target wait-for-device
74+
75+
wait_for_property() {
76+
local property="$1" expected="$2" attempts="${3:-60}" delay="${4:-5}" value=""
77+
for attempt in $(seq 1 "$attempts"); do
78+
value="$(adb_target shell getprop "$property" 2>/dev/null | tr -d '\r')"
79+
if [ "$value" = "$expected" ]; then
80+
return 0
81+
fi
82+
sleep "$delay"
83+
done
84+
ra_log "Timed out waiting for $property to become $expected (last value: ${value:-<unset>})" >&2
85+
return 1
86+
}
87+
88+
ra_log "Waiting for emulator to finish booting"
89+
wait_for_property sys.boot_completed 1 120 5
90+
wait_for_property dev.bootcomplete 1 120 5 || true
91+
adb_target shell input keyevent 82 >/dev/null 2>&1 || true
92+
ra_log "Device build fingerprint: $(adb_target shell getprop ro.build.fingerprint | tr -d '\r')"
93+
ra_log "Installed instrumentation targets:"
94+
adb_target shell pm list instrumentation || true
95+
4896
if [ ! -d "$GRADLE_PROJECT_DIR" ]; then
4997
ra_log "Gradle project directory not found: $GRADLE_PROJECT_DIR" >&2
5098
exit 1

0 commit comments

Comments
 (0)