-
Notifications
You must be signed in to change notification settings - Fork 384
Expand file tree
/
Copy pathmobile-device.ts
More file actions
234 lines (201 loc) · 5.96 KB
/
mobile-device.ts
File metadata and controls
234 lines (201 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { Mobilecli } from "./mobilecli";
import { ActionableError, Button, InstalledApp, Orientation, Robot, ScreenElement, ScreenSize, SwipeDirection } from "./robot";
interface InstalledAppsResponse {
status: "ok",
data: Array<{
packageName: string;
appName?: string; // ios
version?: string; // ios
}>;
}
interface DeviceInfoResponse {
status: "ok",
data: {
device: {
id: string;
name: string;
platform: string;
type: string;
version: string;
state: string;
screenSize?: {
width: number;
height: number;
scale: number;
};
};
};
}
interface UIElementResponse {
type: string;
label?: string;
text?: string;
name?: string;
value?: string;
identifier?: string;
rect: {
x: number;
y: number;
width: number;
height: number;
};
focused?: boolean;
}
interface DumpUIResponse {
status: "ok",
data: {
elements: UIElementResponse[];
};
}
interface OrientationResponse {
status: "ok",
data: {
orientation: Orientation;
};
}
export class MobileDevice implements Robot {
private mobilecli: Mobilecli;
public constructor(private deviceId: string) {
this.mobilecli = new Mobilecli();
}
private runCommand(args: string[]): string {
const fullArgs = [...args, "--device", this.deviceId];
return this.mobilecli.executeCommand(fullArgs);
}
public async getScreenSize(): Promise<ScreenSize> {
const response = JSON.parse(this.runCommand(["device", "info"])) as DeviceInfoResponse;
if (response.data.device.screenSize) {
return response.data.device.screenSize;
}
return { width: 0, height: 0, scale: 1.0 };
}
public async swipe(direction: SwipeDirection): Promise<void> {
const screenSize = await this.getScreenSize();
const centerX = Math.floor(screenSize.width / 2);
const centerY = Math.floor(screenSize.height / 2);
const distance = 400; // Default distance in pixels
let startX = centerX;
let startY = centerY;
let endX = centerX;
let endY = centerY;
switch (direction) {
case "up":
startY = centerY + distance / 2;
endY = centerY - distance / 2;
break;
case "down":
startY = centerY - distance / 2;
endY = centerY + distance / 2;
break;
case "left":
startX = centerX + distance / 2;
endX = centerX - distance / 2;
break;
case "right":
startX = centerX - distance / 2;
endX = centerX + distance / 2;
break;
}
this.runCommand(["io", "swipe", `${startX},${startY},${endX},${endY}`]);
}
public async swipeFromCoordinate(x: number, y: number, direction: SwipeDirection, distance?: number): Promise<void> {
const swipeDistance = distance || 400;
let endX = x;
let endY = y;
switch (direction) {
case "up":
endY = y - swipeDistance;
break;
case "down":
endY = y + swipeDistance;
break;
case "left":
endX = x - swipeDistance;
break;
case "right":
endX = x + swipeDistance;
break;
}
this.runCommand(["io", "swipe", `${x},${y},${endX},${endY}`]);
}
public async getScreenshot(): Promise<Buffer> {
const fullArgs = ["screenshot", "--device", this.deviceId, "--format", "png", "--output", "-"];
return this.mobilecli.executeCommandBuffer(fullArgs);
}
public async listApps(): Promise<InstalledApp[]> {
const response = JSON.parse(this.runCommand(["apps", "list"])) as InstalledAppsResponse;
return response.data.map(app => ({
appName: app.appName || app.packageName,
packageName: app.packageName,
})) as InstalledApp[];
}
public async launchApp(packageName: string): Promise<void> {
this.runCommand(["apps", "launch", packageName]);
}
public async terminateApp(packageName: string): Promise<void> {
this.runCommand(["apps", "terminate", packageName]);
}
public async installApp(path: string): Promise<void> {
this.runCommand(["apps", "install", path]);
}
public async uninstallApp(bundleId: string): Promise<void> {
this.runCommand(["apps", "uninstall", bundleId]);
}
public async openUrl(url: string): Promise<void> {
this.runCommand(["url", url]);
}
public async sendKeys(text: string): Promise<void> {
this.runCommand(["io", "text", text]);
}
public async pressButton(button: Button): Promise<void> {
this.runCommand(["io", "button", button]);
}
public async tap(x: number, y: number): Promise<void> {
this.runCommand(["io", "tap", `${x},${y}`]);
}
public async doubleTap(x: number, y: number): Promise<void> {
// TODO: should move into mobilecli itself as "io doubletap"
await this.tap(x, y);
await this.tap(x, y);
}
public async longPress(x: number, y: number, duration: number): Promise<void> {
this.runCommand(["io", "longpress", `${x},${y}`, "--duration", `${duration}`]);
}
public async getElementsOnScreen(): Promise<ScreenElement[]> {
const response = JSON.parse(this.runCommand(["dump", "ui"])) as DumpUIResponse;
return response.data.elements.map(element => ({
type: element.type,
label: element.label,
text: element.text,
name: element.name,
value: element.value,
identifier: element.identifier,
rect: element.rect,
focused: element.focused,
}));
}
public async setOrientation(orientation: Orientation): Promise<void> {
this.runCommand(["device", "orientation", "set", orientation]);
}
public async getOrientation(): Promise<Orientation> {
const response = JSON.parse(this.runCommand(["device", "orientation", "get"])) as OrientationResponse;
return response.data.orientation;
}
public async getCurrentActivity(): Promise<{ id: string; isCanonical: boolean }> {
try {
const response = JSON.parse(this.runCommand(["device", "get-current-activity"])) as any;
if (response.status === "ok" && response.data?.id) {
return {
id: response.data.id,
isCanonical: response.data.isCanonical ?? true
};
}
throw new ActionableError("No activity is currently in focus. Please launch an app and try again.");
} catch (error) {
if (error instanceof ActionableError) {
throw error;
}
throw new ActionableError("Failed to get current activity. Please ensure the device is properly connected.");
}
}
}