Skip to content

Commit 3be7d6e

Browse files
committed
Trying another direction for screenshot grabbing
1 parent e624c99 commit 3be7d6e

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

.github/workflows/scripts-android.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ jobs:
3737
target: google_apis
3838
script: |
3939
./scripts/run-android-instrumentation-tests.sh "${{ steps.build-android-app.outputs.gradle_project_dir }}"
40+
- name: Upload emulator screenshot
41+
if: always() # still collect it if tests fail
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: emulator-screenshot
45+
path: artifacts/*.png
46+
if-no-files-found: warn
47+
retention-days: 14
48+
compression-level: 6

scripts/build-android-app.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,51 @@ package $PACKAGE_NAME;
325325
import android.content.Context;
326326
import android.test.InstrumentationTestCase;
327327
328+
import android.app.Instrumentation;
329+
import android.os.ParcelFileDescriptor;
330+
import android.util.Base64;
331+
332+
import java.io.FileInputStream;
333+
import java.io.IOException;
334+
import java.io.InputStream;
335+
328336
public class HelloCodenameOneInstrumentedTest extends InstrumentationTestCase {
329337
330338
public void testUseAppContext() {
331339
Context appContext = getInstrumentation().getTargetContext();
332340
assertEquals("$PACKAGE_NAME", appContext.getPackageName());
333341
}
342+
343+
private static final int CHUNK = 200_000;
344+
345+
public static void printPngToStdout(Instrumentation inst) {
346+
try {
347+
ParcelFileDescriptor pfd = inst.getUiAutomation().executeShellCommand("screencap -p");
348+
byte[] png;
349+
try (InputStream in = new FileInputStream(pfd.getFileDescriptor())) {
350+
png = readAll(in);
351+
}
352+
String b64 = Base64.encodeToString(png, Base64.NO_WRAP);
353+
System.out.println("<<CN1_SCREENSHOT_BEGIN>>");
354+
for (int i = 0; i < b64.length(); i += CHUNK) {
355+
int end = Math.min(i + CHUNK, b64.length());
356+
System.out.println(b64.substring(i, end));
357+
}
358+
System.out.println("<<CN1_SCREENSHOT_END>>");
359+
System.out.flush();
360+
} catch (IOException err) {
361+
err.printStackTrace();
362+
throw new RuntimeException(err);
363+
}
364+
}
365+
366+
private static byte[] readAll(InputStream in) throws IOException {
367+
byte[] buf = new byte[64 * 1024];
368+
int n;
369+
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
370+
while ((n = in.read(buf)) != -1) out.write(buf, 0, n);
371+
return out.toByteArray();
372+
}
334373
}
335374
EOF
336375
ba_log "Created instrumentation test at $TEST_CLASS"

scripts/run-android-instrumentation-tests.sh

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ if [ $# -lt 1 ]; then
99
exit 1
1010
fi
1111

12+
# near the top
13+
ARTIFACTS_DIR="${ARTIFACTS_DIR:-${GITHUB_WORKSPACE:-$REPO_ROOT}/artifacts}"
14+
mkdir -p "$ARTIFACTS_DIR"
15+
TEST_LOG="$ARTIFACTS_DIR/connectedAndroidTest.log"
16+
1217
GRADLE_PROJECT_DIR="$1"
1318

1419
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -102,11 +107,58 @@ if [ ! -x "$GRADLE_PROJECT_DIR/gradlew" ]; then
102107
chmod +x "$GRADLE_PROJECT_DIR/gradlew"
103108
fi
104109

105-
ra_log "Running instrumentation tests from $GRADLE_PROJECT_DIR"
106-
ORIGINAL_JAVA_HOME="${JAVA_HOME:-}"; export JAVA_HOME="$JAVA17_HOME"
110+
set -o pipefail
111+
112+
ra_log "Running instrumentation tests (stdout -> $TEST_LOG; stderr -> terminal)"
113+
status=0
107114
(
108115
cd "$GRADLE_PROJECT_DIR"
109-
./gradlew --no-daemon connectedDebugAndroidTest
110-
)
111-
export JAVA_HOME="$ORIGINAL_JAVA_HOME"
116+
# stdout goes to tee+file, stderr remains on the terminal
117+
./gradlew --no-daemon --console=plain connectedDebugAndroidTest | tee "$TEST_LOG"
118+
) || status=$?
119+
120+
# Show the log now (helpful for review even on success)
121+
echo
122+
ra_log "==== Begin connectedAndroidTest.log (tail -n 200) ===="
123+
tail -n 200 "$TEST_LOG" || true
124+
ra_log "==== End connectedAndroidTest.log ===="
125+
echo
126+
127+
# Where Gradle puts connected test XML reports
128+
RESULTS_GLOB="$GRADLE_PROJECT_DIR/app/build/outputs/androidTest-results/connected"
129+
130+
# Pick the newest XML (handles single or multiple files)
131+
NEWEST_XML="$(find "$RESULTS_GLOB" -type f -name 'TEST-*.xml' -printf '%T@ %p\n' 2>/dev/null \
132+
| sort -nr | awk 'NR==1{print $2}')"
133+
134+
ARTIFACTS_DIR="${ARTIFACTS_DIR:-${GITHUB_WORKSPACE:-$REPO_ROOT}/artifacts}"
135+
mkdir -p "$ARTIFACTS_DIR"
136+
SCREENSHOT_PATH="$ARTIFACTS_DIR/emulator-screenshot.png"
137+
138+
if [ -n "$NEWEST_XML" ] && [ -f "$NEWEST_XML" ]; then
139+
ra_log "Extracting screenshot from $NEWEST_XML -> $SCREENSHOT_PATH"
140+
141+
# Pull everything between your begin/end markers from <system-out> (handles CDATA and line breaks)
142+
if awk '
143+
/<<CN1_SCREENSHOT_BEGIN>>/ {on=1; next}
144+
/<<CN1_SCREENSHOT_END>>/ {on=0}
145+
on
146+
' "$NEWEST_XML" \
147+
| tr -d "\r\n" \
148+
| base64 -d > "$SCREENSHOT_PATH" 2>/dev/null; then
149+
150+
if [ -s "$SCREENSHOT_PATH" ]; then
151+
ra_log "Screenshot saved: $(ls -lh "$SCREENSHOT_PATH" | awk "{print \$5, \$9}")"
152+
else
153+
ra_log "Markers found but produced empty file – check that your test emitted a non-empty PNG payload."
154+
fi
155+
else
156+
ra_log "Could not decode base64 from markers in $NEWEST_XML"
157+
fi
158+
else
159+
ra_log "No TEST-*.xml found under $RESULTS_GLOB (did the test run?)"
160+
fi
161+
162+
ra_log "Latest connected test report: ${NEWEST_XML:-<none>}"
163+
112164
ra_log "Instrumentation tests completed successfully"

0 commit comments

Comments
 (0)