@mobilewright/inspector 0.0.60 → 0.0.62
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/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +42 -8
- package/dist/index.js.map +1 -1
- package/dist/lib/device-manager.d.ts +24 -9
- package/dist/lib/device-manager.d.ts.map +1 -1
- package/dist/lib/device-manager.js +95 -14
- package/dist/lib/device-manager.js.map +1 -1
- package/dist/lib/inspect-screen.d.ts +48 -0
- package/dist/lib/inspect-screen.d.ts.map +1 -0
- package/dist/lib/inspect-screen.js +105 -0
- package/dist/lib/inspect-screen.js.map +1 -0
- package/dist/lib/locator-derivation.d.ts +17 -2
- package/dist/lib/locator-derivation.d.ts.map +1 -1
- package/dist/lib/locator-derivation.js +26 -7
- package/dist/lib/locator-derivation.js.map +1 -1
- package/dist/lib/timeout.d.ts +3 -0
- package/dist/lib/timeout.d.ts.map +1 -0
- package/dist/lib/timeout.js +9 -0
- package/dist/lib/timeout.js.map +1 -0
- package/dist/routes/device-actions.d.ts +7 -0
- package/dist/routes/device-actions.d.ts.map +1 -0
- package/dist/routes/device-actions.js +105 -0
- package/dist/routes/device-actions.js.map +1 -0
- package/dist/routes/devices.d.ts +1 -1
- package/dist/routes/devices.d.ts.map +1 -1
- package/dist/routes/devices.js +10 -11
- package/dist/routes/devices.js.map +1 -1
- package/dist/routes/inspect.d.ts.map +1 -1
- package/dist/routes/inspect.js +22 -92
- package/dist/routes/inspect.js.map +1 -1
- package/package.json +3 -3
- package/public/codegen.html +161 -0
- package/public/css/app.css +337 -2
- package/public/fonts/codicon-LICENSE.txt +21 -0
- package/public/fonts/codicon.ttf +0 -0
- package/public/index.html +3 -2
- package/public/js/app.js +203 -55
- package/public/js/codegen.js +425 -0
- package/public/js/inspect.js +7 -0
- package/public/js/view-tree.js +182 -0
- package/public/logo.png +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Derives the best mobilewright locator for every node in a ViewNode tree.
|
|
2
2
|
// Priority: Test ID > Role > Label > Text. Mirrors @mobilewright/core query-engine.ts,
|
|
3
3
|
// and reuses its ROLE_TYPE_MAP so role derivation cannot drift from role matching.
|
|
4
|
-
import { ROLE_TYPE_MAP, bareTypeName } from '@mobilewright/core';
|
|
4
|
+
import { ROLE_TYPE_MAP, bareTypeName, queryAll } from '@mobilewright/core';
|
|
5
5
|
// core declares ROLE_TYPE_MAP `as const`, so each value is a readonly tuple of
|
|
6
6
|
// string literals. Widen to plain strings once, here, so membership can be tested
|
|
7
7
|
// against an arbitrary node type.
|
|
@@ -10,6 +10,10 @@ const ROLE_TYPES = Object.entries(ROLE_TYPE_MAP);
|
|
|
10
10
|
// iOS reports generic containers as "other", so deriving getByRole('listitem')
|
|
11
11
|
// from one would hand the user a locator matching much of the tree.
|
|
12
12
|
const TYPES_TOO_GENERIC_TO_SUGGEST = new Set(['other']);
|
|
13
|
+
/** Whether text can be typed into the node: the same types getByRole('textfield') matches. */
|
|
14
|
+
export function isEditable(node) {
|
|
15
|
+
return ROLE_TYPE_MAP.textfield.includes(bareTypeName(node.type ?? ''));
|
|
16
|
+
}
|
|
13
17
|
/** Map node.type to a mobilewright role string. Returns null for unmapped types. */
|
|
14
18
|
function deriveRole(node) {
|
|
15
19
|
// Same normalization core applies before matching: strips the Android package
|
|
@@ -36,7 +40,7 @@ function deriveRole(node) {
|
|
|
36
40
|
*/
|
|
37
41
|
export function deriveLocators(node) {
|
|
38
42
|
const locators = [];
|
|
39
|
-
const testId = node.identifier || node.resourceId;
|
|
43
|
+
const testId = node.key || node.identifier || node.resourceId;
|
|
40
44
|
if (testId)
|
|
41
45
|
locators.push({ kind: 'testId', value: testId });
|
|
42
46
|
const role = deriveRole(node);
|
|
@@ -103,15 +107,30 @@ function preferUniqueRolesOverDuplicateTestIds(entries) {
|
|
|
103
107
|
*/
|
|
104
108
|
export function deriveElementList(roots) {
|
|
105
109
|
const result = [];
|
|
106
|
-
function walk(nodes) {
|
|
110
|
+
function walk(nodes, depth) {
|
|
107
111
|
for (const node of nodes) {
|
|
108
112
|
const nodeLocators = deriveLocators(node);
|
|
109
|
-
result.push({ node, locator: nodeLocators[0] ?? null, locators: nodeLocators });
|
|
110
|
-
if (node.children?.length)
|
|
111
|
-
walk(node.children);
|
|
113
|
+
result.push({ node, locator: nodeLocators[0] ?? null, locators: nodeLocators, depth });
|
|
114
|
+
if (node.children?.length) {
|
|
115
|
+
walk(node.children, depth + 1);
|
|
116
|
+
}
|
|
112
117
|
}
|
|
113
118
|
}
|
|
114
|
-
walk(roots);
|
|
119
|
+
walk(roots, 0);
|
|
115
120
|
return preferUniqueRolesOverDuplicateTestIds(result);
|
|
116
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Position of `node` among all matches of `locator`, computed with core's own query
|
|
124
|
+
* engine, so `index` is exactly what `.nth(index)` resolves to when the test runs.
|
|
125
|
+
* Returns null when the locator does not match the node at all.
|
|
126
|
+
* ponytail: one full-tree query per element, O(n²) per refresh; index the tree if screens get huge.
|
|
127
|
+
*/
|
|
128
|
+
export function locatorMatchPosition(roots, node, locator) {
|
|
129
|
+
const matches = queryAll(roots, locator);
|
|
130
|
+
const index = matches.indexOf(node);
|
|
131
|
+
if (index === -1) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
return { index, count: matches.length };
|
|
135
|
+
}
|
|
117
136
|
//# sourceMappingURL=locator-derivation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"locator-derivation.js","sourceRoot":"","sources":["../../src/lib/locator-derivation.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,uFAAuF;AACvF,mFAAmF;AAGnF,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"locator-derivation.js","sourceRoot":"","sources":["../../src/lib/locator-derivation.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,uFAAu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AAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,CAAC;YACvF,IAAI,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,CAAC;gBAC1B,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACjC,CAAC;QACH,CAAC;IACH,CAAC;IAED,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACf,OAAO,qCAAqC,CAAC,MAAM,CAAC,CAAC;AACvD,CAAC;AAQD;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAiB,EAAE,IAAc,EAAE,OAAgB;IACtF,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACzC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;QACjB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC;AAC1C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../../src/lib/timeout.ts"],"names":[],"mappings":"AAAA,yFAAyF;AACzF,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAM3E"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Reject if `promise` doesn't settle within `ms`; the underlying call keeps running. */
|
|
2
|
+
export function timeoutAfter(promise, ms) {
|
|
3
|
+
let timer;
|
|
4
|
+
return Promise.race([
|
|
5
|
+
promise,
|
|
6
|
+
new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(`Operation timed out after ${ms}ms`)), ms); }),
|
|
7
|
+
]).finally(() => clearTimeout(timer));
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=timeout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.js","sourceRoot":"","sources":["../../src/lib/timeout.ts"],"names":[],"mappings":"AAAA,yFAAyF;AACzF,MAAM,UAAU,YAAY,CAAI,OAAmB,EAAE,EAAU;IAC7D,IAAI,KAAoC,CAAC;IACzC,OAAO,OAAO,CAAC,IAAI,CAAC;QAClB,OAAO;QACP,IAAI,OAAO,CAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,6BAA6B,EAAE,IAAI,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;KAC7H,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DeviceManager } from '../lib/device-manager.js';
|
|
2
|
+
/**
|
|
3
|
+
* Express router for actions codegen performs on the device.
|
|
4
|
+
* POST /api/tap, POST /api/press-button, POST /api/geolocation, POST /api/open-url, POST /api/fill
|
|
5
|
+
*/
|
|
6
|
+
export declare function createDeviceActionsRouter(deviceManager: DeviceManager): import("express-serve-static-core").Router;
|
|
7
|
+
//# sourceMappingURL=device-actions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device-actions.d.ts","sourceRoot":"","sources":["../../src/routes/device-actions.ts"],"names":[],"mappings":"AAIA,OAAO,EAAe,aAAa,EAAE,MAAM,0BAA0B,CAAC;AActE;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,aAAa,EAAE,aAAa,8CAsCrE"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { logger } from '../lib/logger.js';
|
|
3
|
+
import { DeviceError } from '../lib/device-manager.js';
|
|
4
|
+
/** Screen gestures /api/tap can perform at a point; each is a Screen method taking (x, y). */
|
|
5
|
+
const TAP_GESTURES = ['tap', 'doubleTap', 'longPress'];
|
|
6
|
+
/** Hardware buttons the codegen toolbar can press. */
|
|
7
|
+
const RECORDER_BUTTONS = ['HOME', 'BACK', 'APP_SWITCH'];
|
|
8
|
+
/**
|
|
9
|
+
* Express router for actions codegen performs on the device.
|
|
10
|
+
* POST /api/tap, POST /api/press-button, POST /api/geolocation, POST /api/open-url, POST /api/fill
|
|
11
|
+
*/
|
|
12
|
+
export function createDeviceActionsRouter(deviceManager) {
|
|
13
|
+
const router = Router();
|
|
14
|
+
// Every action: 400 for an invalid body, 409 without a device, 503 while switching devices,
|
|
15
|
+
// 500 if the device call fails.
|
|
16
|
+
function deviceAction(name, parse) {
|
|
17
|
+
return async (req, res) => {
|
|
18
|
+
const action = parse((req.body ?? {}));
|
|
19
|
+
if ('error' in action) {
|
|
20
|
+
res.status(400).json({ error: action.error });
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
await deviceManager.withDevice(action.run);
|
|
25
|
+
res.json({ ok: true });
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
const status = statusFor(err);
|
|
29
|
+
if (status === 500) {
|
|
30
|
+
logger.error(`${name} failed: ${err.message}`);
|
|
31
|
+
}
|
|
32
|
+
res.status(status).json({ error: err.message });
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// body: { x: number, y: number, gesture?: 'tap' | 'doubleTap' | 'longPress' }, in logical screen points
|
|
37
|
+
router.post('/tap', deviceAction('Tap', parseTap));
|
|
38
|
+
// body: { button: 'HOME' | 'BACK' | 'APP_SWITCH' }
|
|
39
|
+
router.post('/press-button', deviceAction('Press button', parsePressButton));
|
|
40
|
+
// body: { geolocation: { latitude, longitude } } to set, { geolocation: null } to reset
|
|
41
|
+
router.post('/geolocation', deviceAction('Set geolocation', parseGeolocation));
|
|
42
|
+
// body: { url: string }, a web url or deep link with a scheme
|
|
43
|
+
router.post('/open-url', deviceAction('Open URL', parseOpenUrl));
|
|
44
|
+
// body: { x: number, y: number, text: string }, the center of the element to fill
|
|
45
|
+
router.post('/fill', deviceAction('Fill', parseFill));
|
|
46
|
+
return router;
|
|
47
|
+
}
|
|
48
|
+
function statusFor(err) {
|
|
49
|
+
if (err instanceof DeviceError && err.code === 'not_found') {
|
|
50
|
+
return 409;
|
|
51
|
+
}
|
|
52
|
+
if (err instanceof DeviceError && err.code === 'in_progress') {
|
|
53
|
+
return 503;
|
|
54
|
+
}
|
|
55
|
+
return 500;
|
|
56
|
+
}
|
|
57
|
+
function parseTap({ x, y, gesture = 'tap' }) {
|
|
58
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
59
|
+
return { error: 'x and y must be numbers' };
|
|
60
|
+
}
|
|
61
|
+
if (!TAP_GESTURES.includes(gesture)) {
|
|
62
|
+
return { error: `gesture must be one of ${TAP_GESTURES.join(', ')}` };
|
|
63
|
+
}
|
|
64
|
+
return { run: device => device.screen[gesture](x, y) };
|
|
65
|
+
}
|
|
66
|
+
function parsePressButton({ button }) {
|
|
67
|
+
if (!RECORDER_BUTTONS.includes(button)) {
|
|
68
|
+
return { error: `button must be one of ${RECORDER_BUTTONS.join(', ')}` };
|
|
69
|
+
}
|
|
70
|
+
return { run: device => device.screen.pressButton(button) };
|
|
71
|
+
}
|
|
72
|
+
function parseOpenUrl({ url }) {
|
|
73
|
+
if (typeof url !== 'string' || !URL.canParse(url)) {
|
|
74
|
+
return { error: 'url must be a url with a scheme, e.g. https://example.com or myapp://path' };
|
|
75
|
+
}
|
|
76
|
+
return { run: device => device.openUrl(url) };
|
|
77
|
+
}
|
|
78
|
+
// Same steps as locator.fill() in core, performed at the element's center.
|
|
79
|
+
function parseFill({ x, y, text }) {
|
|
80
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
81
|
+
return { error: 'x and y must be numbers' };
|
|
82
|
+
}
|
|
83
|
+
if (typeof text !== 'string') {
|
|
84
|
+
return { error: 'text must be a string' };
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
run: async (device) => {
|
|
88
|
+
await device.screen.tap(x, y);
|
|
89
|
+
await device.driver.clearText();
|
|
90
|
+
await device.driver.typeText(text);
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function parseGeolocation({ geolocation }) {
|
|
95
|
+
if (geolocation !== null && !isValidGeolocation(geolocation)) {
|
|
96
|
+
return { error: 'geolocation must be null or { latitude: -90..90, longitude: -180..180 }' };
|
|
97
|
+
}
|
|
98
|
+
return { run: device => device.setGeolocation(geolocation) };
|
|
99
|
+
}
|
|
100
|
+
function isValidGeolocation(value) {
|
|
101
|
+
const { latitude, longitude } = (value ?? {});
|
|
102
|
+
return typeof latitude === 'number' && latitude >= -90 && latitude <= 90
|
|
103
|
+
&& typeof longitude === 'number' && longitude >= -180 && longitude <= 180;
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=device-actions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device-actions.js","sourceRoot":"","sources":["../../src/routes/device-actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAuB,MAAM,SAAS,CAAC;AAGtD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAEtE,8FAA8F;AAC9F,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,WAAW,EAAE,WAAW,CAAU,CAAC;AAGhE,sDAAsD;AACtD,MAAM,gBAAgB,GAA8B,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;AAOn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,CAAC,SAAS,EAAE,CAAC;YAChC,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACrC,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,gBAAgB,CAAC,EAAE,WAAW,EAAe;IACpD,IAAI,WAAW,KAAK,IAAI,IAAI,CAAC,kBAAkB,CAAC,WAAW,CAAC,EAAE,CAAC;QAC7D,OAAO,EAAE,KAAK,EAAE,yEAAyE,EAAE,CAAC;IAC9F,CAAC;IACD,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE,CAAC;AAC/D,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAc;IACxC,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,CAAC,KAAK,IAAI,EAAE,CAAgD,CAAC;IAC7F,OAAO,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,IAAI,CAAC,EAAE,IAAI,QAAQ,IAAI,EAAE;WACnE,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,IAAI,CAAC,GAAG,IAAI,SAAS,IAAI,GAAG,CAAC;AAC9E,CAAC"}
|
package/dist/routes/devices.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DeviceManager } from '../lib/device-manager.js';
|
|
2
2
|
/**
|
|
3
3
|
* Express router for device listing and selection.
|
|
4
|
-
* GET /api/devices, POST /api/devices
|
|
4
|
+
* GET /api/devices, POST /api/devices/select?device=<id>
|
|
5
5
|
*/
|
|
6
6
|
export declare function createDevicesRouter(deviceManager: DeviceManager): import("express-serve-static-core").Router;
|
|
7
7
|
//# sourceMappingURL=devices.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devices.d.ts","sourceRoot":"","sources":["../../src/routes/devices.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEtE;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"devices.d.ts","sourceRoot":"","sources":["../../src/routes/devices.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEtE;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,aAAa,8CAoC/D"}
|
package/dist/routes/devices.js
CHANGED
|
@@ -2,7 +2,7 @@ import { Router } from 'express';
|
|
|
2
2
|
import { DeviceError } from '../lib/device-manager.js';
|
|
3
3
|
/**
|
|
4
4
|
* Express router for device listing and selection.
|
|
5
|
-
* GET /api/devices, POST /api/devices
|
|
5
|
+
* GET /api/devices, POST /api/devices/select?device=<id>
|
|
6
6
|
*/
|
|
7
7
|
export function createDevicesRouter(deviceManager) {
|
|
8
8
|
const router = Router();
|
|
@@ -16,25 +16,24 @@ export function createDevicesRouter(deviceManager) {
|
|
|
16
16
|
res.status(500).json({ error: err.message });
|
|
17
17
|
}
|
|
18
18
|
});
|
|
19
|
-
// POST /api/devices
|
|
20
|
-
router.post('
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
res.status(400).json({ error: 'platform must be \'ios\' or \'android\'' });
|
|
19
|
+
// POST /api/devices/select?device=<id> — the platform comes from the device list
|
|
20
|
+
router.post('/select', async (req, res) => {
|
|
21
|
+
const id = req.query.device;
|
|
22
|
+
if (typeof id !== 'string' || id === '') {
|
|
23
|
+
res.status(400).json({ error: 'device query parameter is required' });
|
|
25
24
|
return;
|
|
26
25
|
}
|
|
27
26
|
try {
|
|
28
|
-
const
|
|
29
|
-
if (!
|
|
27
|
+
const device = (await deviceManager.listDevices()).find(d => d.id === id);
|
|
28
|
+
if (!device) {
|
|
30
29
|
res.status(404).json({ error: `Device '${id}' not found` });
|
|
31
30
|
return;
|
|
32
31
|
}
|
|
33
|
-
await deviceManager.select(id, platform);
|
|
32
|
+
await deviceManager.select(id, device.platform);
|
|
34
33
|
res.json({ ok: true });
|
|
35
34
|
}
|
|
36
35
|
catch (err) {
|
|
37
|
-
const status = err instanceof DeviceError &&
|
|
36
|
+
const status = err instanceof DeviceError && err.code === 'in_progress' ? 409 : 500;
|
|
38
37
|
res.status(status).json({ error: err.message });
|
|
39
38
|
}
|
|
40
39
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devices.js","sourceRoot":"","sources":["../../src/routes/devices.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAEtE;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,aAA4B;IAC9D,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC;IAExB,mBAAmB;IACnB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE;QAClC,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,MAAM,aAAa,CAAC,WAAW,EAAE,CAAC;YAClD,GAAG,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,CAAC,UAAU,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;QACxE,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAG,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,
|
|
1
|
+
{"version":3,"file":"devices.js","sourceRoot":"","sources":["../../src/routes/devices.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAEtE;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,aAA4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,CAAC;YACD,MAAM,aAAa,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;YAChD,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;QACzB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,MAAM,GAAG,GAAG,YAAY,WAAW,IAAI,GAAG,CAAC,IAAI,KAAK,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;YACpF,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAG,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;QAC7D,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inspect.d.ts","sourceRoot":"","sources":["../../src/routes/inspect.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"inspect.d.ts","sourceRoot":"","sources":["../../src/routes/inspect.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,aAAa,8CAiD/D"}
|
package/dist/routes/inspect.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
|
-
import {
|
|
2
|
+
import { inspectScreen, SCREENSHOT_MIME_TYPE } from '../lib/inspect-screen.js';
|
|
3
3
|
import { logger } from '../lib/logger.js';
|
|
4
|
-
const MAX_RETRIES = 3;
|
|
5
|
-
const RETRY_DELAY_MS = 1500;
|
|
6
|
-
const OP_TIMEOUT_MS = 10_000;
|
|
7
4
|
/**
|
|
8
5
|
* Express router for the inspect endpoint.
|
|
9
6
|
* GET /api/inspect — returns screenshot + element list from the same device moment.
|
|
@@ -12,41 +9,36 @@ export function createInspectRouter(deviceManager) {
|
|
|
12
9
|
const router = Router();
|
|
13
10
|
// GET /api/inspect
|
|
14
11
|
// Returns screenshot + element list from the same moment (no drift).
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
// GET /api/inspect?scale=0.5 — optional screenshot downscale factor, 0 < scale <= 1.
|
|
13
|
+
// GET /api/inspect?etag=<etag> — 304 with no body when the capture still hashes to <etag>.
|
|
14
|
+
router.get('/inspect', async (req, res) => {
|
|
15
|
+
const scale = req.query.scale === undefined ? 1 : Number(req.query.scale);
|
|
16
|
+
if (!(scale > 0 && scale <= 1)) {
|
|
17
|
+
res.status(400).json({ error: 'scale must be a number between 0 (exclusive) and 1' });
|
|
19
18
|
return;
|
|
20
19
|
}
|
|
20
|
+
// Claim the inspect slot before looking at the device: mid-switch there is briefly no device,
|
|
21
|
+
// and that must read as "busy" (503), not "disconnected" (409).
|
|
21
22
|
if (!deviceManager.beginInspect()) {
|
|
22
23
|
res.status(503).json({ error: 'Inspect already in progress' });
|
|
23
24
|
return;
|
|
24
25
|
}
|
|
25
26
|
try {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
placeholder: node.placeholder ?? null,
|
|
37
|
-
value: node.value ?? null,
|
|
38
|
-
isEnabled: node.isEnabled ?? true,
|
|
39
|
-
isSelected: node.isSelected ?? null,
|
|
40
|
-
isFocused: node.isFocused ?? null,
|
|
41
|
-
isChecked: node.isChecked ?? null,
|
|
42
|
-
raw: node.raw ?? null,
|
|
43
|
-
locator,
|
|
44
|
-
locators,
|
|
45
|
-
}));
|
|
27
|
+
const device = deviceManager.device;
|
|
28
|
+
if (!device) {
|
|
29
|
+
res.status(409).json({ error: 'No device selected' });
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const { screenshot, screen, elements, etag } = await inspectScreen(device, scale, () => deviceManager.screenSize());
|
|
33
|
+
if (req.query.etag === etag) {
|
|
34
|
+
res.status(304).end();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
46
37
|
res.json({
|
|
47
|
-
screenshot: `data
|
|
48
|
-
screen
|
|
38
|
+
screenshot: `data:${SCREENSHOT_MIME_TYPE};base64,${screenshot.toString('base64')}`,
|
|
39
|
+
screen,
|
|
49
40
|
elements,
|
|
41
|
+
etag,
|
|
50
42
|
});
|
|
51
43
|
}
|
|
52
44
|
catch (err) {
|
|
@@ -59,66 +51,4 @@ export function createInspectRouter(deviceManager) {
|
|
|
59
51
|
});
|
|
60
52
|
return router;
|
|
61
53
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Run screenshot + viewTree + screenSize, retrying up to MAX_RETRIES.
|
|
64
|
-
*
|
|
65
|
-
* Strategy: first attempt runs all 3 in parallel for speed. Retries
|
|
66
|
-
* run only the failed ops sequentially (1 at a time) so that abandoned
|
|
67
|
-
* RPCs from timed-out attempts never stack up more than 2 concurrent
|
|
68
|
-
* mobilecli worker slots per retry cycle.
|
|
69
|
-
*/
|
|
70
|
-
async function attemptWithRetry(device) {
|
|
71
|
-
const ops = [
|
|
72
|
-
{ key: 'screenshotBuffer', run: () => device.screen.screenshot() },
|
|
73
|
-
{ key: 'tree', run: () => device.screen.viewTree() },
|
|
74
|
-
{ key: 'size', run: () => device.screenSize() },
|
|
75
|
-
];
|
|
76
|
-
const results = {};
|
|
77
|
-
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
|
|
78
|
-
const pending = ops.filter(op => !(op.key in results));
|
|
79
|
-
if (pending.length === 0)
|
|
80
|
-
break;
|
|
81
|
-
if (attempt === 0) {
|
|
82
|
-
const settled = await Promise.allSettled(pending.map(op => timeoutAfter(op.run(), OP_TIMEOUT_MS)));
|
|
83
|
-
for (let i = 0; i < settled.length; i++) {
|
|
84
|
-
const result = settled[i];
|
|
85
|
-
if (result.status === 'fulfilled')
|
|
86
|
-
results[pending[i].key] = result.value;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
for (const op of pending) {
|
|
91
|
-
try {
|
|
92
|
-
results[op.key] = await timeoutAfter(op.run(), OP_TIMEOUT_MS);
|
|
93
|
-
}
|
|
94
|
-
catch (err) {
|
|
95
|
-
logger.warn(`Inspect ${op.key} attempt ${attempt + 1}/${MAX_RETRIES} failed: ${err.message}`);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const stillPending = ops.filter(op => !(op.key in results));
|
|
100
|
-
if (stillPending.length > 0 && attempt < MAX_RETRIES - 1) {
|
|
101
|
-
await new Promise(r => setTimeout(r, RETRY_DELAY_MS));
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const missing = ops.filter(op => !(op.key in results));
|
|
105
|
-
if (missing.length > 0) {
|
|
106
|
-
throw new Error(`Inspect failed: ${missing.map(o => o.key).join(', ')} did not complete`);
|
|
107
|
-
}
|
|
108
|
-
return {
|
|
109
|
-
screenshotBuffer: results.screenshotBuffer,
|
|
110
|
-
tree: results.tree,
|
|
111
|
-
size: results.size,
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
/** Reject a promise if it doesn't settle within ms. */
|
|
115
|
-
function timeoutAfter(promise, ms) {
|
|
116
|
-
if (ms <= 0)
|
|
117
|
-
return Promise.reject(new Error('Deadline exceeded'));
|
|
118
|
-
let timer;
|
|
119
|
-
return Promise.race([
|
|
120
|
-
promise,
|
|
121
|
-
new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(`Operation timed out after ${ms}ms`)), ms); }),
|
|
122
|
-
]).finally(() => clearTimeout(timer));
|
|
123
|
-
}
|
|
124
54
|
//# sourceMappingURL=inspect.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inspect.js","sourceRoot":"","sources":["../../src/routes/inspect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SA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IAAI,CAAC,EAAE,KAAK,EAAE,oBAAoB,EAAE,CAAC,CAAC;gBACtD,OAAO;YACT,CAAC;YAED,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,MAAM,aAAa,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,aAAa,CAAC,UAAU,EAAE,CAAC,CAAC;YACpH,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;gBAC5B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;gBACtB,OAAO;YACT,CAAC;YAED,GAAG,CAAC,IAAI,CAAC;gBACP,UAAU,EAAE,QAAQ,oBAAoB,WAAW,UAAU,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;gBAClF,MAAM;gBACN,QAAQ;gBACR,IAAI;aACL,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,CAAC,KAAK,CAAC,mBAAoB,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;YAC1D,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAG,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;QAC1D,CAAC;gBAAS,CAAC;YACT,aAAa,CAAC,UAAU,EAAE,CAAC;QAC7B,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mobilewright/inspector",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.62",
|
|
4
4
|
"description": "Browser-based element inspector for Mobilewright",
|
|
5
5
|
"homepage": "https://mobilewright.dev",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"public"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@mobilewright/core": "^0.0.
|
|
35
|
-
"@mobilewright/protocol": "^0.0.
|
|
34
|
+
"@mobilewright/core": "^0.0.62",
|
|
35
|
+
"@mobilewright/protocol": "^0.0.62",
|
|
36
36
|
"express": "^5.2.1",
|
|
37
37
|
"open": "^10.2.0"
|
|
38
38
|
},
|