spana-test 0.0.2 → 0.2.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/.agents/skills/spana-testing/SKILL.md +304 -0
- package/README.md +308 -88
- package/dist/agent/index.d.ts +110 -0
- package/dist/agent/index.js +25 -0
- package/dist/allure-PTTVORQ6.js +124 -0
- package/dist/allure-PTTVORQ6.js.map +1 -0
- package/dist/android-PAPJJWVN.js +34 -0
- package/dist/android-PAPJJWVN.js.map +1 -0
- package/dist/chunk-3FSZNKEF.js +62 -0
- package/dist/chunk-3FSZNKEF.js.map +1 -0
- package/dist/chunk-3YR4GUGE.js +249 -0
- package/dist/chunk-3YR4GUGE.js.map +1 -0
- package/dist/{discover-XIRJTZ7Z.js → chunk-AEKDFFPJ.js} +12 -5
- package/dist/{discover-XIRJTZ7Z.js.map → chunk-AEKDFFPJ.js.map} +1 -1
- package/dist/chunk-BF37W5EG.js +125 -0
- package/dist/chunk-BF37W5EG.js.map +1 -0
- package/dist/chunk-DGUM43GV.js +11 -0
- package/dist/chunk-DGUM43GV.js.map +1 -0
- package/dist/chunk-EXLUY7KT.js +21 -0
- package/dist/chunk-EXLUY7KT.js.map +1 -0
- package/dist/chunk-FNDOEMOL.js +62 -0
- package/dist/chunk-FNDOEMOL.js.map +1 -0
- package/dist/{chunk-CYEDLQJ4.js → chunk-GZK22EIE.js} +76 -102
- package/dist/chunk-GZK22EIE.js.map +1 -0
- package/dist/chunk-HWFDXIXR.js +183 -0
- package/dist/chunk-HWFDXIXR.js.map +1 -0
- package/dist/chunk-IJIASIX5.js +314 -0
- package/dist/chunk-IJIASIX5.js.map +1 -0
- package/dist/chunk-KJPHH2TW.js +168 -0
- package/dist/chunk-KJPHH2TW.js.map +1 -0
- package/dist/chunk-L7MFJF46.js +137 -0
- package/dist/chunk-L7MFJF46.js.map +1 -0
- package/dist/chunk-MRH7SK52.js +134 -0
- package/dist/chunk-MRH7SK52.js.map +1 -0
- package/dist/chunk-P7JQRLP4.js +114 -0
- package/dist/chunk-P7JQRLP4.js.map +1 -0
- package/dist/chunk-S2XNAKSO.js +1087 -0
- package/dist/chunk-S2XNAKSO.js.map +1 -0
- package/dist/chunk-S4R4OR7T.js +358 -0
- package/dist/chunk-S4R4OR7T.js.map +1 -0
- package/dist/chunk-SFBRFB2N.js +277 -0
- package/dist/chunk-SFBRFB2N.js.map +1 -0
- package/dist/chunk-VNYF5BHX.js +494 -0
- package/dist/chunk-VNYF5BHX.js.map +1 -0
- package/dist/chunk-WBU34APM.js +38 -0
- package/dist/chunk-WBU34APM.js.map +1 -0
- package/dist/chunk-WQ4GXUUE.js +2402 -0
- package/dist/chunk-WQ4GXUUE.js.map +1 -0
- package/dist/chunk-YBZCOQXJ.js +311 -0
- package/dist/chunk-YBZCOQXJ.js.map +1 -0
- package/dist/cli.js +428 -34
- package/dist/cli.js.map +1 -1
- package/dist/compiler-74VGKZO6.js +312 -0
- package/dist/compiler-74VGKZO6.js.map +1 -0
- package/dist/config-loader-BJJ3IJJ7.js +9 -0
- package/dist/config-loader-BJJ3IJJ7.js.map +1 -0
- package/dist/console-XWKUTP3D.js +164 -0
- package/dist/console-XWKUTP3D.js.map +1 -0
- package/dist/coordinator-BbUjvvnX.d.ts +383 -0
- package/dist/discover-KZKXHZZA.js +14 -0
- package/dist/discover-KZKXHZZA.js.map +1 -0
- package/dist/doctor-command-T2UX5RV5.js +732 -0
- package/dist/doctor-command-T2UX5RV5.js.map +1 -0
- package/dist/element-DZ0HyJ3C.d.ts +25 -0
- package/dist/flow-BUC9Mf8W.d.ts +128 -0
- package/dist/gherkin/steps.d.ts +32 -0
- package/dist/gherkin/steps.js +39 -0
- package/dist/gherkin/steps.js.map +1 -0
- package/dist/{html-GRB34S6X.js → html-GE6Q4DCG.js} +76 -22
- package/dist/html-GE6Q4DCG.js.map +1 -0
- package/dist/index.d.ts +159 -144
- package/dist/index.js +39 -165
- package/dist/index.js.map +1 -1
- package/dist/init-command-GVMRXIMY.js +121 -0
- package/dist/init-command-GVMRXIMY.js.map +1 -0
- package/dist/init-flow-command-R76AXF4Z.js +34 -0
- package/dist/init-flow-command-R76AXF4Z.js.map +1 -0
- package/dist/installer-YZJ4DV3F.js +22 -0
- package/dist/installer-YZJ4DV3F.js.map +1 -0
- package/dist/ios-ZW6XVNLG.js +50 -0
- package/dist/ios-ZW6XVNLG.js.map +1 -0
- package/dist/json-KS67BZYU.js +39 -0
- package/dist/json-KS67BZYU.js.map +1 -0
- package/dist/{junit-NAGZJOGQ.js → junit-HLC62MK6.js} +15 -4
- package/dist/junit-HLC62MK6.js.map +1 -0
- package/dist/provider-A57C3MU6.js +12 -0
- package/dist/provider-A57C3MU6.js.map +1 -0
- package/dist/redact-U72BUBBR.js +77 -0
- package/dist/redact-U72BUBBR.js.map +1 -0
- package/dist/runner-UXNHVVGG.js +20 -0
- package/dist/runner-UXNHVVGG.js.map +1 -0
- package/dist/server-W7OA3DDN.js +530 -0
- package/dist/server-W7OA3DDN.js.map +1 -0
- package/dist/session-TMNOKYLG.js +17 -0
- package/dist/session-TMNOKYLG.js.map +1 -0
- package/dist/studio-command-6VHTHDUK.js +24 -0
- package/dist/studio-command-6VHTHDUK.js.map +1 -0
- package/dist/test-command-AHMOZLV4.js +2852 -0
- package/dist/test-command-AHMOZLV4.js.map +1 -0
- package/dist/validator-35624P7S.js +20 -0
- package/dist/validator-35624P7S.js.map +1 -0
- package/package.json +41 -4
- package/studio-dist/assets/index-DC6DIXfl.js +188 -0
- package/studio-dist/assets/index-DDKsnBwD.css +1 -0
- package/studio-dist/index.html +13 -0
- package/dist/chunk-CYEDLQJ4.js.map +0 -1
- package/dist/chunk-PVNNKPBW.js +0 -64
- package/dist/chunk-PVNNKPBW.js.map +0 -1
- package/dist/chunk-VZZH66U5.js +0 -1781
- package/dist/chunk-VZZH66U5.js.map +0 -1
- package/dist/console-GPV3M3PC.js +0 -71
- package/dist/console-GPV3M3PC.js.map +0 -1
- package/dist/html-GRB34S6X.js.map +0 -1
- package/dist/json-FK2NSRGC.js +0 -20
- package/dist/json-FK2NSRGC.js.map +0 -1
- package/dist/junit-NAGZJOGQ.js.map +0 -1
- package/dist/runner-LBQUCG3K.js +0 -12
- package/dist/session-U7VP5J2C.js +0 -122
- package/dist/session-U7VP5J2C.js.map +0 -1
- package/dist/test-command-WPYLJYIK.js +0 -636
- package/dist/test-command-WPYLJYIK.js.map +0 -1
- package/dist/validator-J4YSBB66.js +0 -33
- package/dist/validator-J4YSBB66.js.map +0 -1
- /package/dist/{runner-LBQUCG3K.js.map → agent/index.js.map} +0 -0
package/dist/chunk-VZZH66U5.js
DELETED
|
@@ -1,1781 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
adbClearApp,
|
|
4
|
-
adbForceStop,
|
|
5
|
-
adbForward,
|
|
6
|
-
adbInstall,
|
|
7
|
-
adbLaunchApp,
|
|
8
|
-
adbOpenLink,
|
|
9
|
-
adbShell,
|
|
10
|
-
findADB,
|
|
11
|
-
installedUrlSchemesOnSimulator,
|
|
12
|
-
launchOnSimulator,
|
|
13
|
-
launchWithUrlOnSimulator,
|
|
14
|
-
terminateOnSimulator
|
|
15
|
-
} from "./chunk-CYEDLQJ4.js";
|
|
16
|
-
|
|
17
|
-
// src/smart/element-matcher.ts
|
|
18
|
-
function flattenElements(root) {
|
|
19
|
-
const result = [root];
|
|
20
|
-
if (root.children) {
|
|
21
|
-
for (const child of root.children) {
|
|
22
|
-
result.push(...flattenElements(child));
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
function matchesSelector(element, selector) {
|
|
28
|
-
if (typeof selector === "string") {
|
|
29
|
-
return element.text?.toLowerCase().includes(selector.toLowerCase()) ?? false;
|
|
30
|
-
}
|
|
31
|
-
if ("testID" in selector) {
|
|
32
|
-
return element.id === selector.testID;
|
|
33
|
-
}
|
|
34
|
-
if ("text" in selector) {
|
|
35
|
-
return element.text?.toLowerCase().includes(selector.text.toLowerCase()) ?? false;
|
|
36
|
-
}
|
|
37
|
-
if ("accessibilityLabel" in selector) {
|
|
38
|
-
return element.accessibilityLabel === selector.accessibilityLabel;
|
|
39
|
-
}
|
|
40
|
-
if ("point" in selector) {
|
|
41
|
-
const { x, y } = selector.point;
|
|
42
|
-
const b = element.bounds;
|
|
43
|
-
return x >= b.x && x <= b.x + b.width && y >= b.y && y <= b.y + b.height;
|
|
44
|
-
}
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
function findElements(root, selector) {
|
|
48
|
-
return flattenElements(root).filter((el) => matchesSelector(el, selector));
|
|
49
|
-
}
|
|
50
|
-
function isProbablyOnScreen(element) {
|
|
51
|
-
const centerX = element.bounds.x + element.bounds.width / 2;
|
|
52
|
-
const centerY = element.bounds.y + element.bounds.height / 2;
|
|
53
|
-
return element.bounds.width > 0 && element.bounds.height > 0 && centerX >= 0 && centerY >= 0;
|
|
54
|
-
}
|
|
55
|
-
function findElement(root, selector) {
|
|
56
|
-
const matches = findElements(root, selector).filter((el) => el.visible !== false);
|
|
57
|
-
if (matches.length === 0) return void 0;
|
|
58
|
-
const visibleMatches = matches.filter(isProbablyOnScreen);
|
|
59
|
-
if (visibleMatches.length === 0) return void 0;
|
|
60
|
-
const clickable = visibleMatches.filter((el) => el.clickable);
|
|
61
|
-
if (clickable.length > 0) return clickable[clickable.length - 1];
|
|
62
|
-
return visibleMatches[visibleMatches.length - 1];
|
|
63
|
-
}
|
|
64
|
-
function centerOf(element) {
|
|
65
|
-
return {
|
|
66
|
-
x: Math.round(element.bounds.x + element.bounds.width / 2),
|
|
67
|
-
y: Math.round(element.bounds.y + element.bounds.height / 2)
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// src/drivers/playwright-parser.ts
|
|
72
|
-
function rawNodeToElement(node) {
|
|
73
|
-
const children = node.children && node.children.length > 0 ? node.children.map(rawNodeToElement) : void 0;
|
|
74
|
-
return {
|
|
75
|
-
...node.id !== void 0 ? { id: node.id } : {},
|
|
76
|
-
...node.text !== void 0 ? { text: node.text } : {},
|
|
77
|
-
...node.accessibilityLabel !== void 0 ? { accessibilityLabel: node.accessibilityLabel } : {},
|
|
78
|
-
// Use role as elementType when present, otherwise fall back to HTML tag name
|
|
79
|
-
...node.role !== void 0 ? { elementType: node.role } : node.tag !== void 0 ? { elementType: node.tag } : {},
|
|
80
|
-
bounds: {
|
|
81
|
-
x: node.bounds.x,
|
|
82
|
-
y: node.bounds.y,
|
|
83
|
-
width: node.bounds.width,
|
|
84
|
-
height: node.bounds.height
|
|
85
|
-
},
|
|
86
|
-
...node.enabled !== void 0 ? { enabled: node.enabled } : {},
|
|
87
|
-
...node.visible !== void 0 ? { visible: node.visible } : {},
|
|
88
|
-
...node.clickable !== void 0 ? { clickable: node.clickable } : {},
|
|
89
|
-
...children !== void 0 ? { children } : {}
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
function parseWebHierarchy(json) {
|
|
93
|
-
const raw = JSON.parse(json);
|
|
94
|
-
return rawNodeToElement(raw);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// src/drivers/playwright.ts
|
|
98
|
-
import { Effect, Layer } from "effect";
|
|
99
|
-
import { chromium } from "playwright-core";
|
|
100
|
-
|
|
101
|
-
// src/errors.ts
|
|
102
|
-
import { Schema } from "effect";
|
|
103
|
-
var ElementNotFoundError = class extends Schema.TaggedError()(
|
|
104
|
-
"ElementNotFoundError",
|
|
105
|
-
{
|
|
106
|
-
message: Schema.String,
|
|
107
|
-
selector: Schema.Unknown,
|
|
108
|
-
timeoutMs: Schema.optional(Schema.Number)
|
|
109
|
-
}
|
|
110
|
-
) {
|
|
111
|
-
};
|
|
112
|
-
var TextMismatchError = class extends Schema.TaggedError()(
|
|
113
|
-
"TextMismatchError",
|
|
114
|
-
{
|
|
115
|
-
message: Schema.String,
|
|
116
|
-
expected: Schema.String,
|
|
117
|
-
actual: Schema.optional(Schema.String),
|
|
118
|
-
selector: Schema.Unknown
|
|
119
|
-
}
|
|
120
|
-
) {
|
|
121
|
-
};
|
|
122
|
-
var TimeoutError = class extends Schema.TaggedError()(
|
|
123
|
-
"TimeoutError",
|
|
124
|
-
{
|
|
125
|
-
message: Schema.String,
|
|
126
|
-
operation: Schema.String,
|
|
127
|
-
timeoutMs: Schema.Number
|
|
128
|
-
}
|
|
129
|
-
) {
|
|
130
|
-
};
|
|
131
|
-
var WaitTimeoutError = class extends Schema.TaggedError()(
|
|
132
|
-
"WaitTimeoutError",
|
|
133
|
-
{
|
|
134
|
-
message: Schema.String,
|
|
135
|
-
selector: Schema.Unknown,
|
|
136
|
-
timeoutMs: Schema.Number
|
|
137
|
-
}
|
|
138
|
-
) {
|
|
139
|
-
};
|
|
140
|
-
var DriverError = class extends Schema.TaggedError()(
|
|
141
|
-
"DriverError",
|
|
142
|
-
{
|
|
143
|
-
message: Schema.String,
|
|
144
|
-
command: Schema.optional(Schema.String),
|
|
145
|
-
exitCode: Schema.optional(Schema.Number),
|
|
146
|
-
stdout: Schema.optional(Schema.String),
|
|
147
|
-
stderr: Schema.optional(Schema.String)
|
|
148
|
-
}
|
|
149
|
-
) {
|
|
150
|
-
};
|
|
151
|
-
var DeviceDisconnectedError = class extends Schema.TaggedError()(
|
|
152
|
-
"DeviceDisconnectedError",
|
|
153
|
-
{
|
|
154
|
-
message: Schema.String,
|
|
155
|
-
deviceId: Schema.optional(Schema.String),
|
|
156
|
-
platform: Schema.optional(Schema.String)
|
|
157
|
-
}
|
|
158
|
-
) {
|
|
159
|
-
};
|
|
160
|
-
var AppCrashedError = class extends Schema.TaggedError()(
|
|
161
|
-
"AppCrashedError",
|
|
162
|
-
{
|
|
163
|
-
message: Schema.String,
|
|
164
|
-
appId: Schema.optional(Schema.String),
|
|
165
|
-
platform: Schema.optional(Schema.String)
|
|
166
|
-
}
|
|
167
|
-
) {
|
|
168
|
-
};
|
|
169
|
-
var AppNotInstalledError = class extends Schema.TaggedError()(
|
|
170
|
-
"AppNotInstalledError",
|
|
171
|
-
{
|
|
172
|
-
message: Schema.String,
|
|
173
|
-
appId: Schema.String,
|
|
174
|
-
platform: Schema.optional(Schema.String)
|
|
175
|
-
}
|
|
176
|
-
) {
|
|
177
|
-
};
|
|
178
|
-
var FlowSyntaxError = class extends Schema.TaggedError()(
|
|
179
|
-
"FlowSyntaxError",
|
|
180
|
-
{
|
|
181
|
-
message: Schema.String,
|
|
182
|
-
filePath: Schema.String
|
|
183
|
-
}
|
|
184
|
-
) {
|
|
185
|
-
};
|
|
186
|
-
var DeviceNotFoundError = class extends Schema.TaggedError()(
|
|
187
|
-
"DeviceNotFoundError",
|
|
188
|
-
{
|
|
189
|
-
message: Schema.String,
|
|
190
|
-
platform: Schema.optional(Schema.String),
|
|
191
|
-
deviceId: Schema.optional(Schema.String)
|
|
192
|
-
}
|
|
193
|
-
) {
|
|
194
|
-
};
|
|
195
|
-
var ConfigError = class extends Schema.TaggedError()(
|
|
196
|
-
"ConfigError",
|
|
197
|
-
{
|
|
198
|
-
message: Schema.String,
|
|
199
|
-
filePath: Schema.optional(Schema.String)
|
|
200
|
-
}
|
|
201
|
-
) {
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
// src/drivers/raw-driver.ts
|
|
205
|
-
import { Context } from "effect";
|
|
206
|
-
var RawDriver = class extends Context.Tag("RawDriver")() {
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
// src/drivers/playwright.ts
|
|
210
|
-
function makePlaywrightDriver(config) {
|
|
211
|
-
return Effect.gen(function* () {
|
|
212
|
-
const browser = yield* Effect.tryPromise({
|
|
213
|
-
try: () => chromium.launch({ headless: config.headless ?? true }),
|
|
214
|
-
catch: (e) => new DriverError({ message: `Failed to launch browser: ${e}` })
|
|
215
|
-
});
|
|
216
|
-
const page = yield* Effect.tryPromise({
|
|
217
|
-
try: () => browser.newPage(),
|
|
218
|
-
catch: (e) => new DriverError({ message: `Failed to create page: ${e}` })
|
|
219
|
-
});
|
|
220
|
-
const service = {
|
|
221
|
-
dumpHierarchy: () => Effect.tryPromise({
|
|
222
|
-
try: async () => {
|
|
223
|
-
const tree = await page.evaluate(`
|
|
224
|
-
(function() {
|
|
225
|
-
function walk(el) {
|
|
226
|
-
var rect = el.getBoundingClientRect();
|
|
227
|
-
var style = window.getComputedStyle(el);
|
|
228
|
-
var isVisible = style.display !== "none" && style.visibility !== "hidden" && style.opacity !== "0";
|
|
229
|
-
return {
|
|
230
|
-
tag: el.tagName.toLowerCase(),
|
|
231
|
-
id: el.getAttribute("data-testid") || el.getAttribute("testID") || undefined,
|
|
232
|
-
text: el.childNodes.length === 1 && el.childNodes[0] && el.childNodes[0].nodeType === 3
|
|
233
|
-
? (el.childNodes[0].textContent || "").trim() || undefined
|
|
234
|
-
: undefined,
|
|
235
|
-
accessibilityLabel: el.getAttribute("aria-label") || undefined,
|
|
236
|
-
role: el.getAttribute("role") || undefined,
|
|
237
|
-
bounds: { x: rect.x, y: rect.y, width: rect.width, height: rect.height },
|
|
238
|
-
enabled: !el.hasAttribute("disabled"),
|
|
239
|
-
visible: isVisible && rect.width > 0 && rect.height > 0,
|
|
240
|
-
clickable: el.tagName === "BUTTON" || el.tagName === "A" || el.getAttribute("role") === "button" || el.onclick !== null,
|
|
241
|
-
children: Array.from(el.children).map(walk),
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
return walk(document.body);
|
|
245
|
-
})()
|
|
246
|
-
`);
|
|
247
|
-
return JSON.stringify(tree);
|
|
248
|
-
},
|
|
249
|
-
catch: (e) => new DriverError({ message: `Failed to dump hierarchy: ${e}` })
|
|
250
|
-
}),
|
|
251
|
-
tapAtCoordinate: (x, y) => Effect.tryPromise({
|
|
252
|
-
try: () => page.evaluate(`
|
|
253
|
-
(function(x, y) {
|
|
254
|
-
function isClickable(el) {
|
|
255
|
-
return el.tagName === "BUTTON"
|
|
256
|
-
|| el.tagName === "A"
|
|
257
|
-
|| el.getAttribute("role") === "button"
|
|
258
|
-
|| el.onclick !== null;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function area(el) {
|
|
262
|
-
var rect = el.getBoundingClientRect();
|
|
263
|
-
return rect.width * rect.height;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
var elements = document.elementsFromPoint(x, y);
|
|
267
|
-
var candidates = [];
|
|
268
|
-
|
|
269
|
-
for (var i = 0; i < elements.length; i++) {
|
|
270
|
-
var raw = elements[i];
|
|
271
|
-
var candidate = raw.closest("button, a, [role=\\"button\\"]") || raw;
|
|
272
|
-
|
|
273
|
-
if (
|
|
274
|
-
candidate instanceof HTMLElement
|
|
275
|
-
&& isClickable(candidate)
|
|
276
|
-
&& candidates.indexOf(candidate) === -1
|
|
277
|
-
) {
|
|
278
|
-
candidates.push(candidate);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
var target = candidates.sort(function(a, b) {
|
|
283
|
-
return area(a) - area(b);
|
|
284
|
-
})[0] || document.elementFromPoint(x, y);
|
|
285
|
-
|
|
286
|
-
if (!(target instanceof HTMLElement)) {
|
|
287
|
-
throw new Error("No clickable element found at tap point");
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
target.click();
|
|
291
|
-
})(${x}, ${y})
|
|
292
|
-
`),
|
|
293
|
-
catch: (e) => new DriverError({ message: `Failed to tap at (${x}, ${y}): ${e}` })
|
|
294
|
-
}),
|
|
295
|
-
doubleTapAtCoordinate: (x, y) => Effect.tryPromise({
|
|
296
|
-
try: () => page.mouse.dblclick(x, y),
|
|
297
|
-
catch: (e) => new DriverError({ message: `Failed to double tap at (${x}, ${y}): ${e}` })
|
|
298
|
-
}),
|
|
299
|
-
longPressAtCoordinate: (x, y, duration) => Effect.tryPromise({
|
|
300
|
-
try: async () => {
|
|
301
|
-
await page.mouse.move(x, y);
|
|
302
|
-
await page.mouse.down();
|
|
303
|
-
await new Promise((r) => setTimeout(r, duration));
|
|
304
|
-
await page.mouse.up();
|
|
305
|
-
},
|
|
306
|
-
catch: (e) => new DriverError({ message: `Failed to long press at (${x}, ${y}): ${e}` })
|
|
307
|
-
}),
|
|
308
|
-
swipe: (startX, startY, endX, endY, duration) => Effect.tryPromise({
|
|
309
|
-
try: async () => {
|
|
310
|
-
await page.mouse.move(startX, startY);
|
|
311
|
-
await page.mouse.down();
|
|
312
|
-
const steps = Math.max(Math.round(duration / 16), 5);
|
|
313
|
-
for (let i = 1; i <= steps; i++) {
|
|
314
|
-
const t = i / steps;
|
|
315
|
-
await page.mouse.move(
|
|
316
|
-
startX + (endX - startX) * t,
|
|
317
|
-
startY + (endY - startY) * t
|
|
318
|
-
);
|
|
319
|
-
}
|
|
320
|
-
await page.mouse.up();
|
|
321
|
-
},
|
|
322
|
-
catch: (e) => new DriverError({ message: `Failed to swipe: ${e}` })
|
|
323
|
-
}),
|
|
324
|
-
inputText: (text) => Effect.tryPromise({
|
|
325
|
-
try: () => page.keyboard.type(text),
|
|
326
|
-
catch: (e) => new DriverError({ message: `Failed to input text: ${e}` })
|
|
327
|
-
}),
|
|
328
|
-
pressKey: (key) => Effect.tryPromise({
|
|
329
|
-
try: () => page.keyboard.press(key),
|
|
330
|
-
catch: (e) => new DriverError({ message: `Failed to press key ${key}: ${e}` })
|
|
331
|
-
}),
|
|
332
|
-
hideKeyboard: () => Effect.void,
|
|
333
|
-
takeScreenshot: () => Effect.tryPromise({
|
|
334
|
-
try: async () => {
|
|
335
|
-
const buffer = await page.screenshot();
|
|
336
|
-
return new Uint8Array(buffer);
|
|
337
|
-
},
|
|
338
|
-
catch: (e) => new DriverError({ message: `Failed to take screenshot: ${e}` })
|
|
339
|
-
}),
|
|
340
|
-
getDeviceInfo: () => Effect.tryPromise({
|
|
341
|
-
try: async () => {
|
|
342
|
-
const viewport = page.viewportSize() ?? { width: 1280, height: 720 };
|
|
343
|
-
return {
|
|
344
|
-
platform: "web",
|
|
345
|
-
deviceId: "playwright-chromium",
|
|
346
|
-
name: "Chromium",
|
|
347
|
-
isEmulator: false,
|
|
348
|
-
screenWidth: viewport.width,
|
|
349
|
-
screenHeight: viewport.height,
|
|
350
|
-
driverType: "playwright"
|
|
351
|
-
};
|
|
352
|
-
},
|
|
353
|
-
catch: (e) => new DriverError({ message: `Failed to get device info: ${e}` })
|
|
354
|
-
}),
|
|
355
|
-
launchApp: (url, opts) => Effect.tryPromise({
|
|
356
|
-
try: () => page.goto(opts?.deepLink || url || config.baseUrl || "about:blank").then(() => {
|
|
357
|
-
}),
|
|
358
|
-
catch: (e) => new DriverError({ message: `Failed to navigate to ${url}: ${e}` })
|
|
359
|
-
}),
|
|
360
|
-
stopApp: (_id) => Effect.tryPromise({
|
|
361
|
-
try: () => page.goto("about:blank").then(() => {
|
|
362
|
-
}),
|
|
363
|
-
catch: (e) => new DriverError({ message: `Failed to stop app: ${e}` })
|
|
364
|
-
}),
|
|
365
|
-
killApp: (_id) => Effect.tryPromise({
|
|
366
|
-
try: () => page.close().then(() => {
|
|
367
|
-
}),
|
|
368
|
-
catch: (e) => new DriverError({ message: `Failed to kill app: ${e}` })
|
|
369
|
-
}),
|
|
370
|
-
clearAppState: (_id) => Effect.tryPromise({
|
|
371
|
-
try: async () => {
|
|
372
|
-
await page.context().clearCookies();
|
|
373
|
-
await page.evaluate(`localStorage.clear(); sessionStorage.clear();`);
|
|
374
|
-
},
|
|
375
|
-
catch: (e) => new DriverError({ message: `Failed to clear app state: ${e}` })
|
|
376
|
-
}),
|
|
377
|
-
openLink: (url) => Effect.tryPromise({
|
|
378
|
-
try: () => page.goto(url).then(() => {
|
|
379
|
-
}),
|
|
380
|
-
catch: (e) => new DriverError({ message: `Failed to open link ${url}: ${e}` })
|
|
381
|
-
}),
|
|
382
|
-
back: () => Effect.tryPromise({
|
|
383
|
-
try: () => page.goBack().then(() => {
|
|
384
|
-
}),
|
|
385
|
-
catch: (e) => new DriverError({ message: `Failed to go back: ${e}` })
|
|
386
|
-
})
|
|
387
|
-
};
|
|
388
|
-
return service;
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
// src/drivers/uiautomator2/pagesource.ts
|
|
393
|
-
function parseBounds(s) {
|
|
394
|
-
const normalized = s.replace("][", ",").replace(/[[\]]/g, "");
|
|
395
|
-
const parts = normalized.split(",");
|
|
396
|
-
if (parts.length !== 4) {
|
|
397
|
-
return { x: 0, y: 0, width: 0, height: 0 };
|
|
398
|
-
}
|
|
399
|
-
const x1 = parseInt(parts[0], 10);
|
|
400
|
-
const y1 = parseInt(parts[1], 10);
|
|
401
|
-
const x2 = parseInt(parts[2], 10);
|
|
402
|
-
const y2 = parseInt(parts[3], 10);
|
|
403
|
-
return { x: x1, y: y1, width: x2 - x1, height: y2 - y1 };
|
|
404
|
-
}
|
|
405
|
-
function stripResourceIdPrefix(resourceId) {
|
|
406
|
-
const colonSlash = resourceId.indexOf(":id/");
|
|
407
|
-
if (colonSlash !== -1) {
|
|
408
|
-
return resourceId.slice(colonSlash + 4);
|
|
409
|
-
}
|
|
410
|
-
return resourceId;
|
|
411
|
-
}
|
|
412
|
-
function parseAttributes(tagContent) {
|
|
413
|
-
const attrs = {};
|
|
414
|
-
const re = /([\w-]+)="([^"]*)"/g;
|
|
415
|
-
let m;
|
|
416
|
-
while ((m = re.exec(tagContent)) !== null) {
|
|
417
|
-
attrs[m[1]] = m[2];
|
|
418
|
-
}
|
|
419
|
-
return attrs;
|
|
420
|
-
}
|
|
421
|
-
function tokenizeXML(xml) {
|
|
422
|
-
let pos = 0;
|
|
423
|
-
function skipWhitespace() {
|
|
424
|
-
while (pos < xml.length && /\s/.test(xml[pos])) pos++;
|
|
425
|
-
}
|
|
426
|
-
function parseNode() {
|
|
427
|
-
skipWhitespace();
|
|
428
|
-
if (pos >= xml.length) return null;
|
|
429
|
-
if (xml[pos] !== "<") return null;
|
|
430
|
-
pos++;
|
|
431
|
-
if (xml[pos] === "?" || xml[pos] === "!") {
|
|
432
|
-
const close = xml.indexOf(">", pos);
|
|
433
|
-
if (close === -1) return null;
|
|
434
|
-
pos = close + 1;
|
|
435
|
-
return null;
|
|
436
|
-
}
|
|
437
|
-
if (xml[pos] === "/") {
|
|
438
|
-
const close = xml.indexOf(">", pos);
|
|
439
|
-
if (close === -1) return null;
|
|
440
|
-
pos = close + 1;
|
|
441
|
-
return null;
|
|
442
|
-
}
|
|
443
|
-
const tagStart = pos;
|
|
444
|
-
while (pos < xml.length && !/[\s/>]/.test(xml[pos])) pos++;
|
|
445
|
-
const tagName = xml.slice(tagStart, pos);
|
|
446
|
-
const tagBodyStart = pos;
|
|
447
|
-
let selfClosing = false;
|
|
448
|
-
let inQuote = false;
|
|
449
|
-
let quoteChar = "";
|
|
450
|
-
while (pos < xml.length) {
|
|
451
|
-
const ch = xml[pos];
|
|
452
|
-
if (inQuote) {
|
|
453
|
-
if (ch === quoteChar) inQuote = false;
|
|
454
|
-
} else {
|
|
455
|
-
if (ch === '"' || ch === "'") {
|
|
456
|
-
inQuote = true;
|
|
457
|
-
quoteChar = ch;
|
|
458
|
-
} else if (ch === "/" && xml[pos + 1] === ">") {
|
|
459
|
-
selfClosing = true;
|
|
460
|
-
pos += 2;
|
|
461
|
-
break;
|
|
462
|
-
} else if (ch === ">") {
|
|
463
|
-
pos++;
|
|
464
|
-
break;
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
pos++;
|
|
468
|
-
}
|
|
469
|
-
const tagBody = xml.slice(tagBodyStart, selfClosing ? pos - 2 : pos - 1);
|
|
470
|
-
const attrs = parseAttributes(tagBody);
|
|
471
|
-
if (!attrs["class"]) {
|
|
472
|
-
attrs["class"] = tagName;
|
|
473
|
-
}
|
|
474
|
-
const node = { attrs, children: [] };
|
|
475
|
-
if (!selfClosing) {
|
|
476
|
-
while (pos < xml.length) {
|
|
477
|
-
skipWhitespace();
|
|
478
|
-
if (pos >= xml.length) break;
|
|
479
|
-
if (xml[pos] === "<" && xml[pos + 1] === "/") {
|
|
480
|
-
const close = xml.indexOf(">", pos);
|
|
481
|
-
if (close !== -1) pos = close + 1;
|
|
482
|
-
break;
|
|
483
|
-
}
|
|
484
|
-
const child = parseNode();
|
|
485
|
-
if (child !== null) {
|
|
486
|
-
node.children.push(child);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
return node;
|
|
491
|
-
}
|
|
492
|
-
const roots = [];
|
|
493
|
-
while (pos < xml.length) {
|
|
494
|
-
skipWhitespace();
|
|
495
|
-
if (pos >= xml.length) break;
|
|
496
|
-
if (xml[pos] !== "<") {
|
|
497
|
-
pos++;
|
|
498
|
-
continue;
|
|
499
|
-
}
|
|
500
|
-
const peek = pos + 1;
|
|
501
|
-
let nameEnd = peek;
|
|
502
|
-
while (nameEnd < xml.length && !/[\s/>]/.test(xml[nameEnd])) nameEnd++;
|
|
503
|
-
const peekName = xml.slice(peek, nameEnd);
|
|
504
|
-
if (peekName === "hierarchy") {
|
|
505
|
-
const close = xml.indexOf(">", pos);
|
|
506
|
-
if (close === -1) break;
|
|
507
|
-
pos = close + 1;
|
|
508
|
-
continue;
|
|
509
|
-
}
|
|
510
|
-
if (peekName === "/hierarchy") {
|
|
511
|
-
const close = xml.indexOf(">", pos);
|
|
512
|
-
if (close !== -1) pos = close + 1;
|
|
513
|
-
break;
|
|
514
|
-
}
|
|
515
|
-
if (peekName.startsWith("?") || peekName.startsWith("!")) {
|
|
516
|
-
const close = xml.indexOf(">", pos);
|
|
517
|
-
if (close !== -1) pos = close + 1;
|
|
518
|
-
continue;
|
|
519
|
-
}
|
|
520
|
-
const node = parseNode();
|
|
521
|
-
if (node !== null) {
|
|
522
|
-
roots.push(node);
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
return roots;
|
|
526
|
-
}
|
|
527
|
-
function rawNodeToElement2(node) {
|
|
528
|
-
const a = node.attrs;
|
|
529
|
-
const rawId = a["resource-id"] ?? "";
|
|
530
|
-
const id = rawId ? stripResourceIdPrefix(rawId) : void 0;
|
|
531
|
-
const text = a["text"] || void 0;
|
|
532
|
-
const accessibilityLabel = a["content-desc"] || void 0;
|
|
533
|
-
const elementType = a["class"] || void 0;
|
|
534
|
-
const boundsStr = a["bounds"] ?? "[0,0][0,0]";
|
|
535
|
-
const bounds = parseBounds(boundsStr);
|
|
536
|
-
const enabled = a["enabled"] !== void 0 ? a["enabled"] === "true" : void 0;
|
|
537
|
-
const focused = a["focused"] !== void 0 ? a["focused"] === "true" : void 0;
|
|
538
|
-
const clickable = a["clickable"] !== void 0 ? a["clickable"] === "true" : void 0;
|
|
539
|
-
const children = node.children.length > 0 ? node.children.map(rawNodeToElement2) : void 0;
|
|
540
|
-
return {
|
|
541
|
-
...id !== void 0 ? { id } : {},
|
|
542
|
-
...text !== void 0 ? { text } : {},
|
|
543
|
-
...accessibilityLabel !== void 0 ? { accessibilityLabel } : {},
|
|
544
|
-
...elementType !== void 0 ? { elementType } : {},
|
|
545
|
-
bounds,
|
|
546
|
-
...enabled !== void 0 ? { enabled } : {},
|
|
547
|
-
...focused !== void 0 ? { focused } : {},
|
|
548
|
-
...clickable !== void 0 ? { clickable } : {},
|
|
549
|
-
...children !== void 0 ? { children } : {}
|
|
550
|
-
};
|
|
551
|
-
}
|
|
552
|
-
function parseAndroidHierarchy(xml) {
|
|
553
|
-
const roots = tokenizeXML(xml);
|
|
554
|
-
if (roots.length === 0) {
|
|
555
|
-
throw new Error("parseAndroidHierarchy: no root elements found in hierarchy");
|
|
556
|
-
}
|
|
557
|
-
if (roots.length === 1) {
|
|
558
|
-
return rawNodeToElement2(roots[0]);
|
|
559
|
-
}
|
|
560
|
-
return {
|
|
561
|
-
elementType: "hierarchy",
|
|
562
|
-
bounds: { x: 0, y: 0, width: 0, height: 0 },
|
|
563
|
-
children: roots.map(rawNodeToElement2)
|
|
564
|
-
};
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
// src/drivers/uiautomator2/driver.ts
|
|
568
|
-
import { Effect as Effect2 } from "effect";
|
|
569
|
-
|
|
570
|
-
// src/drivers/uiautomator2/client.ts
|
|
571
|
-
var UiAutomator2Client = class {
|
|
572
|
-
baseUrl;
|
|
573
|
-
sessionId = null;
|
|
574
|
-
constructor(host, port) {
|
|
575
|
-
this.baseUrl = `http://${host}:${port}`;
|
|
576
|
-
}
|
|
577
|
-
// ---------------------------------------------------------------------------
|
|
578
|
-
// HTTP helpers
|
|
579
|
-
// ---------------------------------------------------------------------------
|
|
580
|
-
async request(method, path, body) {
|
|
581
|
-
const url = `${this.baseUrl}${path}`;
|
|
582
|
-
const init = {
|
|
583
|
-
method,
|
|
584
|
-
headers: { "Content-Type": "application/json" }
|
|
585
|
-
};
|
|
586
|
-
if (body !== void 0) {
|
|
587
|
-
init.body = JSON.stringify(body);
|
|
588
|
-
}
|
|
589
|
-
const res = await fetch(url, init);
|
|
590
|
-
const text = await res.text();
|
|
591
|
-
let parsed;
|
|
592
|
-
try {
|
|
593
|
-
parsed = JSON.parse(text);
|
|
594
|
-
} catch {
|
|
595
|
-
throw new Error(`UiAutomator2: failed to parse response: ${text}`);
|
|
596
|
-
}
|
|
597
|
-
if (!res.ok) {
|
|
598
|
-
const val = parsed.value;
|
|
599
|
-
const msg = val?.message ?? val?.error ?? text;
|
|
600
|
-
throw new Error(`UiAutomator2 ${method} ${path} \u2192 ${res.status}: ${msg}`);
|
|
601
|
-
}
|
|
602
|
-
if (parsed.value !== null && typeof parsed.value === "object" && "error" in parsed.value) {
|
|
603
|
-
const val = parsed.value;
|
|
604
|
-
throw new Error(
|
|
605
|
-
`UiAutomator2 error: ${val.message ?? val.error}`
|
|
606
|
-
);
|
|
607
|
-
}
|
|
608
|
-
return parsed.value;
|
|
609
|
-
}
|
|
610
|
-
sessionPath(path) {
|
|
611
|
-
if (this.sessionId === null) {
|
|
612
|
-
throw new Error("UiAutomator2: no active session");
|
|
613
|
-
}
|
|
614
|
-
return `/session/${this.sessionId}${path}`;
|
|
615
|
-
}
|
|
616
|
-
// ---------------------------------------------------------------------------
|
|
617
|
-
// Session management
|
|
618
|
-
// ---------------------------------------------------------------------------
|
|
619
|
-
async createSession(appPackage) {
|
|
620
|
-
const capabilities = {
|
|
621
|
-
platformName: "Android"
|
|
622
|
-
};
|
|
623
|
-
if (appPackage) {
|
|
624
|
-
capabilities["appium:appPackage"] = appPackage;
|
|
625
|
-
}
|
|
626
|
-
const body = { capabilities };
|
|
627
|
-
const res = await fetch(`${this.baseUrl}/session`, {
|
|
628
|
-
method: "POST",
|
|
629
|
-
headers: { "Content-Type": "application/json" },
|
|
630
|
-
body: JSON.stringify(body)
|
|
631
|
-
});
|
|
632
|
-
const text = await res.text();
|
|
633
|
-
let parsed;
|
|
634
|
-
try {
|
|
635
|
-
parsed = JSON.parse(text);
|
|
636
|
-
} catch {
|
|
637
|
-
throw new Error(`UiAutomator2: failed to parse session response: ${text}`);
|
|
638
|
-
}
|
|
639
|
-
if (!res.ok) {
|
|
640
|
-
const val = parsed["value"];
|
|
641
|
-
throw new Error(
|
|
642
|
-
`UiAutomator2: failed to create session: ${val?.message ?? text}`
|
|
643
|
-
);
|
|
644
|
-
}
|
|
645
|
-
let sessionId = parsed["sessionId"] ?? parsed["value"]?.["sessionId"];
|
|
646
|
-
if (!sessionId) {
|
|
647
|
-
throw new Error("UiAutomator2: no sessionId in session response");
|
|
648
|
-
}
|
|
649
|
-
this.sessionId = sessionId;
|
|
650
|
-
return sessionId;
|
|
651
|
-
}
|
|
652
|
-
async deleteSession() {
|
|
653
|
-
if (this.sessionId === null) return;
|
|
654
|
-
const path = `/session/${this.sessionId}`;
|
|
655
|
-
this.sessionId = null;
|
|
656
|
-
await fetch(`${this.baseUrl}${path}`, { method: "DELETE" });
|
|
657
|
-
}
|
|
658
|
-
// ---------------------------------------------------------------------------
|
|
659
|
-
// Screen queries
|
|
660
|
-
// ---------------------------------------------------------------------------
|
|
661
|
-
async getSource() {
|
|
662
|
-
const value = await this.request("GET", this.sessionPath("/source"));
|
|
663
|
-
return value;
|
|
664
|
-
}
|
|
665
|
-
async getScreenshot() {
|
|
666
|
-
const value = await this.request("GET", this.sessionPath("/screenshot"));
|
|
667
|
-
return value;
|
|
668
|
-
}
|
|
669
|
-
async getWindowSize() {
|
|
670
|
-
const value = await this.request(
|
|
671
|
-
"GET",
|
|
672
|
-
this.sessionPath("/window/size")
|
|
673
|
-
);
|
|
674
|
-
return value;
|
|
675
|
-
}
|
|
676
|
-
// ---------------------------------------------------------------------------
|
|
677
|
-
// Gestures — Appium gesture extensions
|
|
678
|
-
// These match the endpoints used by maestro-runner's uiautomator2 client.
|
|
679
|
-
// ---------------------------------------------------------------------------
|
|
680
|
-
async performTap(x, y) {
|
|
681
|
-
await this.request("POST", this.sessionPath("/appium/gestures/click"), {
|
|
682
|
-
offset: { x, y }
|
|
683
|
-
});
|
|
684
|
-
}
|
|
685
|
-
async performDoubleTap(x, y) {
|
|
686
|
-
await this.request("POST", this.sessionPath("/appium/gestures/double_click"), {
|
|
687
|
-
offset: { x, y }
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
async performLongPress(x, y, duration) {
|
|
691
|
-
await this.request("POST", this.sessionPath("/appium/gestures/long_click"), {
|
|
692
|
-
offset: { x, y },
|
|
693
|
-
duration
|
|
694
|
-
// milliseconds
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
/**
|
|
698
|
-
* Coordinate-based swipe from (startX, startY) to (endX, endY).
|
|
699
|
-
*
|
|
700
|
-
* The Appium swipe extension only accepts direction + percent; for arbitrary
|
|
701
|
-
* from/to coordinates we use the W3C Actions API instead.
|
|
702
|
-
*
|
|
703
|
-
* @param duration - gesture duration in milliseconds
|
|
704
|
-
*/
|
|
705
|
-
async performSwipe(startX, startY, endX, endY, duration) {
|
|
706
|
-
await this.request("POST", this.sessionPath("/actions"), {
|
|
707
|
-
actions: [
|
|
708
|
-
{
|
|
709
|
-
type: "pointer",
|
|
710
|
-
id: "finger1",
|
|
711
|
-
parameters: { pointerType: "touch" },
|
|
712
|
-
actions: [
|
|
713
|
-
{ type: "pointerMove", duration: 0, x: startX, y: startY },
|
|
714
|
-
{ type: "pointerDown", button: 0 },
|
|
715
|
-
{ type: "pointerMove", duration, x: endX, y: endY },
|
|
716
|
-
{ type: "pointerUp", button: 0 }
|
|
717
|
-
]
|
|
718
|
-
}
|
|
719
|
-
]
|
|
720
|
-
});
|
|
721
|
-
}
|
|
722
|
-
// ---------------------------------------------------------------------------
|
|
723
|
-
// Text / keyboard
|
|
724
|
-
// ---------------------------------------------------------------------------
|
|
725
|
-
async sendKeys(text) {
|
|
726
|
-
const keyActions = [];
|
|
727
|
-
for (const ch of text) {
|
|
728
|
-
keyActions.push({ type: "keyDown", value: ch });
|
|
729
|
-
keyActions.push({ type: "keyUp", value: ch });
|
|
730
|
-
}
|
|
731
|
-
await this.request("POST", this.sessionPath("/actions"), {
|
|
732
|
-
actions: [
|
|
733
|
-
{
|
|
734
|
-
type: "key",
|
|
735
|
-
id: "keyboard",
|
|
736
|
-
actions: keyActions
|
|
737
|
-
}
|
|
738
|
-
]
|
|
739
|
-
});
|
|
740
|
-
}
|
|
741
|
-
async pressKeyCode(keyCode) {
|
|
742
|
-
await this.request("POST", this.sessionPath("/appium/device/press_keycode"), {
|
|
743
|
-
keycode: keyCode
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
async hideKeyboard() {
|
|
747
|
-
await this.request("POST", this.sessionPath("/appium/device/hide_keyboard"), void 0);
|
|
748
|
-
}
|
|
749
|
-
// ---------------------------------------------------------------------------
|
|
750
|
-
// App lifecycle
|
|
751
|
-
// ---------------------------------------------------------------------------
|
|
752
|
-
async activateApp(appPackage) {
|
|
753
|
-
await this.request("POST", this.sessionPath("/appium/device/activate_app"), {
|
|
754
|
-
appId: appPackage
|
|
755
|
-
});
|
|
756
|
-
}
|
|
757
|
-
async terminateApp(appPackage) {
|
|
758
|
-
await this.request("POST", this.sessionPath("/appium/device/terminate_app"), {
|
|
759
|
-
appId: appPackage
|
|
760
|
-
});
|
|
761
|
-
}
|
|
762
|
-
};
|
|
763
|
-
|
|
764
|
-
// src/drivers/uiautomator2/driver.ts
|
|
765
|
-
function createUiAutomator2Driver(host, port, serial, packageName) {
|
|
766
|
-
return Effect2.gen(function* () {
|
|
767
|
-
const client = new UiAutomator2Client(host, port);
|
|
768
|
-
yield* Effect2.tryPromise({
|
|
769
|
-
try: () => client.createSession(packageName),
|
|
770
|
-
catch: (e) => new DriverError({
|
|
771
|
-
message: `Failed to create UiAutomator2 session: ${e}`
|
|
772
|
-
})
|
|
773
|
-
});
|
|
774
|
-
const service = {
|
|
775
|
-
// -----------------------------------------------------------------------
|
|
776
|
-
// Hierarchy
|
|
777
|
-
// -----------------------------------------------------------------------
|
|
778
|
-
dumpHierarchy: () => Effect2.tryPromise({
|
|
779
|
-
try: () => client.getSource(),
|
|
780
|
-
catch: (e) => new DriverError({ message: `Failed to get page source: ${e}` })
|
|
781
|
-
}),
|
|
782
|
-
// -----------------------------------------------------------------------
|
|
783
|
-
// Coordinate-level actions
|
|
784
|
-
// -----------------------------------------------------------------------
|
|
785
|
-
tapAtCoordinate: (x, y) => Effect2.tryPromise({
|
|
786
|
-
try: () => client.performTap(x, y),
|
|
787
|
-
catch: (e) => new DriverError({ message: `Tap failed: ${e}` })
|
|
788
|
-
}),
|
|
789
|
-
doubleTapAtCoordinate: (x, y) => Effect2.tryPromise({
|
|
790
|
-
try: () => client.performDoubleTap(x, y),
|
|
791
|
-
catch: (e) => new DriverError({ message: `Double tap failed: ${e}` })
|
|
792
|
-
}),
|
|
793
|
-
longPressAtCoordinate: (x, y, duration) => Effect2.tryPromise({
|
|
794
|
-
try: () => client.performLongPress(x, y, duration),
|
|
795
|
-
catch: (e) => new DriverError({ message: `Long press failed: ${e}` })
|
|
796
|
-
}),
|
|
797
|
-
swipe: (sx, sy, ex, ey, dur) => Effect2.tryPromise({
|
|
798
|
-
try: () => client.performSwipe(sx, sy, ex, ey, dur),
|
|
799
|
-
catch: (e) => new DriverError({ message: `Swipe failed: ${e}` })
|
|
800
|
-
}),
|
|
801
|
-
// -----------------------------------------------------------------------
|
|
802
|
-
// Text input
|
|
803
|
-
// -----------------------------------------------------------------------
|
|
804
|
-
inputText: (text) => Effect2.tryPromise({
|
|
805
|
-
try: () => client.sendKeys(text),
|
|
806
|
-
catch: (e) => new DriverError({ message: `Input text failed: ${e}` })
|
|
807
|
-
}),
|
|
808
|
-
pressKey: (key) => Effect2.tryPromise({
|
|
809
|
-
try: () => client.pressKeyCode(parseInt(key, 10) || 0),
|
|
810
|
-
catch: (e) => new DriverError({ message: `Press key failed: ${e}` })
|
|
811
|
-
}),
|
|
812
|
-
hideKeyboard: () => Effect2.tryPromise({
|
|
813
|
-
try: () => client.hideKeyboard(),
|
|
814
|
-
catch: (e) => new DriverError({ message: `Hide keyboard failed: ${e}` })
|
|
815
|
-
}),
|
|
816
|
-
// -----------------------------------------------------------------------
|
|
817
|
-
// Queries
|
|
818
|
-
// -----------------------------------------------------------------------
|
|
819
|
-
takeScreenshot: () => Effect2.tryPromise({
|
|
820
|
-
try: async () => {
|
|
821
|
-
const base64 = await client.getScreenshot();
|
|
822
|
-
return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
|
823
|
-
},
|
|
824
|
-
catch: (e) => new DriverError({ message: `Screenshot failed: ${e}` })
|
|
825
|
-
}),
|
|
826
|
-
getDeviceInfo: () => Effect2.tryPromise({
|
|
827
|
-
try: async () => {
|
|
828
|
-
const size = await client.getWindowSize();
|
|
829
|
-
return {
|
|
830
|
-
platform: "android",
|
|
831
|
-
deviceId: `${host}:${port}`,
|
|
832
|
-
name: "Android Device",
|
|
833
|
-
isEmulator: host === "localhost" || host === "127.0.0.1",
|
|
834
|
-
screenWidth: size.width,
|
|
835
|
-
screenHeight: size.height,
|
|
836
|
-
driverType: "uiautomator2"
|
|
837
|
-
};
|
|
838
|
-
},
|
|
839
|
-
catch: (e) => new DriverError({ message: `Get device info failed: ${e}` })
|
|
840
|
-
}),
|
|
841
|
-
// -----------------------------------------------------------------------
|
|
842
|
-
// App lifecycle
|
|
843
|
-
// -----------------------------------------------------------------------
|
|
844
|
-
launchApp: (bundleId, _opts) => Effect2.tryPromise({
|
|
845
|
-
try: async () => {
|
|
846
|
-
adbForceStop(serial, bundleId);
|
|
847
|
-
if (_opts?.deepLink) {
|
|
848
|
-
adbOpenLink(serial, _opts.deepLink, bundleId);
|
|
849
|
-
} else {
|
|
850
|
-
adbLaunchApp(serial, bundleId);
|
|
851
|
-
}
|
|
852
|
-
await new Promise((resolve3) => setTimeout(resolve3, 1e3));
|
|
853
|
-
},
|
|
854
|
-
catch: (e) => new DriverError({ message: `Launch app failed: ${e}` })
|
|
855
|
-
}),
|
|
856
|
-
stopApp: (bundleId) => Effect2.tryPromise({
|
|
857
|
-
try: async () => {
|
|
858
|
-
adbForceStop(serial, bundleId);
|
|
859
|
-
},
|
|
860
|
-
catch: (e) => new DriverError({ message: `Stop app failed: ${e}` })
|
|
861
|
-
}),
|
|
862
|
-
killApp: (bundleId) => Effect2.tryPromise({
|
|
863
|
-
try: async () => {
|
|
864
|
-
adbForceStop(serial, bundleId);
|
|
865
|
-
},
|
|
866
|
-
catch: (e) => new DriverError({ message: `Kill app failed: ${e}` })
|
|
867
|
-
}),
|
|
868
|
-
clearAppState: (bundleId) => Effect2.tryPromise({
|
|
869
|
-
try: async () => {
|
|
870
|
-
adbForceStop(serial, bundleId);
|
|
871
|
-
adbClearApp(serial, bundleId);
|
|
872
|
-
},
|
|
873
|
-
catch: (e) => new DriverError({ message: `Clear app state failed: ${e}` })
|
|
874
|
-
}),
|
|
875
|
-
// -----------------------------------------------------------------------
|
|
876
|
-
// Navigation
|
|
877
|
-
// -----------------------------------------------------------------------
|
|
878
|
-
openLink: (url) => Effect2.tryPromise({
|
|
879
|
-
try: async () => {
|
|
880
|
-
adbOpenLink(serial, url, packageName);
|
|
881
|
-
await new Promise((resolve3) => setTimeout(resolve3, 1e3));
|
|
882
|
-
},
|
|
883
|
-
catch: (e) => new DriverError({ message: `Open link failed: ${e}` })
|
|
884
|
-
}),
|
|
885
|
-
back: () => Effect2.tryPromise({
|
|
886
|
-
// KEYCODE_BACK = 4
|
|
887
|
-
try: () => client.pressKeyCode(4),
|
|
888
|
-
catch: (e) => new DriverError({ message: `Back failed: ${e}` })
|
|
889
|
-
})
|
|
890
|
-
};
|
|
891
|
-
return service;
|
|
892
|
-
});
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
// src/drivers/wda/pagesource.ts
|
|
896
|
-
var CLICKABLE_TYPES = /* @__PURE__ */ new Set([
|
|
897
|
-
"XCUIElementTypeButton",
|
|
898
|
-
"XCUIElementTypeLink",
|
|
899
|
-
"XCUIElementTypeTextField",
|
|
900
|
-
"XCUIElementTypeSecureTextField",
|
|
901
|
-
"XCUIElementTypeSearchField",
|
|
902
|
-
"XCUIElementTypeSwitch",
|
|
903
|
-
"XCUIElementTypeSlider",
|
|
904
|
-
"XCUIElementTypeStepper",
|
|
905
|
-
"XCUIElementTypeSegmentedControl",
|
|
906
|
-
"XCUIElementTypeCell",
|
|
907
|
-
"XCUIElementTypeTab",
|
|
908
|
-
"XCUIElementTypeTabBar",
|
|
909
|
-
"XCUIElementTypeMenu",
|
|
910
|
-
"XCUIElementTypeMenuItem",
|
|
911
|
-
"XCUIElementTypePickerWheel",
|
|
912
|
-
"XCUIElementTypeDatePicker",
|
|
913
|
-
"XCUIElementTypeToggle",
|
|
914
|
-
"XCUIElementTypePageIndicator"
|
|
915
|
-
]);
|
|
916
|
-
function parseAttributes2(tagContent) {
|
|
917
|
-
const attrs = {};
|
|
918
|
-
const re = /([\w-]+)="([^"]*)"/g;
|
|
919
|
-
let m;
|
|
920
|
-
while ((m = re.exec(tagContent)) !== null) {
|
|
921
|
-
attrs[m[1]] = m[2];
|
|
922
|
-
}
|
|
923
|
-
return attrs;
|
|
924
|
-
}
|
|
925
|
-
function tokenizeXML2(xml) {
|
|
926
|
-
let pos = 0;
|
|
927
|
-
function skipWhitespace() {
|
|
928
|
-
while (pos < xml.length && /\s/.test(xml[pos])) pos++;
|
|
929
|
-
}
|
|
930
|
-
function parseNode() {
|
|
931
|
-
skipWhitespace();
|
|
932
|
-
if (pos >= xml.length) return null;
|
|
933
|
-
if (xml[pos] !== "<") return null;
|
|
934
|
-
pos++;
|
|
935
|
-
if (xml[pos] === "?" || xml[pos] === "!") {
|
|
936
|
-
const close = xml.indexOf(">", pos);
|
|
937
|
-
if (close === -1) return null;
|
|
938
|
-
pos = close + 1;
|
|
939
|
-
return null;
|
|
940
|
-
}
|
|
941
|
-
if (xml[pos] === "/") {
|
|
942
|
-
const close = xml.indexOf(">", pos);
|
|
943
|
-
if (close !== -1) pos = close + 1;
|
|
944
|
-
return null;
|
|
945
|
-
}
|
|
946
|
-
const tagStart = pos;
|
|
947
|
-
while (pos < xml.length && !/[\s/>]/.test(xml[pos])) pos++;
|
|
948
|
-
const tagName = xml.slice(tagStart, pos);
|
|
949
|
-
const tagBodyStart = pos;
|
|
950
|
-
let selfClosing = false;
|
|
951
|
-
let inQuote = false;
|
|
952
|
-
let quoteChar = "";
|
|
953
|
-
while (pos < xml.length) {
|
|
954
|
-
const ch = xml[pos];
|
|
955
|
-
if (inQuote) {
|
|
956
|
-
if (ch === quoteChar) inQuote = false;
|
|
957
|
-
} else {
|
|
958
|
-
if (ch === '"' || ch === "'") {
|
|
959
|
-
inQuote = true;
|
|
960
|
-
quoteChar = ch;
|
|
961
|
-
} else if (ch === "/" && xml[pos + 1] === ">") {
|
|
962
|
-
selfClosing = true;
|
|
963
|
-
pos += 2;
|
|
964
|
-
break;
|
|
965
|
-
} else if (ch === ">") {
|
|
966
|
-
pos++;
|
|
967
|
-
break;
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
pos++;
|
|
971
|
-
}
|
|
972
|
-
const tagBody = xml.slice(tagBodyStart, selfClosing ? pos - 2 : pos - 1);
|
|
973
|
-
const attrs = parseAttributes2(tagBody);
|
|
974
|
-
const resolvedType = attrs["type"] ?? tagName;
|
|
975
|
-
const node = { tagName: resolvedType, attrs, children: [] };
|
|
976
|
-
if (!selfClosing) {
|
|
977
|
-
while (pos < xml.length) {
|
|
978
|
-
skipWhitespace();
|
|
979
|
-
if (pos >= xml.length) break;
|
|
980
|
-
if (xml[pos] === "<" && xml[pos + 1] === "/") {
|
|
981
|
-
const close = xml.indexOf(">", pos);
|
|
982
|
-
if (close !== -1) pos = close + 1;
|
|
983
|
-
break;
|
|
984
|
-
}
|
|
985
|
-
const child = parseNode();
|
|
986
|
-
if (child !== null) {
|
|
987
|
-
node.children.push(child);
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
}
|
|
991
|
-
return node;
|
|
992
|
-
}
|
|
993
|
-
const roots = [];
|
|
994
|
-
while (pos < xml.length) {
|
|
995
|
-
skipWhitespace();
|
|
996
|
-
if (pos >= xml.length) break;
|
|
997
|
-
if (xml[pos] !== "<") {
|
|
998
|
-
pos++;
|
|
999
|
-
continue;
|
|
1000
|
-
}
|
|
1001
|
-
const peek = pos + 1;
|
|
1002
|
-
let nameEnd = peek;
|
|
1003
|
-
while (nameEnd < xml.length && !/[\s/>]/.test(xml[nameEnd])) nameEnd++;
|
|
1004
|
-
const peekName = xml.slice(peek, nameEnd);
|
|
1005
|
-
if (peekName === "AppiumAUT") {
|
|
1006
|
-
const close = xml.indexOf(">", pos);
|
|
1007
|
-
if (close === -1) break;
|
|
1008
|
-
pos = close + 1;
|
|
1009
|
-
continue;
|
|
1010
|
-
}
|
|
1011
|
-
if (peekName === "/AppiumAUT") {
|
|
1012
|
-
const close = xml.indexOf(">", pos);
|
|
1013
|
-
if (close !== -1) pos = close + 1;
|
|
1014
|
-
break;
|
|
1015
|
-
}
|
|
1016
|
-
if (peekName.startsWith("?") || peekName.startsWith("!")) {
|
|
1017
|
-
const close = xml.indexOf(">", pos);
|
|
1018
|
-
if (close !== -1) pos = close + 1;
|
|
1019
|
-
continue;
|
|
1020
|
-
}
|
|
1021
|
-
const node = parseNode();
|
|
1022
|
-
if (node !== null) {
|
|
1023
|
-
roots.push(node);
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
return roots;
|
|
1027
|
-
}
|
|
1028
|
-
function rawNodeToElement3(node) {
|
|
1029
|
-
const a = node.attrs;
|
|
1030
|
-
const elementType = node.tagName || void 0;
|
|
1031
|
-
const id = a["name"] || void 0;
|
|
1032
|
-
const accessibilityLabel = a["label"] || void 0;
|
|
1033
|
-
const text = a["value"] || void 0;
|
|
1034
|
-
const x = a["x"] !== void 0 ? parseInt(a["x"], 10) : 0;
|
|
1035
|
-
const y = a["y"] !== void 0 ? parseInt(a["y"], 10) : 0;
|
|
1036
|
-
const width = a["width"] !== void 0 ? parseInt(a["width"], 10) : 0;
|
|
1037
|
-
const height = a["height"] !== void 0 ? parseInt(a["height"], 10) : 0;
|
|
1038
|
-
const bounds = { x, y, width, height };
|
|
1039
|
-
const enabled = a["enabled"] !== void 0 ? a["enabled"] === "true" : void 0;
|
|
1040
|
-
const visible = a["visible"] !== void 0 ? a["visible"] === "true" : void 0;
|
|
1041
|
-
const focused = a["focused"] !== void 0 ? a["focused"] === "true" : void 0;
|
|
1042
|
-
const clickable = elementType !== void 0 ? CLICKABLE_TYPES.has(elementType) : void 0;
|
|
1043
|
-
const children = node.children.length > 0 ? node.children.map(rawNodeToElement3) : void 0;
|
|
1044
|
-
return {
|
|
1045
|
-
...id !== void 0 ? { id } : {},
|
|
1046
|
-
...text !== void 0 ? { text } : {},
|
|
1047
|
-
...accessibilityLabel !== void 0 ? { accessibilityLabel } : {},
|
|
1048
|
-
...elementType !== void 0 ? { elementType } : {},
|
|
1049
|
-
bounds,
|
|
1050
|
-
...enabled !== void 0 ? { enabled } : {},
|
|
1051
|
-
...visible !== void 0 ? { visible } : {},
|
|
1052
|
-
...focused !== void 0 ? { focused } : {},
|
|
1053
|
-
...clickable !== void 0 ? { clickable } : {},
|
|
1054
|
-
...children !== void 0 ? { children } : {}
|
|
1055
|
-
};
|
|
1056
|
-
}
|
|
1057
|
-
function parseIOSHierarchy(xml) {
|
|
1058
|
-
const roots = tokenizeXML2(xml);
|
|
1059
|
-
if (roots.length === 0) {
|
|
1060
|
-
throw new Error("parseIOSHierarchy: no elements found in page source");
|
|
1061
|
-
}
|
|
1062
|
-
if (roots.length === 1) {
|
|
1063
|
-
return rawNodeToElement3(roots[0]);
|
|
1064
|
-
}
|
|
1065
|
-
return {
|
|
1066
|
-
elementType: "AppiumAUT",
|
|
1067
|
-
bounds: { x: 0, y: 0, width: 0, height: 0 },
|
|
1068
|
-
children: roots.map(rawNodeToElement3)
|
|
1069
|
-
};
|
|
1070
|
-
}
|
|
1071
|
-
|
|
1072
|
-
// src/drivers/wda/driver.ts
|
|
1073
|
-
import { Effect as Effect3 } from "effect";
|
|
1074
|
-
|
|
1075
|
-
// src/drivers/wda/client.ts
|
|
1076
|
-
var WDAClient = class {
|
|
1077
|
-
baseUrl;
|
|
1078
|
-
sessionId = null;
|
|
1079
|
-
constructor(host, port) {
|
|
1080
|
-
this.baseUrl = `http://${host}:${port}`;
|
|
1081
|
-
}
|
|
1082
|
-
// ---------------------------------------------------------------------------
|
|
1083
|
-
// HTTP helpers
|
|
1084
|
-
// ---------------------------------------------------------------------------
|
|
1085
|
-
async request(method, path, body) {
|
|
1086
|
-
const url = `${this.baseUrl}${path}`;
|
|
1087
|
-
const init = {
|
|
1088
|
-
method,
|
|
1089
|
-
headers: { "Content-Type": "application/json" }
|
|
1090
|
-
};
|
|
1091
|
-
if (body !== void 0) {
|
|
1092
|
-
init.body = JSON.stringify(body);
|
|
1093
|
-
}
|
|
1094
|
-
const res = await fetch(url, init);
|
|
1095
|
-
const text = await res.text();
|
|
1096
|
-
let parsed;
|
|
1097
|
-
try {
|
|
1098
|
-
parsed = JSON.parse(text);
|
|
1099
|
-
} catch {
|
|
1100
|
-
throw new Error(`WDA: failed to parse response: ${text}`);
|
|
1101
|
-
}
|
|
1102
|
-
if (!res.ok) {
|
|
1103
|
-
const val = parsed.value;
|
|
1104
|
-
const msg = val?.message ?? val?.error ?? text;
|
|
1105
|
-
throw new Error(`WDA ${method} ${path} \u2192 ${res.status}: ${msg}`);
|
|
1106
|
-
}
|
|
1107
|
-
if (parsed.value !== null && typeof parsed.value === "object" && "error" in parsed.value) {
|
|
1108
|
-
const val = parsed.value;
|
|
1109
|
-
throw new Error(`WDA error: ${val.message ?? val.error}`);
|
|
1110
|
-
}
|
|
1111
|
-
return parsed.value;
|
|
1112
|
-
}
|
|
1113
|
-
sessionPath(path) {
|
|
1114
|
-
if (this.sessionId === null) {
|
|
1115
|
-
throw new Error("WDA: no active session");
|
|
1116
|
-
}
|
|
1117
|
-
return `/session/${this.sessionId}${path}`;
|
|
1118
|
-
}
|
|
1119
|
-
// ---------------------------------------------------------------------------
|
|
1120
|
-
// Session management
|
|
1121
|
-
// ---------------------------------------------------------------------------
|
|
1122
|
-
/**
|
|
1123
|
-
* Creates a new WDA session.
|
|
1124
|
-
*
|
|
1125
|
-
* @param bundleId - the app bundle ID to attach to (e.g. "com.example.app")
|
|
1126
|
-
* @param alertAction - optional auto-alert handling: "accept" | "dismiss"
|
|
1127
|
-
*/
|
|
1128
|
-
async createSession(bundleId, alertAction) {
|
|
1129
|
-
const alwaysMatch = {
|
|
1130
|
-
shouldWaitForQuiescence: false,
|
|
1131
|
-
waitForIdleTimeout: 0,
|
|
1132
|
-
shouldUseTestManagerForVisibilityDetection: false
|
|
1133
|
-
};
|
|
1134
|
-
if (bundleId) {
|
|
1135
|
-
alwaysMatch["bundleId"] = bundleId;
|
|
1136
|
-
}
|
|
1137
|
-
if (alertAction) {
|
|
1138
|
-
alwaysMatch["defaultAlertAction"] = alertAction;
|
|
1139
|
-
}
|
|
1140
|
-
const res = await fetch(`${this.baseUrl}/session`, {
|
|
1141
|
-
method: "POST",
|
|
1142
|
-
headers: { "Content-Type": "application/json" },
|
|
1143
|
-
body: JSON.stringify({
|
|
1144
|
-
capabilities: { alwaysMatch }
|
|
1145
|
-
})
|
|
1146
|
-
});
|
|
1147
|
-
const text = await res.text();
|
|
1148
|
-
let parsed;
|
|
1149
|
-
try {
|
|
1150
|
-
parsed = JSON.parse(text);
|
|
1151
|
-
} catch {
|
|
1152
|
-
throw new Error(`WDA: failed to parse session response: ${text}`);
|
|
1153
|
-
}
|
|
1154
|
-
if (!res.ok) {
|
|
1155
|
-
const val = parsed["value"];
|
|
1156
|
-
throw new Error(`WDA: failed to create session: ${val?.message ?? text}`);
|
|
1157
|
-
}
|
|
1158
|
-
let sessionId = parsed["sessionId"] ?? parsed["value"]?.["sessionId"];
|
|
1159
|
-
if (!sessionId) {
|
|
1160
|
-
throw new Error("WDA: no sessionId in session response");
|
|
1161
|
-
}
|
|
1162
|
-
this.sessionId = sessionId;
|
|
1163
|
-
return sessionId;
|
|
1164
|
-
}
|
|
1165
|
-
async deleteSession() {
|
|
1166
|
-
if (this.sessionId === null) return;
|
|
1167
|
-
const path = `/session/${this.sessionId}`;
|
|
1168
|
-
this.sessionId = null;
|
|
1169
|
-
await fetch(`${this.baseUrl}${path}`, { method: "DELETE" });
|
|
1170
|
-
}
|
|
1171
|
-
hasSession() {
|
|
1172
|
-
return this.sessionId !== null;
|
|
1173
|
-
}
|
|
1174
|
-
sessionID() {
|
|
1175
|
-
return this.sessionId;
|
|
1176
|
-
}
|
|
1177
|
-
// ---------------------------------------------------------------------------
|
|
1178
|
-
// Settings
|
|
1179
|
-
// ---------------------------------------------------------------------------
|
|
1180
|
-
async updateSettings(settings) {
|
|
1181
|
-
await this.request("POST", this.sessionPath("/appium/settings"), {
|
|
1182
|
-
settings
|
|
1183
|
-
});
|
|
1184
|
-
}
|
|
1185
|
-
async disableQuiescence() {
|
|
1186
|
-
await this.updateSettings({
|
|
1187
|
-
shouldWaitForQuiescence: false,
|
|
1188
|
-
waitForIdleTimeout: 0
|
|
1189
|
-
});
|
|
1190
|
-
}
|
|
1191
|
-
// ---------------------------------------------------------------------------
|
|
1192
|
-
// Screen queries
|
|
1193
|
-
// ---------------------------------------------------------------------------
|
|
1194
|
-
/** Returns the UI hierarchy as XML. */
|
|
1195
|
-
async getSource() {
|
|
1196
|
-
const value = await this.request("GET", this.sessionPath("/source"));
|
|
1197
|
-
return value;
|
|
1198
|
-
}
|
|
1199
|
-
/**
|
|
1200
|
-
* Captures the screen and returns PNG bytes.
|
|
1201
|
-
*
|
|
1202
|
-
* The Go client calls GET /session/{id}/screenshot (session-prefixed) and
|
|
1203
|
-
* decodes the base64 value. We follow the same pattern.
|
|
1204
|
-
*/
|
|
1205
|
-
async getScreenshot() {
|
|
1206
|
-
const base64 = await this.request(
|
|
1207
|
-
"GET",
|
|
1208
|
-
this.sessionPath("/screenshot")
|
|
1209
|
-
);
|
|
1210
|
-
return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
|
1211
|
-
}
|
|
1212
|
-
/** Returns the screen dimensions in logical points. */
|
|
1213
|
-
async getWindowSize() {
|
|
1214
|
-
const value = await this.request(
|
|
1215
|
-
"GET",
|
|
1216
|
-
this.sessionPath("/window/size")
|
|
1217
|
-
);
|
|
1218
|
-
return value;
|
|
1219
|
-
}
|
|
1220
|
-
// ---------------------------------------------------------------------------
|
|
1221
|
-
// Touch actions — WDA-specific endpoints
|
|
1222
|
-
// ---------------------------------------------------------------------------
|
|
1223
|
-
async tap(x, y) {
|
|
1224
|
-
await this.request("POST", this.sessionPath("/wda/tap"), { x, y });
|
|
1225
|
-
}
|
|
1226
|
-
async doubleTap(x, y) {
|
|
1227
|
-
await this.request("POST", this.sessionPath("/wda/doubleTap"), { x, y });
|
|
1228
|
-
}
|
|
1229
|
-
/**
|
|
1230
|
-
* Long-presses at coordinates.
|
|
1231
|
-
*
|
|
1232
|
-
* @param durationSec - hold duration in **seconds** (WDA convention)
|
|
1233
|
-
*/
|
|
1234
|
-
async longPress(x, y, durationSec) {
|
|
1235
|
-
await this.request("POST", this.sessionPath("/wda/touchAndHold"), {
|
|
1236
|
-
x,
|
|
1237
|
-
y,
|
|
1238
|
-
duration: durationSec
|
|
1239
|
-
});
|
|
1240
|
-
}
|
|
1241
|
-
/**
|
|
1242
|
-
* Swipes from one point to another.
|
|
1243
|
-
*
|
|
1244
|
-
* @param durationSec - gesture duration in **seconds** (WDA convention)
|
|
1245
|
-
*/
|
|
1246
|
-
async swipe(fromX, fromY, toX, toY, durationSec) {
|
|
1247
|
-
await this.request("POST", this.sessionPath("/wda/dragfromtoforduration"), {
|
|
1248
|
-
fromX,
|
|
1249
|
-
fromY,
|
|
1250
|
-
toX,
|
|
1251
|
-
toY,
|
|
1252
|
-
duration: durationSec
|
|
1253
|
-
});
|
|
1254
|
-
}
|
|
1255
|
-
// ---------------------------------------------------------------------------
|
|
1256
|
-
// Text / keyboard
|
|
1257
|
-
// ---------------------------------------------------------------------------
|
|
1258
|
-
/**
|
|
1259
|
-
* Types text. WDA expects the value as an array of individual characters.
|
|
1260
|
-
*
|
|
1261
|
-
* @param frequency - typing speed in keys/sec (0 = WDA default ~60/s)
|
|
1262
|
-
*/
|
|
1263
|
-
async sendKeys(text, frequency = 0) {
|
|
1264
|
-
const body = {
|
|
1265
|
-
value: [...text]
|
|
1266
|
-
// spread to char array
|
|
1267
|
-
};
|
|
1268
|
-
if (frequency > 0) {
|
|
1269
|
-
body["frequency"] = frequency;
|
|
1270
|
-
}
|
|
1271
|
-
await this.request("POST", this.sessionPath("/wda/keys"), body);
|
|
1272
|
-
}
|
|
1273
|
-
/**
|
|
1274
|
-
* Presses a hardware button by name.
|
|
1275
|
-
*
|
|
1276
|
-
* @param button - "home" | "volumeUp" | "volumeDown"
|
|
1277
|
-
*/
|
|
1278
|
-
async pressButton(button) {
|
|
1279
|
-
await this.request("POST", this.sessionPath("/wda/pressButton"), {
|
|
1280
|
-
name: button
|
|
1281
|
-
});
|
|
1282
|
-
}
|
|
1283
|
-
async pressHome() {
|
|
1284
|
-
await this.pressButton("home");
|
|
1285
|
-
}
|
|
1286
|
-
// ---------------------------------------------------------------------------
|
|
1287
|
-
// App lifecycle
|
|
1288
|
-
// ---------------------------------------------------------------------------
|
|
1289
|
-
async launchApp(bundleId, args) {
|
|
1290
|
-
const body = { bundleId };
|
|
1291
|
-
if (args && args.length > 0) {
|
|
1292
|
-
body["arguments"] = args;
|
|
1293
|
-
}
|
|
1294
|
-
await this.request("POST", this.sessionPath("/wda/apps/launch"), body);
|
|
1295
|
-
}
|
|
1296
|
-
async terminateApp(bundleId) {
|
|
1297
|
-
await this.request("POST", this.sessionPath("/wda/apps/terminate"), {
|
|
1298
|
-
bundleId
|
|
1299
|
-
});
|
|
1300
|
-
}
|
|
1301
|
-
async activateApp(bundleId) {
|
|
1302
|
-
await this.request("POST", this.sessionPath("/wda/apps/activate"), {
|
|
1303
|
-
bundleId
|
|
1304
|
-
});
|
|
1305
|
-
}
|
|
1306
|
-
// ---------------------------------------------------------------------------
|
|
1307
|
-
// Deep link / URL
|
|
1308
|
-
// ---------------------------------------------------------------------------
|
|
1309
|
-
/** Opens a URL or deep link. Works for both simulator and real device. */
|
|
1310
|
-
async openUrl(url) {
|
|
1311
|
-
await this.request("POST", this.sessionPath("/url"), { url });
|
|
1312
|
-
}
|
|
1313
|
-
// ---------------------------------------------------------------------------
|
|
1314
|
-
// Alerts
|
|
1315
|
-
// ---------------------------------------------------------------------------
|
|
1316
|
-
async acceptAlert() {
|
|
1317
|
-
await this.request("POST", this.sessionPath("/alert/accept"), void 0);
|
|
1318
|
-
}
|
|
1319
|
-
async dismissAlert() {
|
|
1320
|
-
await this.request("POST", this.sessionPath("/alert/dismiss"), void 0);
|
|
1321
|
-
}
|
|
1322
|
-
// ---------------------------------------------------------------------------
|
|
1323
|
-
// Device status
|
|
1324
|
-
// ---------------------------------------------------------------------------
|
|
1325
|
-
async getStatus() {
|
|
1326
|
-
return this.request("GET", "/status");
|
|
1327
|
-
}
|
|
1328
|
-
};
|
|
1329
|
-
|
|
1330
|
-
// src/drivers/wda/driver.ts
|
|
1331
|
-
function msToSec(ms) {
|
|
1332
|
-
return ms / 1e3;
|
|
1333
|
-
}
|
|
1334
|
-
function sleep(ms) {
|
|
1335
|
-
return new Promise((resolve3) => setTimeout(resolve3, ms));
|
|
1336
|
-
}
|
|
1337
|
-
function replaceUrlScheme(url, scheme) {
|
|
1338
|
-
const match = url.match(/^([a-zA-Z][a-zA-Z0-9+.-]*):(\/\/.*)$/);
|
|
1339
|
-
if (!match) return null;
|
|
1340
|
-
return `${scheme}:${match[2]}`;
|
|
1341
|
-
}
|
|
1342
|
-
function createWDADriver(host, port, bundleId, simulatorUdid) {
|
|
1343
|
-
return Effect3.gen(function* () {
|
|
1344
|
-
const client = new WDAClient(host, port);
|
|
1345
|
-
const activateSimulatorApp = async (targetBundleId) => {
|
|
1346
|
-
if (!targetBundleId) return;
|
|
1347
|
-
await client.activateApp(targetBundleId);
|
|
1348
|
-
await sleep(1e3);
|
|
1349
|
-
};
|
|
1350
|
-
const openSimulatorUrl = async (url, targetBundleId = bundleId) => {
|
|
1351
|
-
if (!simulatorUdid) {
|
|
1352
|
-
await client.openUrl(url);
|
|
1353
|
-
return;
|
|
1354
|
-
}
|
|
1355
|
-
const fallbackSchemes = installedUrlSchemesOnSimulator(simulatorUdid, targetBundleId);
|
|
1356
|
-
const candidates = [
|
|
1357
|
-
url,
|
|
1358
|
-
...fallbackSchemes.map((scheme) => replaceUrlScheme(url, scheme)).filter((candidate) => Boolean(candidate))
|
|
1359
|
-
].filter((candidate, index, all) => all.indexOf(candidate) === index);
|
|
1360
|
-
let lastError;
|
|
1361
|
-
for (const candidate of candidates) {
|
|
1362
|
-
try {
|
|
1363
|
-
terminateOnSimulator(simulatorUdid, targetBundleId);
|
|
1364
|
-
await sleep(500);
|
|
1365
|
-
launchWithUrlOnSimulator(simulatorUdid, targetBundleId, candidate);
|
|
1366
|
-
await sleep(1500);
|
|
1367
|
-
try {
|
|
1368
|
-
await client.deleteSession();
|
|
1369
|
-
} catch {
|
|
1370
|
-
}
|
|
1371
|
-
await client.createSession(targetBundleId);
|
|
1372
|
-
await client.disableQuiescence();
|
|
1373
|
-
await sleep(500);
|
|
1374
|
-
return;
|
|
1375
|
-
} catch (error) {
|
|
1376
|
-
lastError = error;
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
throw lastError ?? new Error(`Failed to open URL: ${url}`);
|
|
1380
|
-
};
|
|
1381
|
-
yield* Effect3.tryPromise({
|
|
1382
|
-
try: () => client.createSession(bundleId || void 0),
|
|
1383
|
-
catch: (e) => new DriverError({ message: `Failed to create WDA session: ${e}` })
|
|
1384
|
-
});
|
|
1385
|
-
yield* Effect3.tryPromise({
|
|
1386
|
-
try: () => client.disableQuiescence(),
|
|
1387
|
-
catch: (e) => new DriverError({ message: `Failed to disable quiescence: ${e}` })
|
|
1388
|
-
});
|
|
1389
|
-
const service = {
|
|
1390
|
-
// -----------------------------------------------------------------------
|
|
1391
|
-
// Hierarchy
|
|
1392
|
-
// -----------------------------------------------------------------------
|
|
1393
|
-
dumpHierarchy: () => Effect3.tryPromise({
|
|
1394
|
-
try: () => client.getSource(),
|
|
1395
|
-
catch: (e) => new DriverError({ message: `Failed to get page source: ${e}` })
|
|
1396
|
-
}),
|
|
1397
|
-
// -----------------------------------------------------------------------
|
|
1398
|
-
// Coordinate-level actions
|
|
1399
|
-
// -----------------------------------------------------------------------
|
|
1400
|
-
tapAtCoordinate: (x, y) => Effect3.tryPromise({
|
|
1401
|
-
try: () => client.tap(x, y),
|
|
1402
|
-
catch: (e) => new DriverError({ message: `Tap failed: ${e}` })
|
|
1403
|
-
}),
|
|
1404
|
-
doubleTapAtCoordinate: (x, y) => Effect3.tryPromise({
|
|
1405
|
-
try: () => client.doubleTap(x, y),
|
|
1406
|
-
catch: (e) => new DriverError({ message: `Double tap failed: ${e}` })
|
|
1407
|
-
}),
|
|
1408
|
-
longPressAtCoordinate: (x, y, duration) => Effect3.tryPromise({
|
|
1409
|
-
// duration arrives in ms (spana convention) → convert to seconds for WDA
|
|
1410
|
-
try: () => client.longPress(x, y, msToSec(duration)),
|
|
1411
|
-
catch: (e) => new DriverError({ message: `Long press failed: ${e}` })
|
|
1412
|
-
}),
|
|
1413
|
-
swipe: (sx, sy, ex, ey, dur) => Effect3.tryPromise({
|
|
1414
|
-
// dur arrives in ms → convert to seconds for WDA
|
|
1415
|
-
try: () => client.swipe(sx, sy, ex, ey, msToSec(dur)),
|
|
1416
|
-
catch: (e) => new DriverError({ message: `Swipe failed: ${e}` })
|
|
1417
|
-
}),
|
|
1418
|
-
// -----------------------------------------------------------------------
|
|
1419
|
-
// Text input
|
|
1420
|
-
// -----------------------------------------------------------------------
|
|
1421
|
-
inputText: (text) => Effect3.tryPromise({
|
|
1422
|
-
try: () => client.sendKeys(text),
|
|
1423
|
-
catch: (e) => new DriverError({ message: `Input text failed: ${e}` })
|
|
1424
|
-
}),
|
|
1425
|
-
pressKey: (key) => Effect3.tryPromise({
|
|
1426
|
-
// WDA uses named hardware buttons; map common names, fall back to home
|
|
1427
|
-
try: async () => {
|
|
1428
|
-
const buttonMap = {
|
|
1429
|
-
home: "home",
|
|
1430
|
-
volumeup: "volumeUp",
|
|
1431
|
-
volumedown: "volumeDown"
|
|
1432
|
-
};
|
|
1433
|
-
const button = buttonMap[key.toLowerCase()];
|
|
1434
|
-
if (button) {
|
|
1435
|
-
await client.pressButton(button);
|
|
1436
|
-
}
|
|
1437
|
-
},
|
|
1438
|
-
catch: (e) => new DriverError({ message: `Press key failed: ${e}` })
|
|
1439
|
-
}),
|
|
1440
|
-
hideKeyboard: () => Effect3.tryPromise({
|
|
1441
|
-
// WDA hides the keyboard when no text field has focus; pressing home
|
|
1442
|
-
// is the most reliable cross-version approach.
|
|
1443
|
-
try: () => client.pressHome(),
|
|
1444
|
-
catch: (e) => new DriverError({ message: `Hide keyboard failed: ${e}` })
|
|
1445
|
-
}),
|
|
1446
|
-
// -----------------------------------------------------------------------
|
|
1447
|
-
// Queries
|
|
1448
|
-
// -----------------------------------------------------------------------
|
|
1449
|
-
takeScreenshot: () => Effect3.tryPromise({
|
|
1450
|
-
try: () => client.getScreenshot(),
|
|
1451
|
-
catch: (e) => new DriverError({ message: `Screenshot failed: ${e}` })
|
|
1452
|
-
}),
|
|
1453
|
-
getDeviceInfo: () => Effect3.tryPromise({
|
|
1454
|
-
try: async () => {
|
|
1455
|
-
const size = await client.getWindowSize();
|
|
1456
|
-
return {
|
|
1457
|
-
platform: "ios",
|
|
1458
|
-
deviceId: `${host}:${port}`,
|
|
1459
|
-
name: "iOS Device",
|
|
1460
|
-
isEmulator: host === "localhost" || host === "127.0.0.1",
|
|
1461
|
-
screenWidth: size.width,
|
|
1462
|
-
screenHeight: size.height,
|
|
1463
|
-
driverType: "wda"
|
|
1464
|
-
};
|
|
1465
|
-
},
|
|
1466
|
-
catch: (e) => new DriverError({ message: `Get device info failed: ${e}` })
|
|
1467
|
-
}),
|
|
1468
|
-
// -----------------------------------------------------------------------
|
|
1469
|
-
// App lifecycle
|
|
1470
|
-
// -----------------------------------------------------------------------
|
|
1471
|
-
launchApp: (appBundleId, opts) => Effect3.tryPromise({
|
|
1472
|
-
try: async () => {
|
|
1473
|
-
if (simulatorUdid) {
|
|
1474
|
-
if (opts?.deepLink) {
|
|
1475
|
-
await openSimulatorUrl(opts.deepLink, appBundleId);
|
|
1476
|
-
} else {
|
|
1477
|
-
launchOnSimulator(simulatorUdid, appBundleId);
|
|
1478
|
-
await sleep(500);
|
|
1479
|
-
await activateSimulatorApp(appBundleId);
|
|
1480
|
-
}
|
|
1481
|
-
return;
|
|
1482
|
-
}
|
|
1483
|
-
await (opts?.deepLink ? client.openUrl(opts.deepLink) : client.launchApp(appBundleId));
|
|
1484
|
-
},
|
|
1485
|
-
catch: (e) => new DriverError({ message: `Launch app failed: ${e}` })
|
|
1486
|
-
}),
|
|
1487
|
-
stopApp: (appBundleId) => Effect3.tryPromise({
|
|
1488
|
-
try: () => {
|
|
1489
|
-
if (simulatorUdid) {
|
|
1490
|
-
terminateOnSimulator(simulatorUdid, appBundleId);
|
|
1491
|
-
return Promise.resolve();
|
|
1492
|
-
}
|
|
1493
|
-
return client.terminateApp(appBundleId);
|
|
1494
|
-
},
|
|
1495
|
-
catch: (e) => new DriverError({ message: `Stop app failed: ${e}` })
|
|
1496
|
-
}),
|
|
1497
|
-
killApp: (appBundleId) => Effect3.tryPromise({
|
|
1498
|
-
try: () => {
|
|
1499
|
-
if (simulatorUdid) {
|
|
1500
|
-
terminateOnSimulator(simulatorUdid, appBundleId);
|
|
1501
|
-
return Promise.resolve();
|
|
1502
|
-
}
|
|
1503
|
-
return client.terminateApp(appBundleId);
|
|
1504
|
-
},
|
|
1505
|
-
catch: (e) => new DriverError({ message: `Kill app failed: ${e}` })
|
|
1506
|
-
}),
|
|
1507
|
-
clearAppState: (appBundleId) => Effect3.tryPromise({
|
|
1508
|
-
try: async () => {
|
|
1509
|
-
if (simulatorUdid) {
|
|
1510
|
-
terminateOnSimulator(simulatorUdid, appBundleId);
|
|
1511
|
-
return;
|
|
1512
|
-
}
|
|
1513
|
-
await client.terminateApp(appBundleId);
|
|
1514
|
-
},
|
|
1515
|
-
catch: (e) => new DriverError({ message: `Clear app state failed: ${e}` })
|
|
1516
|
-
}),
|
|
1517
|
-
// -----------------------------------------------------------------------
|
|
1518
|
-
// Navigation
|
|
1519
|
-
// -----------------------------------------------------------------------
|
|
1520
|
-
openLink: (url) => Effect3.tryPromise({
|
|
1521
|
-
try: async () => {
|
|
1522
|
-
if (simulatorUdid) {
|
|
1523
|
-
await openSimulatorUrl(url);
|
|
1524
|
-
return;
|
|
1525
|
-
}
|
|
1526
|
-
await client.openUrl(url);
|
|
1527
|
-
},
|
|
1528
|
-
catch: (e) => new DriverError({ message: `Open link failed: ${e}` })
|
|
1529
|
-
}),
|
|
1530
|
-
back: () => Effect3.tryPromise({
|
|
1531
|
-
// iOS has no hardware back button; pressing home is the closest
|
|
1532
|
-
// equivalent for dismissing the current context.
|
|
1533
|
-
try: () => client.pressHome(),
|
|
1534
|
-
catch: (e) => new DriverError({ message: `Back failed: ${e}` })
|
|
1535
|
-
})
|
|
1536
|
-
};
|
|
1537
|
-
return service;
|
|
1538
|
-
});
|
|
1539
|
-
}
|
|
1540
|
-
|
|
1541
|
-
// src/drivers/uiautomator2/installer.ts
|
|
1542
|
-
import { execSync } from "child_process";
|
|
1543
|
-
import { existsSync, readdirSync } from "fs";
|
|
1544
|
-
import { createRequire } from "module";
|
|
1545
|
-
import { dirname, resolve, join } from "path";
|
|
1546
|
-
import { fileURLToPath } from "url";
|
|
1547
|
-
var __dirname2 = fileURLToPath(new URL(".", import.meta.url));
|
|
1548
|
-
var require2 = createRequire(import.meta.url);
|
|
1549
|
-
function resolveOptionalPackageDir(packageName, subpath) {
|
|
1550
|
-
try {
|
|
1551
|
-
const packageJsonPath = require2.resolve(`${packageName}/package.json`);
|
|
1552
|
-
return resolve(dirname(packageJsonPath), subpath);
|
|
1553
|
-
} catch {
|
|
1554
|
-
return null;
|
|
1555
|
-
}
|
|
1556
|
-
}
|
|
1557
|
-
function findUiAutomator2APK() {
|
|
1558
|
-
const appiumServerApks = resolveOptionalPackageDir("appium-uiautomator2-server", "apks");
|
|
1559
|
-
const projectDrivers = resolve(process.cwd(), "drivers/android");
|
|
1560
|
-
const provDrivers = resolve(__dirname2, "../../../../drivers/android");
|
|
1561
|
-
const maestroRunnerDrivers = resolve(
|
|
1562
|
-
__dirname2,
|
|
1563
|
-
"../../../../../../maestro-runner/drivers/android"
|
|
1564
|
-
);
|
|
1565
|
-
for (const dir of [projectDrivers, provDrivers, maestroRunnerDrivers, appiumServerApks]) {
|
|
1566
|
-
if (!dir) continue;
|
|
1567
|
-
if (!existsSync(dir)) continue;
|
|
1568
|
-
const files = readdirSync(dir);
|
|
1569
|
-
const serverApk = files.find(
|
|
1570
|
-
(f) => f.startsWith("appium-uiautomator2-server") && !f.includes("androidTest") && f.endsWith(".apk")
|
|
1571
|
-
);
|
|
1572
|
-
const testApk = files.find(
|
|
1573
|
-
(f) => f.includes("androidTest") && f.endsWith(".apk")
|
|
1574
|
-
);
|
|
1575
|
-
if (serverApk && testApk) {
|
|
1576
|
-
return { serverApk: join(dir, serverApk), testApk: join(dir, testApk) };
|
|
1577
|
-
}
|
|
1578
|
-
}
|
|
1579
|
-
return null;
|
|
1580
|
-
}
|
|
1581
|
-
function installUiAutomator2(serial) {
|
|
1582
|
-
const apks = findUiAutomator2APK();
|
|
1583
|
-
if (!apks) {
|
|
1584
|
-
throw new Error(
|
|
1585
|
-
"UiAutomator2 server APK not found. Install appium-uiautomator2-server or place APKs in drivers/android/"
|
|
1586
|
-
);
|
|
1587
|
-
}
|
|
1588
|
-
console.log(`Installing UiAutomator2 server on ${serial}...`);
|
|
1589
|
-
adbInstall(serial, apks.serverApk);
|
|
1590
|
-
adbInstall(serial, apks.testApk);
|
|
1591
|
-
console.log("UiAutomator2 server installed.");
|
|
1592
|
-
}
|
|
1593
|
-
function isUiAutomator2Installed(serial) {
|
|
1594
|
-
try {
|
|
1595
|
-
const output = adbShell(
|
|
1596
|
-
serial,
|
|
1597
|
-
"pm list packages io.appium.uiautomator2.server"
|
|
1598
|
-
);
|
|
1599
|
-
return output.includes("io.appium.uiautomator2.server");
|
|
1600
|
-
} catch {
|
|
1601
|
-
return false;
|
|
1602
|
-
}
|
|
1603
|
-
}
|
|
1604
|
-
function startUiAutomator2Server(serial) {
|
|
1605
|
-
const adb = findADB();
|
|
1606
|
-
if (!adb) throw new Error("adb not found");
|
|
1607
|
-
try {
|
|
1608
|
-
execSync(
|
|
1609
|
-
`${adb} -s ${serial} shell am instrument -w -e disableAnalytics true io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner &`,
|
|
1610
|
-
{ stdio: "ignore", timeout: 5e3 }
|
|
1611
|
-
);
|
|
1612
|
-
} catch {
|
|
1613
|
-
}
|
|
1614
|
-
}
|
|
1615
|
-
async function setupUiAutomator2(serial, hostPort = 8200) {
|
|
1616
|
-
if (!isUiAutomator2Installed(serial)) {
|
|
1617
|
-
installUiAutomator2(serial);
|
|
1618
|
-
}
|
|
1619
|
-
startUiAutomator2Server(serial);
|
|
1620
|
-
await new Promise((r) => setTimeout(r, 2e3));
|
|
1621
|
-
const devicePort = 6790;
|
|
1622
|
-
adbForward(serial, hostPort, devicePort);
|
|
1623
|
-
const maxRetries = 10;
|
|
1624
|
-
for (let i = 0; i < maxRetries; i++) {
|
|
1625
|
-
try {
|
|
1626
|
-
const res = await fetch(`http://localhost:${hostPort}/status`);
|
|
1627
|
-
if (res.ok) {
|
|
1628
|
-
console.log(`UiAutomator2 server ready on port ${hostPort}`);
|
|
1629
|
-
return { host: "localhost", port: hostPort };
|
|
1630
|
-
}
|
|
1631
|
-
} catch {
|
|
1632
|
-
}
|
|
1633
|
-
await new Promise((r) => setTimeout(r, 1e3));
|
|
1634
|
-
}
|
|
1635
|
-
throw new Error(
|
|
1636
|
-
`UiAutomator2 server did not start within ${maxRetries} seconds`
|
|
1637
|
-
);
|
|
1638
|
-
}
|
|
1639
|
-
|
|
1640
|
-
// src/drivers/wda/installer.ts
|
|
1641
|
-
import { execSync as execSync2, spawn } from "child_process";
|
|
1642
|
-
import { existsSync as existsSync2 } from "fs";
|
|
1643
|
-
import { createRequire as createRequire2 } from "module";
|
|
1644
|
-
import { dirname as dirname2, resolve as resolve2 } from "path";
|
|
1645
|
-
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
1646
|
-
var __dirname3 = fileURLToPath2(new URL(".", import.meta.url));
|
|
1647
|
-
var require3 = createRequire2(import.meta.url);
|
|
1648
|
-
function resolveOptionalPackageDir2(packageName) {
|
|
1649
|
-
try {
|
|
1650
|
-
const packageJsonPath = require3.resolve(`${packageName}/package.json`);
|
|
1651
|
-
return dirname2(packageJsonPath);
|
|
1652
|
-
} catch {
|
|
1653
|
-
return null;
|
|
1654
|
-
}
|
|
1655
|
-
}
|
|
1656
|
-
function findWDAProject() {
|
|
1657
|
-
const appiumWDA = resolveOptionalPackageDir2("appium-webdriveragent");
|
|
1658
|
-
const projectWDA = resolve2(process.cwd(), "drivers/ios/WebDriverAgent");
|
|
1659
|
-
const provWDA = resolve2(__dirname3, "../../../../drivers/ios/WebDriverAgent");
|
|
1660
|
-
const maestroRunnerWDA = resolve2(
|
|
1661
|
-
__dirname3,
|
|
1662
|
-
"../../../../../../maestro-runner/drivers/ios/WebDriverAgent"
|
|
1663
|
-
);
|
|
1664
|
-
for (const wdaPath of [projectWDA, provWDA, maestroRunnerWDA, appiumWDA]) {
|
|
1665
|
-
if (!wdaPath) continue;
|
|
1666
|
-
if (existsSync2(resolve2(wdaPath, "WebDriverAgent.xcodeproj"))) {
|
|
1667
|
-
return wdaPath;
|
|
1668
|
-
}
|
|
1669
|
-
}
|
|
1670
|
-
return null;
|
|
1671
|
-
}
|
|
1672
|
-
function buildWDA(simulatorUDID) {
|
|
1673
|
-
const wdaPath = findWDAProject();
|
|
1674
|
-
if (!wdaPath) {
|
|
1675
|
-
throw new Error(
|
|
1676
|
-
"WebDriverAgent project not found. Install appium-webdriveragent or place WDA in drivers/ios/WebDriverAgent/"
|
|
1677
|
-
);
|
|
1678
|
-
}
|
|
1679
|
-
const derivedDataPath = resolve2(wdaPath, "../../.wda-builds", simulatorUDID);
|
|
1680
|
-
console.log("Building WebDriverAgent for simulator...");
|
|
1681
|
-
execSync2(
|
|
1682
|
-
[
|
|
1683
|
-
"xcodebuild",
|
|
1684
|
-
"build-for-testing",
|
|
1685
|
-
`-project "${resolve2(wdaPath, "WebDriverAgent.xcodeproj")}"`,
|
|
1686
|
-
"-scheme WebDriverAgentRunner",
|
|
1687
|
-
`-destination "platform=iOS Simulator,id=${simulatorUDID}"`,
|
|
1688
|
-
`-derivedDataPath "${derivedDataPath}"`,
|
|
1689
|
-
'CODE_SIGN_IDENTITY=""',
|
|
1690
|
-
"CODE_SIGNING_REQUIRED=NO",
|
|
1691
|
-
"CODE_SIGNING_ALLOWED=NO",
|
|
1692
|
-
"GCC_TREAT_WARNINGS_AS_ERRORS=0",
|
|
1693
|
-
"-quiet"
|
|
1694
|
-
].join(" "),
|
|
1695
|
-
{ stdio: "inherit", timeout: 6e5 }
|
|
1696
|
-
// 10 min build timeout
|
|
1697
|
-
);
|
|
1698
|
-
console.log("WebDriverAgent built successfully.");
|
|
1699
|
-
return derivedDataPath;
|
|
1700
|
-
}
|
|
1701
|
-
var wdaProcess = null;
|
|
1702
|
-
function startWDA(simulatorUDID, port = 8100, derivedDataPath) {
|
|
1703
|
-
const wdaPath = findWDAProject();
|
|
1704
|
-
if (!wdaPath) {
|
|
1705
|
-
throw new Error("WebDriverAgent project not found");
|
|
1706
|
-
}
|
|
1707
|
-
const ddPath = derivedDataPath ?? resolve2(wdaPath, "../../.wda-builds", simulatorUDID);
|
|
1708
|
-
const env = { ...process.env, USE_PORT: String(port) };
|
|
1709
|
-
console.log(`Starting WebDriverAgent on port ${port}...`);
|
|
1710
|
-
const child = spawn(
|
|
1711
|
-
"xcodebuild",
|
|
1712
|
-
[
|
|
1713
|
-
"test-without-building",
|
|
1714
|
-
"-project",
|
|
1715
|
-
resolve2(wdaPath, "WebDriverAgent.xcodeproj"),
|
|
1716
|
-
"-scheme",
|
|
1717
|
-
"WebDriverAgentRunner",
|
|
1718
|
-
"-destination",
|
|
1719
|
-
`platform=iOS Simulator,id=${simulatorUDID}`,
|
|
1720
|
-
"-derivedDataPath",
|
|
1721
|
-
ddPath
|
|
1722
|
-
],
|
|
1723
|
-
{ env, stdio: "ignore", detached: true }
|
|
1724
|
-
);
|
|
1725
|
-
child.unref();
|
|
1726
|
-
wdaProcess = child;
|
|
1727
|
-
return child;
|
|
1728
|
-
}
|
|
1729
|
-
async function setupWDA(simulatorUDID, port = 8100) {
|
|
1730
|
-
const wdaPath = findWDAProject();
|
|
1731
|
-
if (!wdaPath) {
|
|
1732
|
-
throw new Error("WebDriverAgent project not found");
|
|
1733
|
-
}
|
|
1734
|
-
const derivedDataPath = resolve2(
|
|
1735
|
-
wdaPath,
|
|
1736
|
-
"../../.wda-builds",
|
|
1737
|
-
simulatorUDID
|
|
1738
|
-
);
|
|
1739
|
-
const buildProductsDir = resolve2(
|
|
1740
|
-
derivedDataPath,
|
|
1741
|
-
"Build",
|
|
1742
|
-
"Products"
|
|
1743
|
-
);
|
|
1744
|
-
if (!existsSync2(buildProductsDir)) {
|
|
1745
|
-
buildWDA(simulatorUDID);
|
|
1746
|
-
}
|
|
1747
|
-
startWDA(simulatorUDID, port, derivedDataPath);
|
|
1748
|
-
const maxRetries = 30;
|
|
1749
|
-
for (let i = 0; i < maxRetries; i++) {
|
|
1750
|
-
try {
|
|
1751
|
-
const res = await fetch(`http://localhost:${port}/status`);
|
|
1752
|
-
if (res.ok) {
|
|
1753
|
-
console.log(`WebDriverAgent ready on port ${port}`);
|
|
1754
|
-
return { host: "localhost", port };
|
|
1755
|
-
}
|
|
1756
|
-
} catch {
|
|
1757
|
-
}
|
|
1758
|
-
await new Promise((r) => setTimeout(r, 1e3));
|
|
1759
|
-
}
|
|
1760
|
-
throw new Error(
|
|
1761
|
-
`WebDriverAgent did not start within ${maxRetries} seconds`
|
|
1762
|
-
);
|
|
1763
|
-
}
|
|
1764
|
-
|
|
1765
|
-
export {
|
|
1766
|
-
ElementNotFoundError,
|
|
1767
|
-
TextMismatchError,
|
|
1768
|
-
WaitTimeoutError,
|
|
1769
|
-
flattenElements,
|
|
1770
|
-
findElement,
|
|
1771
|
-
centerOf,
|
|
1772
|
-
parseWebHierarchy,
|
|
1773
|
-
makePlaywrightDriver,
|
|
1774
|
-
parseAndroidHierarchy,
|
|
1775
|
-
createUiAutomator2Driver,
|
|
1776
|
-
parseIOSHierarchy,
|
|
1777
|
-
createWDADriver,
|
|
1778
|
-
setupUiAutomator2,
|
|
1779
|
-
setupWDA
|
|
1780
|
-
};
|
|
1781
|
-
//# sourceMappingURL=chunk-VZZH66U5.js.map
|