Skip to content

Mobile MCP: let AI agents drive your iOS and Android apps. MCP server for simulators, emulators and real devices. Works with Claude Code, Codex, Cursor and any MCP client.

fix: quote identifier in text element format so whitespace stays in one key=value pair

gmegidish49bc733ec925
diff --git a/src/format-elements.ts b/src/format-elements.ts
index 51eb9d0..739fe5e 100644
--- a/src/format-elements.ts
+++ b/src/format-elements.ts
@@ -31,7 +31,7 @@ const formatElementAsText = (element: ScreenElement): string => {
}
if (element.identifier) {
- parts.push(`id=${element.identifier}`);
+ parts.push(`id=${quote(element.identifier)}`);
}
parts.push(`at=${element.rect.x},${element.rect.y}`);
diff --git a/test/format-elements.test.ts b/test/format-elements.test.ts
index d95796f..16c7ad0 100644
--- a/test/format-elements.test.ts
+++ b/test/format-elements.test.ts
@@ -24,7 +24,7 @@ const focusedTextField: ScreenElement = {
test("text format emits one self-describing line per element with coordinates", () => {
const lines = formatElements([incrementButton, focusedTextField], "text").split("\n");
expect(lines[0]).toContain("@ref Type");
- expect(lines[1]).toBe("@e42 Button text=\"+\" id=com.mobilenext.playground:id/stepper_increment at=1070,1340 size=120x120");
+ expect(lines[1]).toBe("@e42 Button text=\"+\" id=\"com.mobilenext.playground:id/stepper_increment\" at=1070,1340 size=120x120");
expect(lines[2]).toBe("@e32 EditText text=\"Hello World\" label=\"text_field\" at=48,447 size=1184x149 focused disabled");
});
@@ -34,3 +34,16 @@ test("json format keeps the legacy shape", () => {
expect(parsed[0].coordinates).toEqual({ x: 1070, y: 1340, width: 120, height: 120 });
expect(parsed[0].enabled).toBeUndefined();
});
+
+const iosCellWithSpacesInIdentifier: ScreenElement = {
+ ref: "@e7",
+ type: "XCUIElementTypeCell",
+ identifier: "row 3\nsecond line",
+ rect: { x: 0, y: 200, width: 390, height: 44 },
+};
+
+test("text format quotes identifiers so whitespace stays inside one key=value pair", () => {
+ const lines = formatElements([iosCellWithSpacesInIdentifier], "text").split("\n");
+ expect(lines.length).toBe(2);
+ expect(lines[1]).toBe("@e7 XCUIElementTypeCell id=\"row 3\\nsecond line\" at=0,200 size=390x44");
+});