devlens-mcp 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.json +12 -0
- package/.claude/settings.local.json +17 -0
- package/INSTALLATION_GUIDE.md +354 -0
- package/QUICK_START.md +153 -0
- package/README.md +354 -0
- package/bin/cli.ts +22 -0
- package/bin/register.ts +96 -0
- package/dist/bin/cli.d.ts +3 -0
- package/dist/bin/cli.d.ts.map +1 -0
- package/dist/bin/cli.js +20 -0
- package/dist/bin/cli.js.map +1 -0
- package/dist/bin/register.d.ts +10 -0
- package/dist/bin/register.d.ts.map +1 -0
- package/dist/bin/register.js +92 -0
- package/dist/bin/register.js.map +1 -0
- package/dist/src/config/devlens-config.d.ts +92 -0
- package/dist/src/config/devlens-config.d.ts.map +1 -0
- package/dist/src/config/devlens-config.js +70 -0
- package/dist/src/config/devlens-config.js.map +1 -0
- package/dist/src/index.d.ts +35 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +8 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/metro/cdp-client.d.ts +48 -0
- package/dist/src/metro/cdp-client.d.ts.map +1 -0
- package/dist/src/metro/cdp-client.js +127 -0
- package/dist/src/metro/cdp-client.js.map +1 -0
- package/dist/src/metro/log-collector.d.ts +30 -0
- package/dist/src/metro/log-collector.d.ts.map +1 -0
- package/dist/src/metro/log-collector.js +114 -0
- package/dist/src/metro/log-collector.js.map +1 -0
- package/dist/src/metro/metro-bridge.d.ts +56 -0
- package/dist/src/metro/metro-bridge.d.ts.map +1 -0
- package/dist/src/metro/metro-bridge.js +255 -0
- package/dist/src/metro/metro-bridge.js.map +1 -0
- package/dist/src/metro/network-inspector.d.ts +34 -0
- package/dist/src/metro/network-inspector.d.ts.map +1 -0
- package/dist/src/metro/network-inspector.js +100 -0
- package/dist/src/metro/network-inspector.js.map +1 -0
- package/dist/src/platform/android/adb.d.ts +50 -0
- package/dist/src/platform/android/adb.d.ts.map +1 -0
- package/dist/src/platform/android/adb.js +137 -0
- package/dist/src/platform/android/adb.js.map +1 -0
- package/dist/src/platform/android/android-device.d.ts +21 -0
- package/dist/src/platform/android/android-device.d.ts.map +1 -0
- package/dist/src/platform/android/android-device.js +94 -0
- package/dist/src/platform/android/android-device.js.map +1 -0
- package/dist/src/platform/android/ui-automator.d.ts +17 -0
- package/dist/src/platform/android/ui-automator.d.ts.map +1 -0
- package/dist/src/platform/android/ui-automator.js +126 -0
- package/dist/src/platform/android/ui-automator.js.map +1 -0
- package/dist/src/platform/device-manager.d.ts +28 -0
- package/dist/src/platform/device-manager.d.ts.map +1 -0
- package/dist/src/platform/device-manager.js +185 -0
- package/dist/src/platform/device-manager.js.map +1 -0
- package/dist/src/platform/device.d.ts +86 -0
- package/dist/src/platform/device.d.ts.map +1 -0
- package/dist/src/platform/device.js +7 -0
- package/dist/src/platform/device.js.map +1 -0
- package/dist/src/platform/ios/accessibility.d.ts +17 -0
- package/dist/src/platform/ios/accessibility.d.ts.map +1 -0
- package/dist/src/platform/ios/accessibility.js +159 -0
- package/dist/src/platform/ios/accessibility.js.map +1 -0
- package/dist/src/platform/ios/ios-device.d.ts +22 -0
- package/dist/src/platform/ios/ios-device.d.ts.map +1 -0
- package/dist/src/platform/ios/ios-device.js +97 -0
- package/dist/src/platform/ios/ios-device.js.map +1 -0
- package/dist/src/platform/ios/simctl.d.ts +54 -0
- package/dist/src/platform/ios/simctl.d.ts.map +1 -0
- package/dist/src/platform/ios/simctl.js +192 -0
- package/dist/src/platform/ios/simctl.js.map +1 -0
- package/dist/src/server.d.ts +3 -0
- package/dist/src/server.d.ts.map +1 -0
- package/dist/src/server.js +176 -0
- package/dist/src/server.js.map +1 -0
- package/dist/src/snapshot/formatter.d.ts +18 -0
- package/dist/src/snapshot/formatter.d.ts.map +1 -0
- package/dist/src/snapshot/formatter.js +86 -0
- package/dist/src/snapshot/formatter.js.map +1 -0
- package/dist/src/snapshot/ref-registry.d.ts +67 -0
- package/dist/src/snapshot/ref-registry.d.ts.map +1 -0
- package/dist/src/snapshot/ref-registry.js +169 -0
- package/dist/src/snapshot/ref-registry.js.map +1 -0
- package/dist/src/snapshot/snapshot-differ.d.ts +57 -0
- package/dist/src/snapshot/snapshot-differ.d.ts.map +1 -0
- package/dist/src/snapshot/snapshot-differ.js +153 -0
- package/dist/src/snapshot/snapshot-differ.js.map +1 -0
- package/dist/src/tools/app-tools.d.ts +71 -0
- package/dist/src/tools/app-tools.d.ts.map +1 -0
- package/dist/src/tools/app-tools.js +97 -0
- package/dist/src/tools/app-tools.js.map +1 -0
- package/dist/src/tools/device-tools.d.ts +53 -0
- package/dist/src/tools/device-tools.d.ts.map +1 -0
- package/dist/src/tools/device-tools.js +86 -0
- package/dist/src/tools/device-tools.js.map +1 -0
- package/dist/src/tools/ds-tools.d.ts +65 -0
- package/dist/src/tools/ds-tools.d.ts.map +1 -0
- package/dist/src/tools/ds-tools.js +314 -0
- package/dist/src/tools/ds-tools.js.map +1 -0
- package/dist/src/tools/interaction-tools.d.ts +248 -0
- package/dist/src/tools/interaction-tools.d.ts.map +1 -0
- package/dist/src/tools/interaction-tools.js +391 -0
- package/dist/src/tools/interaction-tools.js.map +1 -0
- package/dist/src/tools/metro-tools.d.ts +115 -0
- package/dist/src/tools/metro-tools.d.ts.map +1 -0
- package/dist/src/tools/metro-tools.js +270 -0
- package/dist/src/tools/metro-tools.js.map +1 -0
- package/dist/src/tools/navigation-tools.d.ts +36 -0
- package/dist/src/tools/navigation-tools.d.ts.map +1 -0
- package/dist/src/tools/navigation-tools.js +60 -0
- package/dist/src/tools/navigation-tools.js.map +1 -0
- package/dist/src/tools/screenshot-tools.d.ts +298 -0
- package/dist/src/tools/screenshot-tools.d.ts.map +1 -0
- package/dist/src/tools/screenshot-tools.js +565 -0
- package/dist/src/tools/screenshot-tools.js.map +1 -0
- package/dist/src/tools/snapshot-tools.d.ts +161 -0
- package/dist/src/tools/snapshot-tools.d.ts.map +1 -0
- package/dist/src/tools/snapshot-tools.js +479 -0
- package/dist/src/tools/snapshot-tools.js.map +1 -0
- package/dist/src/utils/image-preprocess.d.ts +49 -0
- package/dist/src/utils/image-preprocess.d.ts.map +1 -0
- package/dist/src/utils/image-preprocess.js +322 -0
- package/dist/src/utils/image-preprocess.js.map +1 -0
- package/dist/src/utils/retry.d.ts +21 -0
- package/dist/src/utils/retry.d.ts.map +1 -0
- package/dist/src/utils/retry.js +33 -0
- package/dist/src/utils/retry.js.map +1 -0
- package/dist/src/visual/comparator.d.ts +51 -0
- package/dist/src/visual/comparator.d.ts.map +1 -0
- package/dist/src/visual/comparator.js +119 -0
- package/dist/src/visual/comparator.js.map +1 -0
- package/dist/src/visual/layout-analyzer.d.ts +64 -0
- package/dist/src/visual/layout-analyzer.d.ts.map +1 -0
- package/dist/src/visual/layout-analyzer.js +198 -0
- package/dist/src/visual/layout-analyzer.js.map +1 -0
- package/dist/src/visual/screenshot.d.ts +17 -0
- package/dist/src/visual/screenshot.d.ts.map +1 -0
- package/dist/src/visual/screenshot.js +39 -0
- package/dist/src/visual/screenshot.js.map +1 -0
- package/docs/figma-workflow.md +289 -0
- package/docs/setup-guide.md +360 -0
- package/docs/tool-reference.md +622 -0
- package/package.json +57 -0
- package/src/config/devlens-config.ts +76 -0
- package/src/index.ts +5 -0
- package/src/metro/cdp-client.ts +160 -0
- package/src/metro/log-collector.ts +137 -0
- package/src/metro/metro-bridge.ts +307 -0
- package/src/metro/network-inspector.ts +134 -0
- package/src/platform/android/adb.ts +200 -0
- package/src/platform/android/android-device.ts +116 -0
- package/src/platform/android/ui-automator.ts +141 -0
- package/src/platform/device-manager.ts +229 -0
- package/src/platform/device.ts +110 -0
- package/src/platform/ios/accessibility.ts +189 -0
- package/src/platform/ios/ios-device.ts +116 -0
- package/src/platform/ios/simctl.ts +244 -0
- package/src/server.ts +228 -0
- package/src/snapshot/formatter.ts +102 -0
- package/src/snapshot/ref-registry.ts +230 -0
- package/src/snapshot/snapshot-differ.ts +220 -0
- package/src/tools/app-tools.ts +111 -0
- package/src/tools/device-tools.ts +96 -0
- package/src/tools/ds-tools.ts +395 -0
- package/src/tools/interaction-tools.ts +467 -0
- package/src/tools/metro-tools.ts +320 -0
- package/src/tools/navigation-tools.ts +71 -0
- package/src/tools/screenshot-tools.ts +698 -0
- package/src/tools/snapshot-tools.ts +585 -0
- package/src/utils/image-preprocess.ts +430 -0
- package/src/utils/retry.ts +51 -0
- package/src/visual/comparator.ts +191 -0
- package/src/visual/layout-analyzer.ts +283 -0
- package/src/visual/screenshot.ts +49 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { execFile } from "child_process";
|
|
2
|
+
import { promisify } from "util";
|
|
3
|
+
import type { Device, DeviceInfo } from "./device.js";
|
|
4
|
+
import { AndroidDevice } from "./android/android-device.js";
|
|
5
|
+
import { IosDevice } from "./ios/ios-device.js";
|
|
6
|
+
|
|
7
|
+
const execFileAsync = promisify(execFile);
|
|
8
|
+
|
|
9
|
+
export interface DiscoveredDevice {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
platform: "ios" | "android";
|
|
13
|
+
osVersion: string;
|
|
14
|
+
isBooted: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* DeviceManager discovers running iOS Simulators and Android Emulators,
|
|
19
|
+
* and creates Device instances for them.
|
|
20
|
+
*/
|
|
21
|
+
export class DeviceManager {
|
|
22
|
+
private activeDevice: Device | null = null;
|
|
23
|
+
private activeDeviceId: string | null = null;
|
|
24
|
+
private activePlatform: "ios" | "android" | null = null;
|
|
25
|
+
|
|
26
|
+
async discoverDevices(): Promise<DiscoveredDevice[]> {
|
|
27
|
+
const devices: DiscoveredDevice[] = [];
|
|
28
|
+
|
|
29
|
+
const [androidDevices, iosDevices] = await Promise.allSettled([
|
|
30
|
+
this.discoverAndroidDevices(),
|
|
31
|
+
this.discoverIosDevices(),
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
if (androidDevices.status === "fulfilled") {
|
|
35
|
+
devices.push(...androidDevices.value);
|
|
36
|
+
}
|
|
37
|
+
if (iosDevices.status === "fulfilled") {
|
|
38
|
+
devices.push(...iosDevices.value);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return devices;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async getDevice(deviceId?: string): Promise<Device> {
|
|
45
|
+
// If we have a cached device and it matches, verify it's still healthy
|
|
46
|
+
if (
|
|
47
|
+
this.activeDevice &&
|
|
48
|
+
this.activeDeviceId &&
|
|
49
|
+
(!deviceId || deviceId === this.activeDeviceId)
|
|
50
|
+
) {
|
|
51
|
+
const isHealthy = await this.checkDeviceHealth(
|
|
52
|
+
this.activeDeviceId,
|
|
53
|
+
this.activePlatform
|
|
54
|
+
);
|
|
55
|
+
if (isHealthy) {
|
|
56
|
+
return this.activeDevice;
|
|
57
|
+
}
|
|
58
|
+
// Device is stale — clear cache and rediscover
|
|
59
|
+
this.activeDevice = null;
|
|
60
|
+
this.activeDeviceId = null;
|
|
61
|
+
this.activePlatform = null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const devices = await this.discoverDevices();
|
|
65
|
+
const bootedDevices = devices.filter((d) => d.isBooted);
|
|
66
|
+
|
|
67
|
+
if (bootedDevices.length === 0) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
"No running simulators or emulators found. Please start a device first."
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let target: DiscoveredDevice;
|
|
74
|
+
if (deviceId) {
|
|
75
|
+
const found = bootedDevices.find((d) => d.id === deviceId);
|
|
76
|
+
if (!found) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`Device "${deviceId}" not found or not running. Available: ${bootedDevices.map((d) => d.id).join(", ")}`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
target = found;
|
|
82
|
+
} else {
|
|
83
|
+
target = bootedDevices[0];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (target.platform === "android") {
|
|
87
|
+
this.activeDevice = new AndroidDevice(target.id);
|
|
88
|
+
} else {
|
|
89
|
+
this.activeDevice = new IosDevice(target.id);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.activeDeviceId = target.id;
|
|
93
|
+
this.activePlatform = target.platform;
|
|
94
|
+
return this.activeDevice;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Quick health check to verify a cached device is still alive.
|
|
99
|
+
* Returns false if the device is disconnected or no longer booted.
|
|
100
|
+
*/
|
|
101
|
+
private async checkDeviceHealth(
|
|
102
|
+
deviceId: string,
|
|
103
|
+
platform: "ios" | "android" | null
|
|
104
|
+
): Promise<boolean> {
|
|
105
|
+
try {
|
|
106
|
+
if (platform === "android") {
|
|
107
|
+
const adbPath = this.getAdbPath();
|
|
108
|
+
const { stdout } = await execFileAsync(adbPath, ["-s", deviceId, "get-state"], {
|
|
109
|
+
timeout: 5000,
|
|
110
|
+
});
|
|
111
|
+
return stdout.trim() === "device";
|
|
112
|
+
} else if (platform === "ios") {
|
|
113
|
+
const { stdout } = await execFileAsync(
|
|
114
|
+
"xcrun",
|
|
115
|
+
["simctl", "list", "devices", "--json"],
|
|
116
|
+
{ timeout: 5000 }
|
|
117
|
+
);
|
|
118
|
+
const data = JSON.parse(stdout);
|
|
119
|
+
for (const deviceList of Object.values(
|
|
120
|
+
data.devices as Record<string, Array<{ udid: string; state: string }>>
|
|
121
|
+
)) {
|
|
122
|
+
const found = deviceList.find((d) => d.udid === deviceId);
|
|
123
|
+
if (found) return found.state === "Booted";
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
} catch {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private async discoverAndroidDevices(): Promise<DiscoveredDevice[]> {
|
|
134
|
+
try {
|
|
135
|
+
const adbPath = this.getAdbPath();
|
|
136
|
+
const { stdout } = await execFileAsync(adbPath, ["devices", "-l"]);
|
|
137
|
+
const devices: DiscoveredDevice[] = [];
|
|
138
|
+
|
|
139
|
+
for (const line of stdout.split("\n").slice(1)) {
|
|
140
|
+
const trimmed = line.trim();
|
|
141
|
+
if (!trimmed || trimmed === "") continue;
|
|
142
|
+
|
|
143
|
+
const parts = trimmed.split(/\s+/);
|
|
144
|
+
const id = parts[0];
|
|
145
|
+
const status = parts[1];
|
|
146
|
+
|
|
147
|
+
if (status !== "device") continue;
|
|
148
|
+
|
|
149
|
+
// Extract model name from properties
|
|
150
|
+
const modelMatch = trimmed.match(/model:(\S+)/);
|
|
151
|
+
const name = modelMatch ? modelMatch[1] : id;
|
|
152
|
+
|
|
153
|
+
// Get Android version
|
|
154
|
+
let osVersion = "unknown";
|
|
155
|
+
try {
|
|
156
|
+
const { stdout: versionOut } = await execFileAsync(adbPath, [
|
|
157
|
+
"-s",
|
|
158
|
+
id,
|
|
159
|
+
"shell",
|
|
160
|
+
"getprop",
|
|
161
|
+
"ro.build.version.release",
|
|
162
|
+
]);
|
|
163
|
+
osVersion = versionOut.trim();
|
|
164
|
+
} catch {
|
|
165
|
+
// Ignore version fetch errors
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
devices.push({
|
|
169
|
+
id,
|
|
170
|
+
name,
|
|
171
|
+
platform: "android",
|
|
172
|
+
osVersion,
|
|
173
|
+
isBooted: true,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return devices;
|
|
178
|
+
} catch {
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private async discoverIosDevices(): Promise<DiscoveredDevice[]> {
|
|
184
|
+
try {
|
|
185
|
+
const { stdout } = await execFileAsync("xcrun", [
|
|
186
|
+
"simctl",
|
|
187
|
+
"list",
|
|
188
|
+
"devices",
|
|
189
|
+
"--json",
|
|
190
|
+
]);
|
|
191
|
+
const data = JSON.parse(stdout);
|
|
192
|
+
const devices: DiscoveredDevice[] = [];
|
|
193
|
+
|
|
194
|
+
for (const [runtime, deviceList] of Object.entries(
|
|
195
|
+
data.devices as Record<string, Array<{ udid: string; name: string; state: string }>>
|
|
196
|
+
)) {
|
|
197
|
+
const osVersion = runtime.replace(
|
|
198
|
+
/^com\.apple\.CoreSimulator\.SimRuntime\./,
|
|
199
|
+
""
|
|
200
|
+
).replace(/-/g, ".").replace(/^iOS\./, "");
|
|
201
|
+
|
|
202
|
+
for (const device of deviceList) {
|
|
203
|
+
if (device.state === "Booted") {
|
|
204
|
+
devices.push({
|
|
205
|
+
id: device.udid,
|
|
206
|
+
name: device.name,
|
|
207
|
+
platform: "ios",
|
|
208
|
+
osVersion,
|
|
209
|
+
isBooted: true,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return devices;
|
|
216
|
+
} catch {
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private getAdbPath(): string {
|
|
222
|
+
const androidHome =
|
|
223
|
+
process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT;
|
|
224
|
+
if (androidHome) {
|
|
225
|
+
return `${androidHome}/platform-tools/adb`;
|
|
226
|
+
}
|
|
227
|
+
return "adb";
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract Device interface — the core abstraction that iOS and Android drivers implement.
|
|
3
|
+
* Every MCP tool delegates to this interface. Tools never know which platform they're on.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface DeviceInfo {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
platform: "ios" | "android";
|
|
10
|
+
osVersion: string;
|
|
11
|
+
screenWidth: number;
|
|
12
|
+
screenHeight: number;
|
|
13
|
+
orientation: "portrait" | "landscape";
|
|
14
|
+
isBooted: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface AppInfo {
|
|
18
|
+
appId: string;
|
|
19
|
+
name: string;
|
|
20
|
+
isRunning: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface Bounds {
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SnapshotNode {
|
|
31
|
+
/** Element class/type (e.g., "android.widget.TextView", "UILabel") */
|
|
32
|
+
type: string;
|
|
33
|
+
/** Text content of the element */
|
|
34
|
+
text?: string;
|
|
35
|
+
/** Accessibility label */
|
|
36
|
+
label?: string;
|
|
37
|
+
/** Content description / accessibility hint */
|
|
38
|
+
description?: string;
|
|
39
|
+
/** Bounding box in screen coordinates */
|
|
40
|
+
bounds: Bounds;
|
|
41
|
+
/** Whether the element is interactive (clickable, tappable) */
|
|
42
|
+
interactive: boolean;
|
|
43
|
+
/** Whether the element is focused */
|
|
44
|
+
focused?: boolean;
|
|
45
|
+
/** Whether the element is enabled */
|
|
46
|
+
enabled?: boolean;
|
|
47
|
+
/** Whether the element is visible */
|
|
48
|
+
visible?: boolean;
|
|
49
|
+
/** Element value (for inputs, toggles, etc.) */
|
|
50
|
+
value?: string;
|
|
51
|
+
/** Resource ID (Android) or accessibility identifier (iOS) */
|
|
52
|
+
resourceId?: string;
|
|
53
|
+
/** Child elements */
|
|
54
|
+
children: SnapshotNode[];
|
|
55
|
+
/** Platform-specific raw attributes (for debugging) */
|
|
56
|
+
rawAttributes?: Record<string, string>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface Device {
|
|
60
|
+
/** Get device information */
|
|
61
|
+
getInfo(): Promise<DeviceInfo>;
|
|
62
|
+
|
|
63
|
+
/** Get the accessibility tree of the current screen */
|
|
64
|
+
getSnapshot(): Promise<SnapshotNode>;
|
|
65
|
+
|
|
66
|
+
/** Take a screenshot, returns PNG buffer */
|
|
67
|
+
takeScreenshot(): Promise<Buffer>;
|
|
68
|
+
|
|
69
|
+
/** Tap at screen coordinates */
|
|
70
|
+
tap(x: number, y: number): Promise<void>;
|
|
71
|
+
|
|
72
|
+
/** Double-tap at screen coordinates */
|
|
73
|
+
doubleTap(x: number, y: number): Promise<void>;
|
|
74
|
+
|
|
75
|
+
/** Long press at screen coordinates */
|
|
76
|
+
longPress(x: number, y: number, durationMs?: number): Promise<void>;
|
|
77
|
+
|
|
78
|
+
/** Type text (assumes an input is already focused) */
|
|
79
|
+
typeText(text: string): Promise<void>;
|
|
80
|
+
|
|
81
|
+
/** Swipe from one point to another */
|
|
82
|
+
swipe(
|
|
83
|
+
startX: number,
|
|
84
|
+
startY: number,
|
|
85
|
+
endX: number,
|
|
86
|
+
endY: number,
|
|
87
|
+
durationMs?: number
|
|
88
|
+
): Promise<void>;
|
|
89
|
+
|
|
90
|
+
/** Press a hardware/system button */
|
|
91
|
+
pressButton(button: "home" | "back" | "enter"): Promise<void>;
|
|
92
|
+
|
|
93
|
+
/** Set device orientation */
|
|
94
|
+
setOrientation(orientation: "portrait" | "landscape"): Promise<void>;
|
|
95
|
+
|
|
96
|
+
/** Launch an app by its bundle/package ID */
|
|
97
|
+
launchApp(appId: string): Promise<void>;
|
|
98
|
+
|
|
99
|
+
/** Terminate a running app */
|
|
100
|
+
terminateApp(appId: string): Promise<void>;
|
|
101
|
+
|
|
102
|
+
/** Install an app from a file path */
|
|
103
|
+
installApp(path: string): Promise<void>;
|
|
104
|
+
|
|
105
|
+
/** List installed apps */
|
|
106
|
+
listApps(): Promise<AppInfo[]>;
|
|
107
|
+
|
|
108
|
+
/** Open a URL (deep link or web URL) */
|
|
109
|
+
openUrl(url: string): Promise<void>;
|
|
110
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { SnapshotNode, Bounds } from "../device.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parses iOS accessibility output from `simctl ui` into SnapshotNode tree.
|
|
5
|
+
*
|
|
6
|
+
* The simctl accessibility output varies by iOS/Xcode version. This parser
|
|
7
|
+
* handles the common formats:
|
|
8
|
+
*
|
|
9
|
+
* Format 1 (structured):
|
|
10
|
+
* Element: UIButton, Label: "Submit", Traits: Button, Frame: {{20, 100}, {340, 44}}
|
|
11
|
+
*
|
|
12
|
+
* Format 2 (XML-like from accessibility inspector):
|
|
13
|
+
* <AXElement type="UIButton" label="Submit" frame="20 100 340 44" ...>
|
|
14
|
+
*
|
|
15
|
+
* When neither format is available, we fall back to a basic tree.
|
|
16
|
+
*/
|
|
17
|
+
export function parseIosAccessibilityOutput(
|
|
18
|
+
output: string
|
|
19
|
+
): SnapshotNode {
|
|
20
|
+
// Try XML-like format first
|
|
21
|
+
if (output.includes("<AX") || output.includes("AXElement")) {
|
|
22
|
+
return parseAxFormat(output);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Try structured text format
|
|
26
|
+
if (output.includes("Element:") || output.includes("Label:")) {
|
|
27
|
+
return parseStructuredFormat(output);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Try simctl describe format (indented tree)
|
|
31
|
+
return parseDescribeFormat(output);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function parseAxFormat(output: string): SnapshotNode {
|
|
35
|
+
const root: SnapshotNode = createContainerNode("Screen");
|
|
36
|
+
const lines = output.split("\n");
|
|
37
|
+
|
|
38
|
+
for (const line of lines) {
|
|
39
|
+
const trimmed = line.trim();
|
|
40
|
+
if (!trimmed) continue;
|
|
41
|
+
|
|
42
|
+
const typeMatch = trimmed.match(/type[=:]\s*"?(\w+)"?/i);
|
|
43
|
+
const labelMatch = trimmed.match(/label[=:]\s*"([^"]*)"/i);
|
|
44
|
+
const frameMatch = trimmed.match(
|
|
45
|
+
/frame[=:]\s*"?(\d+)\s+(\d+)\s+(\d+)\s+(\d+)"?/i
|
|
46
|
+
);
|
|
47
|
+
const traitsMatch = trimmed.match(/traits?[=:]\s*"?([^">\n]+)"?/i);
|
|
48
|
+
|
|
49
|
+
if (typeMatch) {
|
|
50
|
+
const node: SnapshotNode = {
|
|
51
|
+
type: simplifyIosType(typeMatch[1]),
|
|
52
|
+
text: labelMatch ? labelMatch[1] : undefined,
|
|
53
|
+
label: labelMatch ? labelMatch[1] : undefined,
|
|
54
|
+
description: undefined,
|
|
55
|
+
bounds: frameMatch
|
|
56
|
+
? {
|
|
57
|
+
x: parseInt(frameMatch[1]),
|
|
58
|
+
y: parseInt(frameMatch[2]),
|
|
59
|
+
width: parseInt(frameMatch[3]),
|
|
60
|
+
height: parseInt(frameMatch[4]),
|
|
61
|
+
}
|
|
62
|
+
: { x: 0, y: 0, width: 0, height: 0 },
|
|
63
|
+
interactive: isInteractiveTrait(traitsMatch?.[1] || ""),
|
|
64
|
+
children: [],
|
|
65
|
+
};
|
|
66
|
+
root.children.push(node);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return root;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseStructuredFormat(output: string): SnapshotNode {
|
|
74
|
+
const root: SnapshotNode = createContainerNode("Screen");
|
|
75
|
+
const blocks = output.split(/\n(?=Element:)/);
|
|
76
|
+
|
|
77
|
+
for (const block of blocks) {
|
|
78
|
+
const typeMatch = block.match(/Element:\s*(\w+)/);
|
|
79
|
+
const labelMatch = block.match(/Label:\s*"([^"]*)"/);
|
|
80
|
+
const frameMatch = block.match(
|
|
81
|
+
/Frame:\s*\{\{(\d+),\s*(\d+)\},\s*\{(\d+),\s*(\d+)\}\}/
|
|
82
|
+
);
|
|
83
|
+
const traitsMatch = block.match(/Traits?:\s*(.+)/);
|
|
84
|
+
|
|
85
|
+
if (typeMatch) {
|
|
86
|
+
const node: SnapshotNode = {
|
|
87
|
+
type: simplifyIosType(typeMatch[1]),
|
|
88
|
+
text: labelMatch ? labelMatch[1] : undefined,
|
|
89
|
+
label: labelMatch ? labelMatch[1] : undefined,
|
|
90
|
+
description: undefined,
|
|
91
|
+
bounds: frameMatch
|
|
92
|
+
? {
|
|
93
|
+
x: parseInt(frameMatch[1]),
|
|
94
|
+
y: parseInt(frameMatch[2]),
|
|
95
|
+
width: parseInt(frameMatch[3]),
|
|
96
|
+
height: parseInt(frameMatch[4]),
|
|
97
|
+
}
|
|
98
|
+
: { x: 0, y: 0, width: 0, height: 0 },
|
|
99
|
+
interactive: isInteractiveTrait(traitsMatch?.[1] || ""),
|
|
100
|
+
children: [],
|
|
101
|
+
};
|
|
102
|
+
root.children.push(node);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return root;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function parseDescribeFormat(output: string): SnapshotNode {
|
|
110
|
+
const root: SnapshotNode = createContainerNode("Screen");
|
|
111
|
+
const lines = output.split("\n");
|
|
112
|
+
|
|
113
|
+
const stack: Array<{ node: SnapshotNode; indent: number }> = [
|
|
114
|
+
{ node: root, indent: -1 },
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
for (const line of lines) {
|
|
118
|
+
if (!line.trim()) continue;
|
|
119
|
+
|
|
120
|
+
const indent = line.search(/\S/);
|
|
121
|
+
const trimmed = line.trim();
|
|
122
|
+
|
|
123
|
+
// Parse "Type: Label" or just "Type"
|
|
124
|
+
const parts = trimmed.split(/:\s*/);
|
|
125
|
+
const type = parts[0] || "View";
|
|
126
|
+
const label = parts.length > 1 ? parts.slice(1).join(": ") : undefined;
|
|
127
|
+
|
|
128
|
+
const node: SnapshotNode = {
|
|
129
|
+
type: simplifyIosType(type),
|
|
130
|
+
text: label,
|
|
131
|
+
label,
|
|
132
|
+
description: undefined,
|
|
133
|
+
bounds: { x: 0, y: 0, width: 0, height: 0 },
|
|
134
|
+
interactive: isInteractiveType(type),
|
|
135
|
+
children: [],
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// Find parent based on indentation
|
|
139
|
+
while (stack.length > 1 && stack[stack.length - 1].indent >= indent) {
|
|
140
|
+
stack.pop();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
stack[stack.length - 1].node.children.push(node);
|
|
144
|
+
stack.push({ node, indent });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return root;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function createContainerNode(name: string): SnapshotNode {
|
|
151
|
+
return {
|
|
152
|
+
type: name,
|
|
153
|
+
bounds: { x: 0, y: 0, width: 0, height: 0 },
|
|
154
|
+
interactive: false,
|
|
155
|
+
children: [],
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function simplifyIosType(type: string): string {
|
|
160
|
+
return type
|
|
161
|
+
.replace(/^UI/, "")
|
|
162
|
+
.replace(/^AX/, "")
|
|
163
|
+
.replace(/^_/, "");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function isInteractiveTrait(traits: string): boolean {
|
|
167
|
+
const lower = traits.toLowerCase();
|
|
168
|
+
return (
|
|
169
|
+
lower.includes("button") ||
|
|
170
|
+
lower.includes("link") ||
|
|
171
|
+
lower.includes("textfield") ||
|
|
172
|
+
lower.includes("searchfield") ||
|
|
173
|
+
lower.includes("adjustable") ||
|
|
174
|
+
lower.includes("toggle")
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function isInteractiveType(type: string): boolean {
|
|
179
|
+
const lower = type.toLowerCase();
|
|
180
|
+
return (
|
|
181
|
+
lower.includes("button") ||
|
|
182
|
+
lower.includes("textfield") ||
|
|
183
|
+
lower.includes("textview") ||
|
|
184
|
+
lower.includes("switch") ||
|
|
185
|
+
lower.includes("slider") ||
|
|
186
|
+
lower.includes("picker") ||
|
|
187
|
+
lower.includes("link")
|
|
188
|
+
);
|
|
189
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { Device, DeviceInfo, AppInfo, SnapshotNode } from "../device.js";
|
|
2
|
+
import { Simctl } from "./simctl.js";
|
|
3
|
+
import { parseIosAccessibilityOutput } from "./accessibility.js";
|
|
4
|
+
|
|
5
|
+
export class IosDevice implements Device {
|
|
6
|
+
private simctl: Simctl;
|
|
7
|
+
|
|
8
|
+
constructor(private deviceId: string) {
|
|
9
|
+
this.simctl = new Simctl(deviceId);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getInfo(): Promise<DeviceInfo> {
|
|
13
|
+
const [deviceInfo, screenSize] = await Promise.all([
|
|
14
|
+
this.simctl.getDeviceInfo(),
|
|
15
|
+
this.simctl.getScreenSize(),
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
id: this.deviceId,
|
|
20
|
+
name: deviceInfo.name,
|
|
21
|
+
platform: "ios",
|
|
22
|
+
osVersion: deviceInfo.runtime,
|
|
23
|
+
screenWidth: screenSize.width,
|
|
24
|
+
screenHeight: screenSize.height,
|
|
25
|
+
orientation: "portrait", // Default; would need to query actual orientation
|
|
26
|
+
isBooted: deviceInfo.state === "Booted",
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async getSnapshot(): Promise<SnapshotNode> {
|
|
31
|
+
const output = await this.simctl.getAccessibilityTree();
|
|
32
|
+
return parseIosAccessibilityOutput(output);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async takeScreenshot(): Promise<Buffer> {
|
|
36
|
+
return this.simctl.screenshot();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async tap(x: number, y: number): Promise<void> {
|
|
40
|
+
await this.simctl.tap(x, y);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async doubleTap(x: number, y: number): Promise<void> {
|
|
44
|
+
await this.simctl.tap(x, y);
|
|
45
|
+
await sleep(100);
|
|
46
|
+
await this.simctl.tap(x, y);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async longPress(x: number, y: number, _durationMs = 1000): Promise<void> {
|
|
50
|
+
// simctl doesn't have a native long-press; we hold via swipe-in-place
|
|
51
|
+
await this.simctl.swipe(x, y, x, y);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async typeText(text: string): Promise<void> {
|
|
55
|
+
await this.simctl.typeText(text);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async swipe(
|
|
59
|
+
startX: number,
|
|
60
|
+
startY: number,
|
|
61
|
+
endX: number,
|
|
62
|
+
endY: number,
|
|
63
|
+
_durationMs = 300
|
|
64
|
+
): Promise<void> {
|
|
65
|
+
await this.simctl.swipe(startX, startY, endX, endY);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async pressButton(button: "home" | "back" | "enter"): Promise<void> {
|
|
69
|
+
switch (button) {
|
|
70
|
+
case "home":
|
|
71
|
+
await this.simctl.pressKey("home");
|
|
72
|
+
break;
|
|
73
|
+
case "back":
|
|
74
|
+
// iOS doesn't have a hardware back button; swipe from left edge
|
|
75
|
+
const screenSize = await this.simctl.getScreenSize();
|
|
76
|
+
await this.simctl.swipe(0, screenSize.height / 2, screenSize.width / 2, screenSize.height / 2);
|
|
77
|
+
break;
|
|
78
|
+
case "enter":
|
|
79
|
+
await this.simctl.pressKey("return");
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async setOrientation(orientation: "portrait" | "landscape"): Promise<void> {
|
|
85
|
+
await this.simctl.setOrientation(orientation);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async launchApp(appId: string): Promise<void> {
|
|
89
|
+
await this.simctl.launchApp(appId);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async terminateApp(appId: string): Promise<void> {
|
|
93
|
+
await this.simctl.terminateApp(appId);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async installApp(path: string): Promise<void> {
|
|
97
|
+
await this.simctl.installApp(path);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async listApps(): Promise<AppInfo[]> {
|
|
101
|
+
const apps = await this.simctl.listApps();
|
|
102
|
+
return apps.map((app) => ({
|
|
103
|
+
appId: app.bundleId,
|
|
104
|
+
name: app.name,
|
|
105
|
+
isRunning: false,
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async openUrl(url: string): Promise<void> {
|
|
110
|
+
await this.simctl.openUrl(url);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function sleep(ms: number): Promise<void> {
|
|
115
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
116
|
+
}
|