Skip to content

Commit 197d3b4

Browse files
committed
Trying another direction for screenshot grabbing
1 parent e624c99 commit 197d3b4

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
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: 23 additions & 1 deletion
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)"
@@ -106,7 +111,24 @@ ra_log "Running instrumentation tests from $GRADLE_PROJECT_DIR"
106111
ORIGINAL_JAVA_HOME="${JAVA_HOME:-}"; export JAVA_HOME="$JAVA17_HOME"
107112
(
108113
cd "$GRADLE_PROJECT_DIR"
109-
./gradlew --no-daemon connectedDebugAndroidTest
114+
./gradlew --no-daemon connectedDebugAndroidTest 2>&1 | tee "$TEST_LOG"
110115
)
116+
SCREENSHOT_PATH="$ARTIFACTS_DIR/emulator-screenshot.png"
117+
ra_log "Extracting screenshot from test output into $SCREENSHOT_PATH"
118+
119+
if awk '
120+
/<<CN1_SCREENSHOT_BEGIN>>/ {printing=1; next}
121+
/<<CN1_SCREENSHOT_END>>/ {printing=0}
122+
printing { print }
123+
' "$TEST_LOG" | tr -d "\r" | base64 -d > "$SCREENSHOT_PATH"; then
124+
if [ -s "$SCREENSHOT_PATH" ]; then
125+
ra_log "Screenshot saved: $(ls -lh "$SCREENSHOT_PATH" | awk "{print \$5, \$9}")"
126+
else
127+
ra_log "Screenshot extraction produced an empty file" >&2
128+
fi
129+
else
130+
ra_log "Failed to extract/decode screenshot from test output" >&2
131+
fi
132+
111133
export JAVA_HOME="$ORIGINAL_JAVA_HOME"
112134
ra_log "Instrumentation tests completed successfully"

0 commit comments

Comments
 (0)