Skip to content

Commit c64a649

Browse files
committed
Capture emulator screenshot in Android workflow
1 parent ae21e26 commit c64a649

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

.github/workflows/scripts-android.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ jobs:
3636
arch: x86_64
3737
target: google_apis
3838
script: |
39-
./scripts/run-android-instrumentation-tests.sh "${{ steps.build-android-app.outputs.gradle_project_dir }}"
39+
./scripts/run-android-instrumentation-tests.sh "${{ steps.build-android-app.outputs.gradle_project_dir }}" "${{ steps.build-android-app.outputs.package_name }}"
40+
- name: Upload emulator screenshot
41+
if: ${{ success() }}
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: android-emulator-screenshot
45+
path: out/android-emulator/hello-codenameone.png
46+
if-no-files-found: error

scripts/build-android-app.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ if [ -n "${GITHUB_OUTPUT:-}" ]; then
370370
echo "gradle_project_dir=$GRADLE_PROJECT_DIR"
371371
echo "apk_path=$APK_PATH"
372372
echo "instrumentation_test_class=$PACKAGE_NAME.HelloCodenameOneInstrumentedTest"
373+
echo "package_name=$PACKAGE_NAME"
373374
} >> "$GITHUB_OUTPUT"
374375
ba_log "Published GitHub Actions outputs for downstream steps"
375376
fi

scripts/run-android-instrumentation-tests.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ set -euo pipefail
55
ra_log() { echo "[run-android-instrumentation-tests] $1"; }
66

77
if [ $# -lt 1 ]; then
8-
ra_log "Usage: $0 <gradle_project_dir>" >&2
8+
ra_log "Usage: $0 <gradle_project_dir> [package_name]" >&2
99
exit 1
1010
fi
1111

1212
GRADLE_PROJECT_DIR="$1"
13+
PACKAGE_NAME="${2:-}"
1314

1415
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1516
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
@@ -93,6 +94,13 @@ ra_log "Device build fingerprint: $(adb_target shell getprop ro.build.fingerprin
9394
ra_log "Installed instrumentation targets:"
9495
adb_target shell pm list instrumentation || true
9596

97+
if [ -z "$PACKAGE_NAME" ]; then
98+
PACKAGE_NAME="$(adb_target shell pm list instrumentation 2>/dev/null | sed -n 's/.*target=\([^)]*\)).*/\1/p' | tr -d '\r' | head -n 1 || true)"
99+
if [ -n "$PACKAGE_NAME" ]; then
100+
ra_log "Detected application package from instrumentation list: $PACKAGE_NAME"
101+
fi
102+
fi
103+
96104
if [ ! -d "$GRADLE_PROJECT_DIR" ]; then
97105
ra_log "Gradle project directory not found: $GRADLE_PROJECT_DIR" >&2
98106
exit 1
@@ -110,3 +118,35 @@ ORIGINAL_JAVA_HOME="${JAVA_HOME:-}"; export JAVA_HOME="$JAVA17_HOME"
110118
)
111119
export JAVA_HOME="$ORIGINAL_JAVA_HOME"
112120
ra_log "Instrumentation tests completed successfully"
121+
122+
if [ -z "$PACKAGE_NAME" ]; then
123+
ra_log "Application package name not available; skipping screenshot capture" >&2
124+
exit 1
125+
fi
126+
127+
ra_log "Launching $PACKAGE_NAME before capturing screenshot"
128+
adb_target shell monkey -p "$PACKAGE_NAME" -c android.intent.category.LAUNCHER 1 >/dev/null 2>&1 || {
129+
ra_log "Failed to launch $PACKAGE_NAME via monkey" >&2
130+
exit 1
131+
}
132+
133+
SCREENSHOT_DEVICE_PATH="/sdcard/Download/cn1-instrumentation-screenshot.png"
134+
SCREENSHOT_DIR="$REPO_ROOT/out/android-emulator"
135+
SCREENSHOT_PATH="$SCREENSHOT_DIR/hello-codenameone.png"
136+
mkdir -p "$SCREENSHOT_DIR"
137+
rm -f "$SCREENSHOT_PATH"
138+
139+
ra_log "Capturing emulator screenshot to $SCREENSHOT_DEVICE_PATH"
140+
adb_target shell rm "$SCREENSHOT_DEVICE_PATH" >/dev/null 2>&1 || true
141+
adb_target shell screencap -p "$SCREENSHOT_DEVICE_PATH" >/dev/null || {
142+
ra_log "Failed to capture screenshot on device" >&2
143+
exit 1
144+
}
145+
146+
ra_log "Pulling screenshot to $SCREENSHOT_PATH"
147+
adb_target pull "$SCREENSHOT_DEVICE_PATH" "$SCREENSHOT_PATH" >/dev/null || {
148+
ra_log "Failed to pull screenshot from device" >&2
149+
exit 1
150+
}
151+
adb_target shell rm "$SCREENSHOT_DEVICE_PATH" >/dev/null 2>&1 || true
152+
ra_log "Screenshot available at $SCREENSHOT_PATH"

0 commit comments

Comments
 (0)