Skip to content

Commit d087a62

Browse files
authored
fix: improve Maestro Android reliability and snapshot speed (#612)
* fix: improve maestro tab target swipes * fix: stop replay suites on pending timeout cleanup * fix: tighten maestro compat support plumbing * fix: resolve ios simulator url open targets * fix: speed up Android Maestro snapshots * fix: resolve android snapshot ci regressions * perf: reduce android snapshot helper overhead * docs: record persistent helper session architecture * chore: address snapshot session review * perf: add persistent Android snapshot helper session * fix: stabilize Android replay interactions * fix: stabilize Maestro visibility resolution * refactor: simplify Android keyboard parsing
1 parent 9a026bf commit d087a62

57 files changed

Lines changed: 3278 additions & 447 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎android-snapshot-helper/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.callstack.agentdevice.snapshothelper">
44
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="36" />
5+
<uses-permission android:name="android.permission.INTERNET" />
56

67
<application
78
android:debuggable="false"

‎android-snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/SnapshotInstrumentation.java‎

Lines changed: 232 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
import android.os.Bundle;
88
import android.util.Base64;
99
import android.view.accessibility.AccessibilityNodeInfo;
10+
import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
1011
import android.view.accessibility.AccessibilityWindowInfo;
12+
import java.io.BufferedReader;
1113
import java.io.File;
1214
import java.io.FileOutputStream;
1315
import java.io.IOException;
16+
import java.io.InputStreamReader;
17+
import java.io.OutputStream;
18+
import java.net.InetAddress;
19+
import java.net.ServerSocket;
20+
import java.net.Socket;
1421
import java.lang.reflect.Field;
1522
import java.nio.charset.StandardCharsets;
1623
import java.util.List;
@@ -22,8 +29,10 @@ public final class SnapshotInstrumentation extends Instrumentation {
2229
private static final String OUTPUT_FORMAT = "uiautomator-xml";
2330
private static final String HELPER_API_VERSION = "1";
2431
private static final int CHUNK_SIZE = 2 * 1024;
25-
private static final long DEFAULT_WAIT_FOR_IDLE_TIMEOUT_MS = 500;
26-
private static final long DEFAULT_WAIT_FOR_IDLE_QUIET_MS = 100;
32+
// Keep the default quiet window short: RN/animation-heavy apps often never become fully idle,
33+
// and callers can still override this for alert-style flows that need a longer settle period.
34+
private static final long DEFAULT_WAIT_FOR_IDLE_TIMEOUT_MS = 25;
35+
private static final long DEFAULT_WAIT_FOR_IDLE_QUIET_MS = 25;
2736
private static final long DEFAULT_TIMEOUT_MS = 8_000;
2837
private static final int DEFAULT_MAX_DEPTH = 128;
2938
private static final int DEFAULT_MAX_NODES = 5_000;
@@ -47,7 +56,47 @@ public void onStart() {
4756
int maxDepth = readIntArgument(arguments, "maxDepth", DEFAULT_MAX_DEPTH);
4857
int maxNodes = readIntArgument(arguments, "maxNodes", DEFAULT_MAX_NODES);
4958
String outputPath = readStringArgument(arguments, "outputPath");
59+
boolean emitChunks = readBooleanArgument(arguments, "emitChunks", true);
60+
int sessionPort = readIntArgument(arguments, "sessionPort", 0);
5061
Bundle result = new Bundle();
62+
putBaseMetadata(result, waitForIdleTimeoutMs, waitForIdleQuietMs, timeoutMs, maxDepth, maxNodes);
63+
64+
try {
65+
if (sessionPort > 0) {
66+
runSnapshotSession(
67+
sessionPort, waitForIdleQuietMs, waitForIdleTimeoutMs, timeoutMs, maxDepth, maxNodes);
68+
result.putString("ok", "true");
69+
result.putString("sessionEnded", "true");
70+
finishSafely(0, result);
71+
return;
72+
}
73+
long startedAtMs = System.currentTimeMillis();
74+
CaptureResult capture =
75+
captureXml(waitForIdleQuietMs, waitForIdleTimeoutMs, timeoutMs, maxDepth, maxNodes);
76+
writeOutputFile(outputPath, capture.xml);
77+
if (emitChunks) {
78+
emitChunks(capture.xml);
79+
}
80+
result.putString("ok", "true");
81+
putCaptureMetadata(result, capture, System.currentTimeMillis() - startedAtMs);
82+
finishSafely(0, result);
83+
} catch (Throwable error) {
84+
result.putString("ok", "false");
85+
result.putString("errorType", error.getClass().getName());
86+
result.putString(
87+
"message",
88+
error.getMessage() == null ? error.getClass().getName() : error.getMessage());
89+
finishSafely(1, result);
90+
}
91+
}
92+
93+
private static void putBaseMetadata(
94+
Bundle result,
95+
long waitForIdleTimeoutMs,
96+
long waitForIdleQuietMs,
97+
long timeoutMs,
98+
int maxDepth,
99+
int maxNodes) {
51100
result.putString("agentDeviceProtocol", PROTOCOL);
52101
result.putString("helperApiVersion", HELPER_API_VERSION);
53102
result.putString("outputFormat", OUTPUT_FORMAT);
@@ -56,31 +105,143 @@ public void onStart() {
56105
result.putString("timeoutMs", Long.toString(timeoutMs));
57106
result.putString("maxDepth", Integer.toString(maxDepth));
58107
result.putString("maxNodes", Integer.toString(maxNodes));
108+
}
109+
110+
private static void putCaptureMetadata(Bundle result, CaptureResult capture, long elapsedMs) {
111+
result.putString("rootPresent", Boolean.toString(capture.rootPresent));
112+
result.putString("captureMode", capture.captureMode);
113+
result.putString("windowCount", Integer.toString(capture.windowCount));
114+
result.putString("nodeCount", Integer.toString(capture.nodeCount));
115+
result.putString("truncated", Boolean.toString(capture.truncated));
116+
result.putString("elapsedMs", Long.toString(elapsedMs));
117+
}
118+
119+
private void runSnapshotSession(
120+
int sessionPort,
121+
long waitForIdleQuietMs,
122+
long waitForIdleTimeoutMs,
123+
long timeoutMs,
124+
int maxDepth,
125+
int maxNodes)
126+
throws IOException {
127+
try (ServerSocket server =
128+
new ServerSocket(sessionPort, 1, InetAddress.getByName("127.0.0.1"))) {
129+
Bundle ready = new Bundle();
130+
putBaseMetadata(
131+
ready, waitForIdleTimeoutMs, waitForIdleQuietMs, timeoutMs, maxDepth, maxNodes);
132+
ready.putString("sessionReady", "true");
133+
ready.putString("sessionPort", Integer.toString(sessionPort));
134+
sendStatus(2, ready);
135+
136+
while (!Thread.currentThread().isInterrupted()) {
137+
try (Socket socket = server.accept()) {
138+
String command =
139+
new BufferedReader(
140+
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))
141+
.readLine();
142+
if (command == null) {
143+
writeSessionError(socket.getOutputStream(), "", "java.io.EOFException", "empty command");
144+
continue;
145+
}
146+
String[] parts = command.trim().split("\\s+", 2);
147+
String action = parts.length > 0 ? parts[0] : "";
148+
String requestId = parts.length > 1 ? parts[1] : "";
149+
if ("quit".equals(action)) {
150+
writeSessionOk(socket.getOutputStream(), requestId);
151+
return;
152+
}
153+
if (!"snapshot".equals(action)) {
154+
writeSessionError(
155+
socket.getOutputStream(),
156+
requestId,
157+
"java.lang.IllegalArgumentException",
158+
"unknown session command");
159+
continue;
160+
}
161+
writeSessionSnapshot(
162+
socket.getOutputStream(),
163+
requestId,
164+
waitForIdleQuietMs,
165+
waitForIdleTimeoutMs,
166+
timeoutMs,
167+
maxDepth,
168+
maxNodes);
169+
}
170+
}
171+
}
172+
}
59173

174+
private void writeSessionSnapshot(
175+
OutputStream output,
176+
String requestId,
177+
long waitForIdleQuietMs,
178+
long waitForIdleTimeoutMs,
179+
long timeoutMs,
180+
int maxDepth,
181+
int maxNodes)
182+
throws IOException {
183+
Bundle result = new Bundle();
184+
putBaseMetadata(result, waitForIdleTimeoutMs, waitForIdleQuietMs, timeoutMs, maxDepth, maxNodes);
185+
result.putString("requestId", requestId);
60186
try {
61187
long startedAtMs = System.currentTimeMillis();
62188
CaptureResult capture =
63189
captureXml(waitForIdleQuietMs, waitForIdleTimeoutMs, timeoutMs, maxDepth, maxNodes);
64-
writeOutputFile(outputPath, capture.xml);
65-
emitChunks(capture.xml);
66190
result.putString("ok", "true");
67-
result.putString("rootPresent", Boolean.toString(capture.rootPresent));
68-
result.putString("captureMode", capture.captureMode);
69-
result.putString("windowCount", Integer.toString(capture.windowCount));
70-
result.putString("nodeCount", Integer.toString(capture.nodeCount));
71-
result.putString("truncated", Boolean.toString(capture.truncated));
72-
result.putString("elapsedMs", Long.toString(System.currentTimeMillis() - startedAtMs));
73-
finishSafely(0, result);
191+
putCaptureMetadata(result, capture, System.currentTimeMillis() - startedAtMs);
192+
result.putString("byteLength", Integer.toString(capture.xml.getBytes(StandardCharsets.UTF_8).length));
193+
writeSessionResponse(output, result, capture.xml);
74194
} catch (Throwable error) {
75-
result.putString("ok", "false");
76-
result.putString("errorType", error.getClass().getName());
77-
result.putString(
78-
"message",
195+
writeSessionError(
196+
output,
197+
requestId,
198+
error.getClass().getName(),
79199
error.getMessage() == null ? error.getClass().getName() : error.getMessage());
80-
finishSafely(1, result);
81200
}
82201
}
83202

203+
private static void writeSessionOk(OutputStream output, String requestId) throws IOException {
204+
Bundle result = new Bundle();
205+
result.putString("agentDeviceProtocol", PROTOCOL);
206+
result.putString("helperApiVersion", HELPER_API_VERSION);
207+
result.putString("outputFormat", OUTPUT_FORMAT);
208+
result.putString("requestId", requestId);
209+
result.putString("ok", "true");
210+
writeSessionResponse(output, result, "");
211+
}
212+
213+
private static void writeSessionError(
214+
OutputStream output, String requestId, String errorType, String message) throws IOException {
215+
Bundle result = new Bundle();
216+
result.putString("agentDeviceProtocol", PROTOCOL);
217+
result.putString("helperApiVersion", HELPER_API_VERSION);
218+
result.putString("outputFormat", OUTPUT_FORMAT);
219+
result.putString("requestId", requestId);
220+
result.putString("ok", "false");
221+
result.putString("errorType", errorType);
222+
result.putString("message", message);
223+
writeSessionResponse(output, result, "");
224+
}
225+
226+
private static void writeSessionResponse(OutputStream output, Bundle result, String body)
227+
throws IOException {
228+
StringBuilder headers = new StringBuilder();
229+
for (String key : result.keySet()) {
230+
Object value = result.get(key);
231+
if (value != null) {
232+
headers.append(key).append('=').append(sanitizeHeaderValue(value.toString())).append('\n');
233+
}
234+
}
235+
headers.append('\n');
236+
output.write(headers.toString().getBytes(StandardCharsets.UTF_8));
237+
output.write(body.getBytes(StandardCharsets.UTF_8));
238+
output.flush();
239+
}
240+
241+
private static String sanitizeHeaderValue(String value) {
242+
return value.replace('\r', ' ').replace('\n', ' ');
243+
}
244+
84245
private static String readStringArgument(Bundle arguments, String key) {
85246
if (arguments == null || !arguments.containsKey(key)) {
86247
return null;
@@ -330,22 +491,34 @@ private static void appendNode(
330491
Rect bounds = new Rect();
331492
node.getBoundsInScreen(bounds);
332493
xml.append("<node");
494+
// Emit only fields consumed by the host parser. Extra boolean attrs made every node larger
495+
// without affecting current snapshot semantics; add fields back here when TS starts reading
496+
// them.
333497
appendAttribute(xml, "index", Integer.toString(nodeIndex));
334-
appendAttribute(xml, "text", node.getText());
335-
appendAttribute(xml, "resource-id", node.getViewIdResourceName());
498+
appendNonEmptyAttribute(xml, "text", node.getText());
499+
appendNonEmptyAttribute(xml, "resource-id", node.getViewIdResourceName());
336500
appendAttribute(xml, "class", node.getClassName());
337-
appendAttribute(xml, "package", node.getPackageName());
338-
appendAttribute(xml, "content-desc", node.getContentDescription());
339-
appendAttribute(xml, "checkable", Boolean.toString(node.isCheckable()));
340-
appendAttribute(xml, "checked", Boolean.toString(node.isChecked()));
341-
appendAttribute(xml, "clickable", Boolean.toString(node.isClickable()));
501+
appendNonEmptyAttribute(xml, "package", node.getPackageName());
502+
appendNonEmptyAttribute(xml, "content-desc", node.getContentDescription());
503+
appendTrueAttribute(xml, "clickable", node.isClickable());
342504
appendAttribute(xml, "enabled", Boolean.toString(node.isEnabled()));
343-
appendAttribute(xml, "focusable", Boolean.toString(node.isFocusable()));
344-
appendAttribute(xml, "focused", Boolean.toString(node.isFocused()));
345-
appendAttribute(xml, "scrollable", Boolean.toString(node.isScrollable()));
346-
appendAttribute(xml, "long-clickable", Boolean.toString(node.isLongClickable()));
347-
appendAttribute(xml, "password", Boolean.toString(node.isPassword()));
348-
appendAttribute(xml, "selected", Boolean.toString(node.isSelected()));
505+
appendTrueAttribute(xml, "focusable", node.isFocusable());
506+
appendTrueAttribute(xml, "focused", node.isFocused());
507+
boolean scrollable = node.isScrollable();
508+
if (scrollable) {
509+
appendAttribute(xml, "scrollable", "true");
510+
appendAttribute(
511+
xml,
512+
"can-scroll-forward",
513+
Boolean.toString(
514+
hasAccessibilityAction(node, AccessibilityAction.ACTION_SCROLL_FORWARD)));
515+
appendAttribute(
516+
xml,
517+
"can-scroll-backward",
518+
Boolean.toString(
519+
hasAccessibilityAction(node, AccessibilityAction.ACTION_SCROLL_BACKWARD)));
520+
}
521+
appendTrueAttribute(xml, "password", node.isPassword());
349522
appendAttribute(
350523
xml,
351524
"bounds",
@@ -385,6 +558,19 @@ private static void appendNode(
385558
xml.append("</node>");
386559
}
387560

561+
private static void appendNonEmptyAttribute(StringBuilder xml, String name, CharSequence value) {
562+
if (value == null || value.length() == 0) {
563+
return;
564+
}
565+
appendAttribute(xml, name, value);
566+
}
567+
568+
private static void appendTrueAttribute(StringBuilder xml, String name, boolean value) {
569+
if (value) {
570+
appendAttribute(xml, name, "true");
571+
}
572+
}
573+
388574
private static void appendAttribute(StringBuilder xml, String name, CharSequence value) {
389575
String stringValue = value == null ? "" : value.toString();
390576
xml.append(' ');
@@ -394,6 +580,12 @@ private static void appendAttribute(StringBuilder xml, String name, CharSequence
394580
xml.append('"');
395581
}
396582

583+
private static boolean hasAccessibilityAction(
584+
AccessibilityNodeInfo node, AccessibilityAction action) {
585+
List<AccessibilityAction> actions = node.getActionList();
586+
return actions != null && actions.contains(action);
587+
}
588+
397589
private static void appendEscaped(StringBuilder xml, String value) {
398590
for (int index = 0; index < value.length(); index += 1) {
399591
char character = value.charAt(index);
@@ -459,6 +651,17 @@ private static int readIntArgument(Bundle arguments, String name, int fallback)
459651
}
460652
}
461653

654+
private static boolean readBooleanArgument(Bundle arguments, String name, boolean fallback) {
655+
if (arguments == null) {
656+
return fallback;
657+
}
658+
String raw = arguments.getString(name);
659+
if (raw == null || raw.trim().isEmpty()) {
660+
return fallback;
661+
}
662+
return Boolean.parseBoolean(raw.trim());
663+
}
664+
462665
private static final class CaptureStats {
463666
int nodeCount;
464667
boolean truncated;

0 commit comments

Comments
 (0)