@ph-qa/midscene-android 1.6.1

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.
@@ -0,0 +1,279 @@
1
+ import { AbstractInterface } from '@midscene/core/device';
2
+ import type { ActionParam } from '@midscene/core';
3
+ import type { ActionReturn } from '@midscene/core';
4
+ import { ADB } from 'appium-adb';
5
+ import { Agent } from '@midscene/core/agent';
6
+ import { AgentOpt } from '@midscene/core/agent';
7
+ import { AndroidDeviceInputOpt } from '@midscene/core/device';
8
+ import { AndroidDeviceOpt } from '@midscene/core/device';
9
+ import { BaseMCPServer } from '@midscene/shared/mcp';
10
+ import { BaseMidsceneTools } from '@midscene/shared/mcp';
11
+ import { DeviceAction } from '@midscene/core';
12
+ import type { ElementInfo } from '@midscene/shared/extractor';
13
+ import { InterfaceType } from '@midscene/core';
14
+ import { LaunchMCPServerOptions } from '@midscene/shared/mcp';
15
+ import { LaunchMCPServerResult } from '@midscene/shared/mcp';
16
+ import { Point } from '@midscene/core';
17
+ import { Size } from '@midscene/core';
18
+ import { Tool } from '@midscene/shared/mcp';
19
+ import { ToolDefinition } from '@midscene/shared/mcp';
20
+
21
+ declare type ActionArgs<T extends DeviceAction> = [ActionParam<T>] extends [undefined] ? [] : [ActionParam<T>];
22
+
23
+ declare class AndroidAgent extends Agent<AndroidDevice> {
24
+ /**
25
+ * Trigger the system back operation on Android devices
26
+ */
27
+ back: WrappedAction<DeviceActionAndroidBackButton>;
28
+ /**
29
+ * Trigger the system home operation on Android devices
30
+ */
31
+ home: WrappedAction<DeviceActionAndroidHomeButton>;
32
+ /**
33
+ * Trigger the system recent apps operation on Android devices
34
+ */
35
+ recentApps: WrappedAction<DeviceActionAndroidRecentAppsButton>;
36
+ /**
37
+ * User-provided app name to package name mapping
38
+ */
39
+ private appNameMapping;
40
+ constructor(device: AndroidDevice, opts?: AndroidAgentOpt);
41
+ /**
42
+ * Launch an Android app or URL
43
+ * @param uri - App package name, URL, or app name to launch
44
+ */
45
+ launch(uri: string): Promise<void>;
46
+ /**
47
+ * Execute ADB shell command on Android device
48
+ * @param command - ADB shell command to execute
49
+ */
50
+ runAdbShell(command: string): Promise<string>;
51
+ private createActionWrapper;
52
+ }
53
+
54
+ declare type AndroidAgentOpt = AgentOpt & {
55
+ /**
56
+ * Custom mapping of app names to package names
57
+ * User-provided mappings will take precedence over default mappings
58
+ */
59
+ appNameMapping?: Record<string, string>;
60
+ };
61
+
62
+ declare class AndroidDevice implements AbstractInterface {
63
+ private deviceId;
64
+ private yadbPushed;
65
+ private devicePixelRatio;
66
+ private devicePixelRatioInitialized;
67
+ private adb;
68
+ private connectingAdb;
69
+ private destroyed;
70
+ private description;
71
+ private customActions?;
72
+ private cachedScreenSize;
73
+ private cachedOrientation;
74
+ private cachedPhysicalDisplayId;
75
+ private scrcpyAdapter;
76
+ private appNameMapping;
77
+ private cachedAdjustScale;
78
+ private takeScreenshotFailCount;
79
+ private static readonly TAKE_SCREENSHOT_FAIL_THRESHOLD;
80
+ private uiautomatorProcess;
81
+ private uiautomatorConfirmedUnavailable;
82
+ private uiautomatorKnownHealthy;
83
+ interfaceType: InterfaceType;
84
+ uri: string | undefined;
85
+ options?: AndroidDeviceOpt;
86
+ actionSpace(): DeviceAction<any>[];
87
+ constructor(deviceId: string, options?: AndroidDeviceOpt);
88
+ describe(): string;
89
+ connect(): Promise<ADB>;
90
+ getAdb(): Promise<ADB>;
91
+ private createAdbProxy;
92
+ /**
93
+ * Get or create the scrcpy adapter (lazy initialization)
94
+ */
95
+ private getScrcpyAdapter;
96
+ /**
97
+ * Get device physical info needed by scrcpy adapter
98
+ */
99
+ private getDevicePhysicalInfo;
100
+ /**
101
+ * Set the app name to package name mapping
102
+ */
103
+ setAppNameMapping(mapping: Record<string, string>): void;
104
+ /**
105
+ * Resolve app name to package name using the mapping
106
+ * Comparison is case-insensitive and ignores spaces, dashes, and underscores.
107
+ * Keys in appNameMapping are pre-normalized, so we only need to normalize the input.
108
+ * @param appName The app name to resolve
109
+ */
110
+ private resolvePackageName;
111
+ launch(uri: string): Promise<AndroidDevice>;
112
+ execYadb(keyboardContent: string): Promise<void>;
113
+ getElementsInfo(): Promise<ElementInfo[]>;
114
+ getElementsNodeTree(): Promise<any>;
115
+ getScreenSize(): Promise<{
116
+ override: string;
117
+ physical: string;
118
+ orientation: number;
119
+ isCurrentOrientation?: boolean;
120
+ }>;
121
+ private initializeDevicePixelRatio;
122
+ getDisplayDensity(): Promise<number>;
123
+ getDisplayOrientation(): Promise<number>;
124
+ /**
125
+ * Get physical screen dimensions adjusted for current orientation.
126
+ * Swaps width/height when the device is in landscape and the reported
127
+ * dimensions do not already reflect the current orientation.
128
+ */
129
+ private getOrientedPhysicalSize;
130
+ size(): Promise<Size>;
131
+ /**
132
+ * Compute and cache the coordinate adjustment scale by comparing
133
+ * physical dimensions with logical dimensions from size().
134
+ * Cached after first call; invalidated on destroy().
135
+ */
136
+ private getAdjustScale;
137
+ /**
138
+ * Convert logical coordinates (from AI) back to physical coordinates (for ADB).
139
+ * The ratio is derived from size(), so overriding size() alone is sufficient.
140
+ */
141
+ private adjustCoordinates;
142
+ /**
143
+ * Calculate the end point for scroll operations based on start point, scroll delta, and screen boundaries.
144
+ * This method ensures that scroll operations stay within screen bounds and maintain a minimum scroll distance
145
+ * for effective scrolling gestures on Android devices.
146
+ *
147
+ * @param start - The starting point of the scroll gesture
148
+ * @param deltaX - The horizontal scroll distance (positive = scroll right, negative = scroll left)
149
+ * @param deltaY - The vertical scroll distance (positive = scroll down, negative = scroll up)
150
+ * @param maxWidth - The maximum width boundary (screen width)
151
+ * @param maxHeight - The maximum height boundary (screen height)
152
+ * @returns The calculated end point for the scroll gesture
153
+ */
154
+ private calculateScrollEndPoint;
155
+ screenshotBase64(): Promise<string>;
156
+ clearInput(element?: ElementInfo): Promise<void>;
157
+ forceScreenshot(path: string): Promise<void>;
158
+ url(): Promise<string>;
159
+ scrollUntilTop(startPoint?: Point): Promise<void>;
160
+ scrollUntilBottom(startPoint?: Point): Promise<void>;
161
+ scrollUntilLeft(startPoint?: Point): Promise<void>;
162
+ scrollUntilRight(startPoint?: Point): Promise<void>;
163
+ scrollUp(distance?: number, startPoint?: Point): Promise<void>;
164
+ scrollDown(distance?: number, startPoint?: Point): Promise<void>;
165
+ scrollLeft(distance?: number, startPoint?: Point): Promise<void>;
166
+ scrollRight(distance?: number, startPoint?: Point): Promise<void>;
167
+ ensureYadb(): Promise<void>;
168
+ /**
169
+ * Check if text contains characters that may cause issues with ADB inputText.
170
+ * appium-adb's inputText has known bugs with certain characters:
171
+ * - Backslash causes broken shell quoting
172
+ * - Backtick is not escaped at all
173
+ * - Text containing both " and ' throws an error
174
+ * - Dollar sign can cause variable expansion issues
175
+ *
176
+ * For these characters, we route through yadb which handles them correctly
177
+ * via escapeForShell + double-quoted shell context.
178
+ */
179
+ private shouldUseYadbForText;
180
+ keyboardType(text: string, options?: AndroidDeviceInputOpt): Promise<void>;
181
+ private normalizeKeyName;
182
+ keyboardPress(key: string): Promise<void>;
183
+ /**
184
+ * Start the UIAutomator2 server process on the device.
185
+ */
186
+ private startUIAutomatorProcess;
187
+ /**
188
+ * Ensure the UIAutomator2 HTTP server is alive and ready.
189
+ * Returns true if UIAutomator2 is ready to accept requests, false otherwise.
190
+ */
191
+ private ensureUIAutomatorReady;
192
+ mouseClick(x: number, y: number): Promise<void>;
193
+ mouseDoubleClick(x: number, y: number): Promise<void>;
194
+ mouseMove(): Promise<void>;
195
+ mouseDrag(from: {
196
+ x: number;
197
+ y: number;
198
+ }, to: {
199
+ x: number;
200
+ y: number;
201
+ }, duration?: number): Promise<void>;
202
+ scroll(deltaX: number, deltaY: number, duration?: number): Promise<void>;
203
+ destroy(): Promise<void>;
204
+ /**
205
+ * Get the current time from the Android device.
206
+ * Returns the device's current timestamp in milliseconds.
207
+ * This is useful when the system time and device time are not synchronized.
208
+ */
209
+ getTimestamp(): Promise<number>;
210
+ back(): Promise<void>;
211
+ home(): Promise<void>;
212
+ recentApps(): Promise<void>;
213
+ longPress(x: number, y: number, duration?: number): Promise<void>;
214
+ pullDown(startPoint?: Point, distance?: number, duration?: number): Promise<void>;
215
+ pullDrag(from: {
216
+ x: number;
217
+ y: number;
218
+ }, to: {
219
+ x: number;
220
+ y: number;
221
+ }, duration: number): Promise<void>;
222
+ pullUp(startPoint?: Point, distance?: number, duration?: number): Promise<void>;
223
+ private getDisplayArg;
224
+ getPhysicalDisplayId(): Promise<string | null>;
225
+ hideKeyboard(options?: AndroidDeviceInputOpt, timeoutMs?: number): Promise<boolean>;
226
+ }
227
+
228
+ /**
229
+ * Android MCP Server
230
+ * Provides MCP tools for Android automation through ADB
231
+ */
232
+ export declare class AndroidMCPServer extends BaseMCPServer {
233
+ constructor(toolsManager?: AndroidMidsceneTools);
234
+ protected createToolsManager(): AndroidMidsceneTools;
235
+ }
236
+
237
+ /**
238
+ * Android-specific tools manager
239
+ * Extends BaseMidsceneTools to provide Android ADB device connection tools
240
+ */
241
+ declare class AndroidMidsceneTools extends BaseMidsceneTools<AndroidAgent> {
242
+ protected createTemporaryDevice(): AndroidDevice;
243
+ protected ensureAgent(deviceId?: string): Promise<AndroidAgent>;
244
+ /**
245
+ * Provide Android-specific platform tools
246
+ */
247
+ protected preparePlatformTools(): ToolDefinition[];
248
+ }
249
+
250
+ declare type DeviceActionAndroidBackButton = DeviceAction<undefined, void>;
251
+
252
+ declare type DeviceActionAndroidHomeButton = DeviceAction<undefined, void>;
253
+
254
+ declare type DeviceActionAndroidRecentAppsButton = DeviceAction<undefined, void>;
255
+
256
+ /**
257
+ * Create MCP kit for a specific Android Agent
258
+ */
259
+ export declare function mcpKitForAgent(agent: Agent | AndroidAgent): Promise<{
260
+ description: string;
261
+ tools: Tool[];
262
+ }>;
263
+
264
+ /**
265
+ * Create an MCP server launcher for a specific Android Agent
266
+ */
267
+ export declare function mcpServerForAgent(agent: Agent | AndroidAgent): {
268
+ launch(options?: {
269
+ verbose?: boolean;
270
+ }): Promise<LaunchMCPServerResult>;
271
+ launchHttp(options: LaunchMCPServerOptions): Promise<LaunchMCPServerResult>;
272
+ };
273
+
274
+ /**
275
+ * Helper type to convert DeviceAction to wrapped method signature
276
+ */
277
+ declare type WrappedAction<T extends DeviceAction> = (...args: ActionArgs<T>) => Promise<ActionReturn<T>>;
278
+
279
+ export { }
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@ph-qa/midscene-android",
3
+ "version": "1.6.1",
4
+ "description": "Android automation library for Midscene with UIAutomator2 fast-click support (forked by PuHui QA)",
5
+ "keywords": [
6
+ "Android UI automation",
7
+ "Android AI testing",
8
+ "Android automation library",
9
+ "Android automation tool",
10
+ "Android use",
11
+ "uiautomator2",
12
+ "midscene"
13
+ ],
14
+ "main": "./dist/lib/index.js",
15
+ "module": "./dist/es/index.mjs",
16
+ "types": "./dist/types/index.d.ts",
17
+ "bin": {
18
+ "midscene-android": "./bin/midscene-android"
19
+ },
20
+ "files": ["bin", "dist", "README.md"],
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/types/index.d.ts",
24
+ "import": "./dist/es/index.mjs",
25
+ "require": "./dist/lib/index.js"
26
+ },
27
+ "./mcp-server": {
28
+ "types": "./dist/types/mcp-server.d.ts",
29
+ "import": "./dist/es/mcp-server.mjs",
30
+ "require": "./dist/lib/mcp-server.js"
31
+ },
32
+ "./package.json": "./package.json"
33
+ },
34
+ "scripts": {
35
+ "dev": "npm run build:watch",
36
+ "prebuild": "node scripts/download-scrcpy-server.mjs && node scripts/download-yadb.mjs",
37
+ "build": "rslib build",
38
+ "build:watch": "rslib build --watch --no-clean",
39
+ "prepack": "node scripts/download-scrcpy-server.mjs && node scripts/download-yadb.mjs",
40
+ "playground": "DEBUG=midscene:* tsx demo/playground.ts",
41
+ "test": "vitest --run",
42
+ "test:u": "vitest --run -u",
43
+ "test:ai": "AI_TEST_TYPE=android npm run test",
44
+ "test:ai:cache": "MIDSCENE_CACHE=true AI_TEST_TYPE=android npm run test"
45
+ },
46
+ "dependencies": {
47
+ "@midscene/core": "^1.6.1",
48
+ "@midscene/shared": "^1.6.1",
49
+ "@yume-chan/adb": "2.5.1",
50
+ "@yume-chan/adb-scrcpy": "2.3.2",
51
+ "@yume-chan/adb-server-node-tcp": "2.5.2",
52
+ "@yume-chan/scrcpy": "2.3.0",
53
+ "@yume-chan/stream-extra": "2.1.0",
54
+ "appium-adb": "12.12.1",
55
+ "sharp": "^0.34.3"
56
+ },
57
+ "optionalDependencies": {
58
+ "@ffmpeg-installer/ffmpeg": "^1.1.0"
59
+ },
60
+ "devDependencies": {
61
+ "@midscene/playground": "^1.6.1",
62
+ "@rslib/core": "^0.18.3",
63
+ "@types/node": "^18.0.0",
64
+ "dotenv": "^16.4.5",
65
+ "gh-release-fetch": "^4.0.3",
66
+ "typescript": "^5.8.3",
67
+ "tsx": "^4.19.2",
68
+ "vitest": "3.0.5",
69
+ "zod": "3.24.3"
70
+ },
71
+ "repository": {
72
+ "type": "git",
73
+ "url": "https://github.com/puhui-qa/midscene-android"
74
+ },
75
+ "license": "MIT"
76
+ }