@midscene/harmony 1.17.8-beta-20261106101231.0 → 1.17.8
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/es/140.mjs +100 -0
- package/dist/es/380.mjs +185 -0
- package/dist/es/430.mjs +112 -0
- package/dist/es/600.mjs +1139 -0
- package/dist/es/bin.mjs +3 -1381
- package/dist/es/cli.mjs +3 -1318
- package/dist/es/index.mjs +5 -1502
- package/dist/es/test-runner.mjs +1 -100
- package/dist/lib/bin.js +29 -0
- package/dist/lib/cli.js +30 -1
- package/dist/lib/index.js +30 -1
- package/dist/lib/test-runner.js +1 -1
- package/dist/types/index.d.ts +1 -0
- package/package.json +5 -5
- package/static/index.html +1 -1
- package/static/static/js/208.eb5899a1.js +314 -0
- package/static/static/js/{998.dbb78266.js.LICENSE.txt → 208.eb5899a1.js.LICENSE.txt} +0 -2
- package/static/static/js/208.eb5899a1.js.map +1 -0
- package/static/static/js/async/195.430205e7.js +3 -0
- package/static/static/js/async/{195.beaced02.js.map → 195.430205e7.js.map} +1 -1
- package/static/static/js/async/236.e8e04e6d.js +2 -0
- package/static/static/js/async/{236.a5d2c1b1.js.map → 236.e8e04e6d.js.map} +1 -1
- package/static/static/js/index.adbc3a23.js +887 -0
- package/static/static/js/index.adbc3a23.js.map +1 -0
- package/static/static/js/lib-react.d4684574.js +3 -0
- package/static/static/js/{lib-react.8bb5dc6e.js.map → lib-react.d4684574.js.map} +1 -1
- package/static/static/js/998.dbb78266.js +0 -315
- package/static/static/js/998.dbb78266.js.map +0 -1
- package/static/static/js/async/195.beaced02.js +0 -3
- package/static/static/js/async/236.a5d2c1b1.js +0 -2
- package/static/static/js/index.dced2504.js +0 -887
- package/static/static/js/index.dced2504.js.map +0 -1
- package/static/static/js/lib-react.8bb5dc6e.js +0 -3
- /package/static/static/js/{index.dced2504.js.LICENSE.txt → index.adbc3a23.js.LICENSE.txt} +0 -0
- /package/static/static/js/{lib-react.8bb5dc6e.js.LICENSE.txt → lib-react.d4684574.js.LICENSE.txt} +0 -0
package/dist/es/600.mjs
ADDED
|
@@ -0,0 +1,1139 @@
|
|
|
1
|
+
import node_assert from "node:assert";
|
|
2
|
+
import node_fs, { accessSync, constants } from "node:fs";
|
|
3
|
+
import { createReportCliCommands, z } from "@midscene/core";
|
|
4
|
+
import { createDefaultMobileActions, defineAction, resolveDefaultMobileScroll, resolveTextInputOptions, sendTextSequentially, shouldInputSequentially } from "@midscene/core/device";
|
|
5
|
+
import { getTmpFile, sleep } from "@midscene/core/utils";
|
|
6
|
+
import { createImgBase64ByFormat } from "@midscene/shared/img";
|
|
7
|
+
import { getDebug } from "@midscene/shared/logger";
|
|
8
|
+
import { mergeAndNormalizeAppNameMapping, normalizeForComparison, repeat } from "@midscene/shared/utils";
|
|
9
|
+
import { execFile } from "node:child_process";
|
|
10
|
+
import { promisify } from "node:util";
|
|
11
|
+
import { Agent } from "@midscene/core/agent";
|
|
12
|
+
import { harmonyAgentTestRunnerNodeDefinitions } from "./140.mjs";
|
|
13
|
+
const uiInputSpeedRange = {
|
|
14
|
+
min: 200,
|
|
15
|
+
max: 40000
|
|
16
|
+
};
|
|
17
|
+
function _define_property(obj, key, value) {
|
|
18
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
19
|
+
value: value,
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true
|
|
23
|
+
});
|
|
24
|
+
else obj[key] = value;
|
|
25
|
+
return obj;
|
|
26
|
+
}
|
|
27
|
+
const execFileAsync = promisify(execFile);
|
|
28
|
+
const debugHdc = getDebug('harmony:hdc');
|
|
29
|
+
const supportedStringKeyEvents = new Set([
|
|
30
|
+
'Back',
|
|
31
|
+
'Home',
|
|
32
|
+
'Power'
|
|
33
|
+
]);
|
|
34
|
+
const numericKeyEventPattern = /^\d+$/;
|
|
35
|
+
const uiInputErrorPattern = /(?:(?:Invalid parameters|Missing parameter|Too many parameters)\.?|Please confirm that the coordinate values are correct\.?)/i;
|
|
36
|
+
function roundUiCoordinate(value, name) {
|
|
37
|
+
if (!Number.isFinite(value) || value < 0) throw new Error(`HDC ${name} coordinate must be a non-negative finite number`);
|
|
38
|
+
return Math.round(value);
|
|
39
|
+
}
|
|
40
|
+
function roundUiInputSpeed(value, operation) {
|
|
41
|
+
if (!Number.isFinite(value) || value < uiInputSpeedRange.min || value > uiInputSpeedRange.max) throw new Error(`HDC ${operation} speed must be a finite number between ${uiInputSpeedRange.min} and ${uiInputSpeedRange.max}`);
|
|
42
|
+
return Math.round(value);
|
|
43
|
+
}
|
|
44
|
+
function parseBundleInfo(output, bundleName) {
|
|
45
|
+
const jsonStart = output.indexOf('{');
|
|
46
|
+
if (jsonStart < 0) throw new Error(`Cannot parse bundle information for ${bundleName}`);
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(output.slice(jsonStart));
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw new Error(`Cannot parse bundle information for ${bundleName}`, {
|
|
51
|
+
cause: error
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function isLauncherAbility(ability) {
|
|
56
|
+
return Boolean(ability.skills?.some((skill)=>skill.entities?.includes('entity.system.home') && skill.actions?.some((action)=>'action.system.home' === action || 'ohos.want.action.home' === action)));
|
|
57
|
+
}
|
|
58
|
+
function resolveModuleName(module) {
|
|
59
|
+
return [
|
|
60
|
+
module.moduleName,
|
|
61
|
+
module.name
|
|
62
|
+
].find((name)=>Boolean(name));
|
|
63
|
+
}
|
|
64
|
+
function componentNamesMatch(left, right) {
|
|
65
|
+
return left === right || left.endsWith(`.${right}`) || right.endsWith(`.${left}`);
|
|
66
|
+
}
|
|
67
|
+
function declaredTargetFromModule(module) {
|
|
68
|
+
const moduleName = resolveModuleName(module);
|
|
69
|
+
if (!moduleName) return;
|
|
70
|
+
const abilities = module.abilityInfos ?? [];
|
|
71
|
+
const declaredAbilityName = [
|
|
72
|
+
module.mainElementName,
|
|
73
|
+
module.mainAbility
|
|
74
|
+
].find((name)=>Boolean(name));
|
|
75
|
+
if (declaredAbilityName) {
|
|
76
|
+
const matchedAbility = abilities.find((ability)=>ability.name && componentNamesMatch(ability.name, declaredAbilityName));
|
|
77
|
+
const isDeclaredExtension = module.extensionInfos?.some((extension)=>extension.name && componentNamesMatch(extension.name, declaredAbilityName));
|
|
78
|
+
if (!matchedAbility && (abilities.length > 0 || isDeclaredExtension)) return;
|
|
79
|
+
return {
|
|
80
|
+
abilityName: matchedAbility?.name ?? declaredAbilityName,
|
|
81
|
+
moduleName
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function launcherTargetFromModule(module) {
|
|
86
|
+
const moduleName = resolveModuleName(module);
|
|
87
|
+
if (!moduleName) return;
|
|
88
|
+
const launcherAbility = module.abilityInfos?.find(isLauncherAbility);
|
|
89
|
+
return launcherAbility?.name ? {
|
|
90
|
+
abilityName: launcherAbility.name,
|
|
91
|
+
moduleName
|
|
92
|
+
} : void 0;
|
|
93
|
+
}
|
|
94
|
+
function resolveHdcPath(hdcPath) {
|
|
95
|
+
if (hdcPath) return hdcPath;
|
|
96
|
+
if (process.env.HDC_HOME) {
|
|
97
|
+
const envPath = `${process.env.HDC_HOME}/hdc`;
|
|
98
|
+
debugHdc(`Using HDC from HDC_HOME: ${envPath}`);
|
|
99
|
+
return envPath;
|
|
100
|
+
}
|
|
101
|
+
const homeDir = process.env.HOME || process.env.USERPROFILE || '';
|
|
102
|
+
const commonPaths = [
|
|
103
|
+
`${homeDir}/Library/HarmonyOS/next/command-line-tools/sdk/default/openharmony/toolchains/hdc`,
|
|
104
|
+
`${homeDir}/Library/HarmonyOS/sdk/hmscore/3.1.0/toolchains/hdc`
|
|
105
|
+
];
|
|
106
|
+
for (const p of commonPaths)try {
|
|
107
|
+
accessSync(p, constants.X_OK);
|
|
108
|
+
debugHdc(`Found HDC at: ${p}`);
|
|
109
|
+
return p;
|
|
110
|
+
} catch {}
|
|
111
|
+
return 'hdc';
|
|
112
|
+
}
|
|
113
|
+
class HdcClient {
|
|
114
|
+
buildArgs(args) {
|
|
115
|
+
if (this.deviceId) return [
|
|
116
|
+
'-t',
|
|
117
|
+
this.deviceId,
|
|
118
|
+
...args
|
|
119
|
+
];
|
|
120
|
+
return args;
|
|
121
|
+
}
|
|
122
|
+
async exec(...args) {
|
|
123
|
+
let release;
|
|
124
|
+
const prev = this.execMutex;
|
|
125
|
+
this.execMutex = new Promise((r)=>{
|
|
126
|
+
release = r;
|
|
127
|
+
});
|
|
128
|
+
await prev;
|
|
129
|
+
const fullArgs = this.buildArgs(args);
|
|
130
|
+
debugHdc(`hdc ${fullArgs.join(' ')}`);
|
|
131
|
+
try {
|
|
132
|
+
const { stdout, stderr } = await execFileAsync(this.hdcPath, fullArgs, {
|
|
133
|
+
timeout: this.timeout,
|
|
134
|
+
maxBuffer: 52428800
|
|
135
|
+
});
|
|
136
|
+
if (stderr?.trim()) debugHdc(`hdc stderr: ${stderr.trim()}`);
|
|
137
|
+
debugHdc(`hdc ${fullArgs.join(' ')} end`);
|
|
138
|
+
return stdout;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
if (error.killed && error.stdout?.trim()) {
|
|
141
|
+
debugHdc('hdc process was killed but stdout is available, treating as success');
|
|
142
|
+
return error.stdout;
|
|
143
|
+
}
|
|
144
|
+
debugHdc(`hdc error: ${error.message}`);
|
|
145
|
+
throw new Error(`HDC command failed: hdc ${fullArgs.join(' ')}: ${error.message}`, {
|
|
146
|
+
cause: error
|
|
147
|
+
});
|
|
148
|
+
} finally{
|
|
149
|
+
release();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async shell(command) {
|
|
153
|
+
return this.exec('shell', command);
|
|
154
|
+
}
|
|
155
|
+
assertUiInputSucceeded(operation, output, detail) {
|
|
156
|
+
const errorMatch = output.match(uiInputErrorPattern);
|
|
157
|
+
if (!errorMatch) return;
|
|
158
|
+
const detailText = detail ? ` for ${detail}` : '';
|
|
159
|
+
throw new Error(`HDC uiInput ${operation} failed${detailText}: ${errorMatch[0]}`);
|
|
160
|
+
}
|
|
161
|
+
async runUiInput(operation, args, detail) {
|
|
162
|
+
const output = await this.shell(`uitest uiInput ${operation} ${args}`);
|
|
163
|
+
this.assertUiInputSucceeded(operation, output, detail);
|
|
164
|
+
}
|
|
165
|
+
buildPointerMovementArgs(operation, fromX, fromY, toX, toY, speed) {
|
|
166
|
+
const args = [
|
|
167
|
+
roundUiCoordinate(fromX, 'fromX'),
|
|
168
|
+
roundUiCoordinate(fromY, 'fromY'),
|
|
169
|
+
roundUiCoordinate(toX, 'toX'),
|
|
170
|
+
roundUiCoordinate(toY, 'toY')
|
|
171
|
+
];
|
|
172
|
+
if (void 0 !== speed) args.push(roundUiInputSpeed(speed, operation));
|
|
173
|
+
return args.join(' ');
|
|
174
|
+
}
|
|
175
|
+
async fileSend(localPath, remotePath) {
|
|
176
|
+
await this.exec('file', 'send', localPath, remotePath);
|
|
177
|
+
}
|
|
178
|
+
async fileRecv(remotePath, localPath) {
|
|
179
|
+
await this.exec('file', 'recv', remotePath, localPath);
|
|
180
|
+
}
|
|
181
|
+
async screenshot(remotePath) {
|
|
182
|
+
return await this.shell(`snapshot_display -f ${remotePath}`);
|
|
183
|
+
}
|
|
184
|
+
async click(x, y) {
|
|
185
|
+
await this.runUiInput('click', `${roundUiCoordinate(x, 'x')} ${roundUiCoordinate(y, 'y')}`);
|
|
186
|
+
}
|
|
187
|
+
async doubleClick(x, y) {
|
|
188
|
+
await this.runUiInput('doubleClick', `${roundUiCoordinate(x, 'x')} ${roundUiCoordinate(y, 'y')}`);
|
|
189
|
+
}
|
|
190
|
+
async longClick(x, y) {
|
|
191
|
+
await this.runUiInput('longClick', `${roundUiCoordinate(x, 'x')} ${roundUiCoordinate(y, 'y')}`);
|
|
192
|
+
}
|
|
193
|
+
async swipe(fromX, fromY, toX, toY, speed) {
|
|
194
|
+
await this.runUiInput('swipe', this.buildPointerMovementArgs('swipe', fromX, fromY, toX, toY, speed));
|
|
195
|
+
}
|
|
196
|
+
async fling(fromX, fromY, toX, toY, speed) {
|
|
197
|
+
await this.runUiInput('fling', this.buildPointerMovementArgs('fling', fromX, fromY, toX, toY, speed));
|
|
198
|
+
}
|
|
199
|
+
async drag(fromX, fromY, toX, toY, speed) {
|
|
200
|
+
await this.runUiInput('drag', this.buildPointerMovementArgs('drag', fromX, fromY, toX, toY, speed));
|
|
201
|
+
}
|
|
202
|
+
async inputText(x, y, text) {
|
|
203
|
+
const escapedText = text.replace(/'/g, "'\\''");
|
|
204
|
+
await this.runUiInput('inputText', `${roundUiCoordinate(x, 'x')} ${roundUiCoordinate(y, 'y')} '${escapedText}'`);
|
|
205
|
+
}
|
|
206
|
+
async keyEvent(...keys) {
|
|
207
|
+
if (keys.length < 1 || keys.length > 3) throw new Error('HDC keyEvent requires between 1 and 3 keys');
|
|
208
|
+
if (keys.length > 1 && keys.some((key)=>supportedStringKeyEvents.has(key))) throw new Error('HDC system key events cannot be combined');
|
|
209
|
+
for (const key of keys)if (!supportedStringKeyEvents.has(key) && !numericKeyEventPattern.test(key)) throw new Error(`Invalid HDC key event: ${key}`);
|
|
210
|
+
const joinedKeys = keys.join(' ');
|
|
211
|
+
await this.runUiInput('keyEvent', joinedKeys, joinedKeys);
|
|
212
|
+
}
|
|
213
|
+
async clearTextField() {
|
|
214
|
+
const output = await this.shell('uitest uiInput keyEvent 2072 2017;uitest uiInput keyEvent 2055');
|
|
215
|
+
this.assertUiInputSucceeded('clearTextField', output);
|
|
216
|
+
}
|
|
217
|
+
async startAbility(bundleName, abilityName, moduleName) {
|
|
218
|
+
const moduleArg = moduleName ? ` -m ${moduleName}` : '';
|
|
219
|
+
const output = await this.shell(`aa start -a ${abilityName} -b ${bundleName}${moduleArg}`);
|
|
220
|
+
if (output.includes('error:')) throw new Error(`Failed to start ${bundleName}/${abilityName}: ${output.trim()}`);
|
|
221
|
+
}
|
|
222
|
+
async queryLaunchAbility(bundleName) {
|
|
223
|
+
const output = await this.shell(`bm dump -n ${bundleName}`);
|
|
224
|
+
const bundleInfo = parseBundleInfo(output, bundleName);
|
|
225
|
+
const modules = bundleInfo.hapModuleInfos ?? [];
|
|
226
|
+
const entryModuleNames = new Set([
|
|
227
|
+
bundleInfo.entryModuleName,
|
|
228
|
+
bundleInfo.mainEntry
|
|
229
|
+
].filter((name)=>Boolean(name)));
|
|
230
|
+
const preferredModules = modules.filter((module)=>entryModuleNames.has(module.moduleName ?? '') || entryModuleNames.has(module.name ?? ''));
|
|
231
|
+
const entryModules = modules.filter((module)=>!preferredModules.includes(module) && (1 === module.moduleType || 'entry' === module.moduleType));
|
|
232
|
+
const remainingModules = modules.filter((module)=>!preferredModules.includes(module) && !entryModules.includes(module));
|
|
233
|
+
const entryModulesByPriority = [
|
|
234
|
+
...preferredModules,
|
|
235
|
+
...entryModules
|
|
236
|
+
];
|
|
237
|
+
for (const module of entryModulesByPriority){
|
|
238
|
+
const target = declaredTargetFromModule(module);
|
|
239
|
+
if (target) return target;
|
|
240
|
+
}
|
|
241
|
+
for (const module of entryModulesByPriority){
|
|
242
|
+
const target = launcherTargetFromModule(module);
|
|
243
|
+
if (target) return target;
|
|
244
|
+
}
|
|
245
|
+
for (const module of remainingModules){
|
|
246
|
+
const target = launcherTargetFromModule(module);
|
|
247
|
+
if (target) return target;
|
|
248
|
+
}
|
|
249
|
+
for (const module of remainingModules){
|
|
250
|
+
const target = declaredTargetFromModule(module);
|
|
251
|
+
if (target) return target;
|
|
252
|
+
}
|
|
253
|
+
throw new Error(`Cannot find a launchable ability for ${bundleName}`);
|
|
254
|
+
}
|
|
255
|
+
async launchBundle(bundleName) {
|
|
256
|
+
const cachedTarget = this.launchAbilityCache.get(bundleName);
|
|
257
|
+
const target = cachedTarget ?? await this.queryLaunchAbility(bundleName);
|
|
258
|
+
try {
|
|
259
|
+
await this.startAbility(bundleName, target.abilityName, target.moduleName);
|
|
260
|
+
this.launchAbilityCache.set(bundleName, target);
|
|
261
|
+
} catch (error) {
|
|
262
|
+
this.launchAbilityCache.delete(bundleName);
|
|
263
|
+
if (!cachedTarget) throw error;
|
|
264
|
+
const refreshedTarget = await this.queryLaunchAbility(bundleName);
|
|
265
|
+
if (refreshedTarget.abilityName === cachedTarget.abilityName && refreshedTarget.moduleName === cachedTarget.moduleName) throw error;
|
|
266
|
+
debugHdc(`Cached launch ability changed from ${cachedTarget.moduleName}/${cachedTarget.abilityName} to ${refreshedTarget.moduleName}/${refreshedTarget.abilityName}`);
|
|
267
|
+
await this.startAbility(bundleName, refreshedTarget.abilityName, refreshedTarget.moduleName);
|
|
268
|
+
this.launchAbilityCache.set(bundleName, refreshedTarget);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async forceStop(bundleName) {
|
|
272
|
+
const output = await this.shell(`aa force-stop ${bundleName}`);
|
|
273
|
+
if (output.includes('error:')) throw new Error(`Failed to force stop ${bundleName}: ${output.trim()}`);
|
|
274
|
+
}
|
|
275
|
+
async getScreenInfo() {
|
|
276
|
+
const stdout = await this.shell('hidumper -s RenderService -a screen');
|
|
277
|
+
const renderDimensionPattern = 'render\\s+(?:size|resolution)\\s*[:=]\\s*(\\d{3,5})x(\\d{3,5})';
|
|
278
|
+
const activeFoldMatch = stdout.match(/foldScreenId:(\d+),\s*isConnected:\d+,\s*isPowerOn:1/);
|
|
279
|
+
if (activeFoldMatch) {
|
|
280
|
+
const activeId = activeFoldMatch[1];
|
|
281
|
+
const screenRegex = new RegExp(`screen\\[\\d+\\]:\\s*id=${activeId},.*?${renderDimensionPattern}`);
|
|
282
|
+
const screenMatch = stdout.match(screenRegex);
|
|
283
|
+
if (screenMatch) {
|
|
284
|
+
debugHdc(`Foldable screen detected, active screen id=${activeId}: ${screenMatch[1]}x${screenMatch[2]}`);
|
|
285
|
+
return {
|
|
286
|
+
width: Number.parseInt(screenMatch[1], 10),
|
|
287
|
+
height: Number.parseInt(screenMatch[2], 10)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const renderSizeMatch = stdout.match(new RegExp(renderDimensionPattern));
|
|
292
|
+
if (renderSizeMatch) return {
|
|
293
|
+
width: Number.parseInt(renderSizeMatch[1], 10),
|
|
294
|
+
height: Number.parseInt(renderSizeMatch[2], 10)
|
|
295
|
+
};
|
|
296
|
+
const displayStdout = await this.shell('hidumper -s DisplayManagerService -a');
|
|
297
|
+
const displayMatch = displayStdout.match(/activeModes.*?(\d{3,5}),\s*(\d{3,5})/);
|
|
298
|
+
if (displayMatch) return {
|
|
299
|
+
width: Number.parseInt(displayMatch[1], 10),
|
|
300
|
+
height: Number.parseInt(displayMatch[2], 10)
|
|
301
|
+
};
|
|
302
|
+
throw new Error(`Failed to get screen size from HDC. RenderService output: ${stdout}`);
|
|
303
|
+
}
|
|
304
|
+
async listTargets() {
|
|
305
|
+
const stdout = await this.exec('list', 'targets');
|
|
306
|
+
return stdout.trim().split('\n').map((line)=>line.trim()).filter((line)=>line.length > 0 && !line.startsWith('['));
|
|
307
|
+
}
|
|
308
|
+
constructor(options){
|
|
309
|
+
_define_property(this, "hdcPath", void 0);
|
|
310
|
+
_define_property(this, "deviceId", void 0);
|
|
311
|
+
_define_property(this, "timeout", void 0);
|
|
312
|
+
_define_property(this, "execMutex", Promise.resolve());
|
|
313
|
+
_define_property(this, "launchAbilityCache", new Map());
|
|
314
|
+
this.hdcPath = resolveHdcPath(options.hdcPath);
|
|
315
|
+
this.deviceId = options.deviceId ?? '';
|
|
316
|
+
this.timeout = options.timeout ?? 60000;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
const namedHarmonyKeyCodeMap = {
|
|
320
|
+
Enter: '2054',
|
|
321
|
+
Backspace: '2055',
|
|
322
|
+
Tab: '2049',
|
|
323
|
+
Escape: '2070',
|
|
324
|
+
Home: 'Home',
|
|
325
|
+
ArrowUp: '2012',
|
|
326
|
+
ArrowDown: '2013',
|
|
327
|
+
ArrowLeft: '2014',
|
|
328
|
+
ArrowRight: '2015',
|
|
329
|
+
Space: '2050',
|
|
330
|
+
Delete: '2071',
|
|
331
|
+
Alt: '2045',
|
|
332
|
+
Shift: '2047',
|
|
333
|
+
Control: '2072',
|
|
334
|
+
Meta: '2076',
|
|
335
|
+
Back: 'Back',
|
|
336
|
+
Power: 'Power'
|
|
337
|
+
};
|
|
338
|
+
const keyNameAliasMap = new Map([
|
|
339
|
+
[
|
|
340
|
+
'return',
|
|
341
|
+
'Enter'
|
|
342
|
+
],
|
|
343
|
+
[
|
|
344
|
+
'esc',
|
|
345
|
+
'Escape'
|
|
346
|
+
],
|
|
347
|
+
[
|
|
348
|
+
'up',
|
|
349
|
+
'ArrowUp'
|
|
350
|
+
],
|
|
351
|
+
[
|
|
352
|
+
'down',
|
|
353
|
+
'ArrowDown'
|
|
354
|
+
],
|
|
355
|
+
[
|
|
356
|
+
'left',
|
|
357
|
+
'ArrowLeft'
|
|
358
|
+
],
|
|
359
|
+
[
|
|
360
|
+
'right',
|
|
361
|
+
'ArrowRight'
|
|
362
|
+
],
|
|
363
|
+
[
|
|
364
|
+
'ctrl',
|
|
365
|
+
'Control'
|
|
366
|
+
]
|
|
367
|
+
]);
|
|
368
|
+
const caseInsensitiveNamedKeyCodeMap = new Map(Object.entries(namedHarmonyKeyCodeMap).map(([key, code])=>[
|
|
369
|
+
key.toLowerCase(),
|
|
370
|
+
code
|
|
371
|
+
]));
|
|
372
|
+
function numericKeyCode(value) {
|
|
373
|
+
return String(value);
|
|
374
|
+
}
|
|
375
|
+
function resolveSingleHarmonyKeyCodes(keyName) {
|
|
376
|
+
const trimmedKeyName = keyName.trim();
|
|
377
|
+
if (!trimmedKeyName) {
|
|
378
|
+
if (0 === keyName.length) throw new Error('Unsupported HarmonyOS key: empty key name');
|
|
379
|
+
throw unsupportedKeyboardPressKey(keyName);
|
|
380
|
+
}
|
|
381
|
+
const alias = keyNameAliasMap.get(trimmedKeyName.toLowerCase());
|
|
382
|
+
const normalizedKeyName = alias ?? trimmedKeyName;
|
|
383
|
+
if (/^[0-9]$/.test(normalizedKeyName)) return [
|
|
384
|
+
numericKeyCode(2000 + Number(normalizedKeyName))
|
|
385
|
+
];
|
|
386
|
+
const letterKeyMatch = normalizedKeyName.match(/^[a-z]$/i);
|
|
387
|
+
if (letterKeyMatch) {
|
|
388
|
+
const alphabetIndex = letterKeyMatch[0].toUpperCase().charCodeAt(0) - 65;
|
|
389
|
+
return [
|
|
390
|
+
numericKeyCode(2017 + alphabetIndex)
|
|
391
|
+
];
|
|
392
|
+
}
|
|
393
|
+
const namedCode = caseInsensitiveNamedKeyCodeMap.get(normalizedKeyName.toLowerCase());
|
|
394
|
+
if (namedCode) return [
|
|
395
|
+
namedCode
|
|
396
|
+
];
|
|
397
|
+
throw unsupportedKeyboardPressKey(keyName);
|
|
398
|
+
}
|
|
399
|
+
function unsupportedKeyboardPressKey(keyName) {
|
|
400
|
+
return new Error(`Unsupported HarmonyOS keyboardPress key: ${JSON.stringify(keyName)}. Use typeText() for text input, or pass a supported key name such as "Enter", "A", or "1".`);
|
|
401
|
+
}
|
|
402
|
+
function resolveHarmonyKeyCodes(keyName) {
|
|
403
|
+
const trimmedKeyName = keyName.trim();
|
|
404
|
+
if (!trimmedKeyName && ' ' !== keyName) return resolveSingleHarmonyKeyCodes(keyName);
|
|
405
|
+
if ('+' === trimmedKeyName) return resolveSingleHarmonyKeyCodes(keyName);
|
|
406
|
+
if (trimmedKeyName.includes('+')) throw new Error(`HarmonyOS keyboardPress does not support key combinations: ${JSON.stringify(keyName)}`);
|
|
407
|
+
return resolveSingleHarmonyKeyCodes(keyName);
|
|
408
|
+
}
|
|
409
|
+
const defaultSwipeDuration = 300;
|
|
410
|
+
const warnSwipe = getDebug('harmony:swipe', {
|
|
411
|
+
console: true
|
|
412
|
+
});
|
|
413
|
+
function resolveSwipeSpeed(start, end, duration = defaultSwipeDuration) {
|
|
414
|
+
if (!Number.isFinite(duration) || duration <= 0) throw new Error('Harmony swipe duration must be a positive finite number');
|
|
415
|
+
const distance = Math.hypot(end.x - start.x, end.y - start.y);
|
|
416
|
+
const requestedSpeed = 1000 * distance / duration;
|
|
417
|
+
const speed = Math.min(uiInputSpeedRange.max, Math.max(uiInputSpeedRange.min, requestedSpeed));
|
|
418
|
+
if (speed !== requestedSpeed) warnSwipe(`Harmony swipe speed ${requestedSpeed}px/s was clamped to ${speed}px/s; the requested ${duration}ms duration cannot be represented exactly`);
|
|
419
|
+
return Math.round(speed);
|
|
420
|
+
}
|
|
421
|
+
function device_define_property(obj, key, value) {
|
|
422
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
423
|
+
value: value,
|
|
424
|
+
enumerable: true,
|
|
425
|
+
configurable: true,
|
|
426
|
+
writable: true
|
|
427
|
+
});
|
|
428
|
+
else obj[key] = value;
|
|
429
|
+
return obj;
|
|
430
|
+
}
|
|
431
|
+
const defaultScrollUntilTimes = 10;
|
|
432
|
+
const defaultFastSwipeSpeed = 2000;
|
|
433
|
+
const maxScrollDistance = 9999999;
|
|
434
|
+
const scrollQuadrantDivisions = 4;
|
|
435
|
+
const screenEdgeMargin = 50;
|
|
436
|
+
const debugDevice = getDebug('harmony:device');
|
|
437
|
+
let screenshotResizeScaleWarned = false;
|
|
438
|
+
class HarmonyDevice {
|
|
439
|
+
actionSpace() {
|
|
440
|
+
const mobileActionContext = {
|
|
441
|
+
input: this.inputPrimitives,
|
|
442
|
+
size: ()=>this.size(),
|
|
443
|
+
sleep: async (timeMs)=>{
|
|
444
|
+
await sleep(timeMs);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
const defaultActions = [
|
|
448
|
+
...createDefaultMobileActions(mobileActionContext)
|
|
449
|
+
];
|
|
450
|
+
const platformSpecificActions = Object.values(createPlatformActions(this));
|
|
451
|
+
const customActions = this.customActions ?? [];
|
|
452
|
+
return [
|
|
453
|
+
...defaultActions,
|
|
454
|
+
...platformSpecificActions,
|
|
455
|
+
...customActions
|
|
456
|
+
];
|
|
457
|
+
}
|
|
458
|
+
async performActionScroll(param, opts) {
|
|
459
|
+
const element = param.locate;
|
|
460
|
+
const startingPoint = element ? {
|
|
461
|
+
left: element.center[0],
|
|
462
|
+
top: element.center[1]
|
|
463
|
+
} : void 0;
|
|
464
|
+
const scrollToEventName = param?.scrollType;
|
|
465
|
+
if ('scrollToTop' === scrollToEventName) await this.scrollUntilTop(startingPoint);
|
|
466
|
+
else if ('scrollToBottom' === scrollToEventName) await this.scrollUntilBottom(startingPoint);
|
|
467
|
+
else if ('scrollToRight' === scrollToEventName) await this.scrollUntilRight(startingPoint);
|
|
468
|
+
else if ('scrollToLeft' === scrollToEventName) await this.scrollUntilLeft(startingPoint);
|
|
469
|
+
else if ('singleAction' !== scrollToEventName && scrollToEventName) throw new Error(`Unknown scroll event type: ${scrollToEventName}, param: ${JSON.stringify(param)}`);
|
|
470
|
+
else {
|
|
471
|
+
if (param?.direction !== 'down' && param && param.direction) if ('up' === param.direction) await this.scrollUp(param.distance ?? void 0, startingPoint);
|
|
472
|
+
else if ('left' === param.direction) await this.scrollLeft(param.distance ?? void 0, startingPoint);
|
|
473
|
+
else if ('right' === param.direction) await this.scrollRight(param.distance ?? void 0, startingPoint);
|
|
474
|
+
else throw new Error(`Unknown scroll direction: ${param.direction}`);
|
|
475
|
+
else await this.scrollDown(param?.distance ?? void 0, startingPoint);
|
|
476
|
+
if (!opts?.skipDefaultWait) await sleep(500);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
describe() {
|
|
480
|
+
return this.descriptionText || `DeviceId: ${this.deviceId}`;
|
|
481
|
+
}
|
|
482
|
+
async connect() {
|
|
483
|
+
const hdc = await this.getHdc();
|
|
484
|
+
return hdc;
|
|
485
|
+
}
|
|
486
|
+
async getHdc() {
|
|
487
|
+
if (this.destroyed) throw new Error(`HarmonyDevice ${this.deviceId} has been destroyed and cannot execute HDC commands`);
|
|
488
|
+
if (this.hdc) return this.hdc;
|
|
489
|
+
if (this.connecting) return this.connecting;
|
|
490
|
+
this.connecting = (async ()=>{
|
|
491
|
+
debugDevice(`Initializing HDC with device ID: ${this.deviceId}`);
|
|
492
|
+
try {
|
|
493
|
+
this.hdc = new HdcClient({
|
|
494
|
+
hdcPath: this.options?.hdcPath,
|
|
495
|
+
deviceId: this.deviceId
|
|
496
|
+
});
|
|
497
|
+
const screenInfo = await this.hdc.getScreenInfo();
|
|
498
|
+
this.cachedScreenSize = screenInfo;
|
|
499
|
+
this.descriptionText = `DeviceId: ${this.deviceId}\nScreenSize: ${screenInfo.width}x${screenInfo.height}`;
|
|
500
|
+
debugDevice('HDC initialized successfully', this.descriptionText);
|
|
501
|
+
return this.hdc;
|
|
502
|
+
} catch (e) {
|
|
503
|
+
debugDevice(`Failed to initialize HDC: ${e}`);
|
|
504
|
+
throw new Error(`Unable to connect to device ${this.deviceId}: ${e}`);
|
|
505
|
+
} finally{
|
|
506
|
+
this.connecting = null;
|
|
507
|
+
}
|
|
508
|
+
})();
|
|
509
|
+
return this.connecting;
|
|
510
|
+
}
|
|
511
|
+
setAppNameMapping(mapping) {
|
|
512
|
+
this.appNameMapping = mergeAndNormalizeAppNameMapping({}, mapping);
|
|
513
|
+
}
|
|
514
|
+
resolveMappedAppTarget(appName) {
|
|
515
|
+
const normalizedAppName = normalizeForComparison(appName);
|
|
516
|
+
return this.appNameMapping[normalizedAppName];
|
|
517
|
+
}
|
|
518
|
+
resolveBundleNameForTerminate(target) {
|
|
519
|
+
const mappedTarget = this.resolveMappedAppTarget(target);
|
|
520
|
+
if (mappedTarget) return mappedTarget.split('/')[0];
|
|
521
|
+
const [bundleOrAppName] = target.split('/');
|
|
522
|
+
return (this.resolveMappedAppTarget(bundleOrAppName) ?? bundleOrAppName).split('/')[0];
|
|
523
|
+
}
|
|
524
|
+
async launch(uri) {
|
|
525
|
+
const hdc = await this.getHdc();
|
|
526
|
+
this.uri = uri;
|
|
527
|
+
try {
|
|
528
|
+
debugDevice(`Launching app: ${uri}`);
|
|
529
|
+
const target = uri.includes('://') ? uri : this.resolveMappedAppTarget(uri) ?? uri;
|
|
530
|
+
if (target.includes('://')) {
|
|
531
|
+
const sanitizedUri = target.replace(/[`$\\;"'|&<>(){}]/g, '');
|
|
532
|
+
await hdc.shell(`aa start -U ${sanitizedUri}`);
|
|
533
|
+
} else if (target.includes('/')) {
|
|
534
|
+
const [bundleName, abilityName] = target.split('/');
|
|
535
|
+
await hdc.startAbility(bundleName, abilityName);
|
|
536
|
+
} else await hdc.launchBundle(target);
|
|
537
|
+
debugDevice(`Successfully launched: ${uri}`);
|
|
538
|
+
} catch (error) {
|
|
539
|
+
debugDevice(`Error launching ${uri}: ${error}`);
|
|
540
|
+
throw new Error(`Failed to launch ${uri}: ${error.message}`, {
|
|
541
|
+
cause: error
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
return this;
|
|
545
|
+
}
|
|
546
|
+
async terminate(uri) {
|
|
547
|
+
const bundleName = this.resolveBundleNameForTerminate(uri);
|
|
548
|
+
const hdc = await this.getHdc();
|
|
549
|
+
try {
|
|
550
|
+
debugDevice(`Terminating app: ${bundleName}`);
|
|
551
|
+
await hdc.forceStop(bundleName);
|
|
552
|
+
debugDevice(`Successfully terminated: ${bundleName}`);
|
|
553
|
+
} catch (error) {
|
|
554
|
+
debugDevice(`Error terminating ${bundleName}: ${error}`);
|
|
555
|
+
throw new Error(`Failed to terminate ${bundleName}: ${error.message}`, {
|
|
556
|
+
cause: error
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
async getScreenSize() {
|
|
561
|
+
if (this.cachedScreenSize) return this.cachedScreenSize;
|
|
562
|
+
const hdc = await this.getHdc();
|
|
563
|
+
const screenInfo = await hdc.getScreenInfo();
|
|
564
|
+
this.cachedScreenSize = screenInfo;
|
|
565
|
+
return screenInfo;
|
|
566
|
+
}
|
|
567
|
+
async size() {
|
|
568
|
+
const screenInfo = await this.getScreenSize();
|
|
569
|
+
return {
|
|
570
|
+
width: screenInfo.width,
|
|
571
|
+
height: screenInfo.height
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
async screenshotBase64() {
|
|
575
|
+
debugDevice('screenshotBase64 begin');
|
|
576
|
+
const hdc = await this.getHdc();
|
|
577
|
+
if (!this.localScreenshotPath) this.localScreenshotPath = getTmpFile('jpeg');
|
|
578
|
+
const maxAttempts = 2;
|
|
579
|
+
for(let attempt = 1; attempt <= maxAttempts; attempt++){
|
|
580
|
+
const snapshotOutput = await hdc.screenshot(this.remoteScreenshotPath);
|
|
581
|
+
const dimMatch = snapshotOutput.match(/width\s+(\d+),\s*height\s+(\d+)/);
|
|
582
|
+
if (dimMatch) {
|
|
583
|
+
const w = Number.parseInt(dimMatch[1], 10);
|
|
584
|
+
const h = Number.parseInt(dimMatch[2], 10);
|
|
585
|
+
if (this.cachedScreenSize && (this.cachedScreenSize.width !== w || this.cachedScreenSize.height !== h)) {
|
|
586
|
+
debugDevice(`Screen size changed: ${this.cachedScreenSize.width}x${this.cachedScreenSize.height} -> ${w}x${h}`);
|
|
587
|
+
this.cachedScreenSize = {
|
|
588
|
+
width: w,
|
|
589
|
+
height: h
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
await hdc.fileRecv(this.remoteScreenshotPath, this.localScreenshotPath);
|
|
594
|
+
const screenshotBuffer = await node_fs.promises.readFile(this.localScreenshotPath);
|
|
595
|
+
if (screenshotBuffer && screenshotBuffer.length > 0) {
|
|
596
|
+
debugDevice(`Screenshot captured: ${screenshotBuffer.length} bytes`);
|
|
597
|
+
return createImgBase64ByFormat('jpeg', screenshotBuffer.toString('base64'));
|
|
598
|
+
}
|
|
599
|
+
debugDevice(`Screenshot buffer empty (attempt ${attempt}/${maxAttempts})`);
|
|
600
|
+
if (attempt < maxAttempts) await sleep(200);
|
|
601
|
+
}
|
|
602
|
+
throw new Error('Screenshot buffer is empty after retries');
|
|
603
|
+
}
|
|
604
|
+
async tapPoint(point) {
|
|
605
|
+
this.lastTapPosition = {
|
|
606
|
+
x: point.x,
|
|
607
|
+
y: point.y
|
|
608
|
+
};
|
|
609
|
+
const hdc = await this.getHdc();
|
|
610
|
+
await hdc.click(point.x, point.y);
|
|
611
|
+
}
|
|
612
|
+
async doubleTapPoint(point) {
|
|
613
|
+
const hdc = await this.getHdc();
|
|
614
|
+
await hdc.doubleClick(point.x, point.y);
|
|
615
|
+
}
|
|
616
|
+
async longPressPoint(point) {
|
|
617
|
+
const hdc = await this.getHdc();
|
|
618
|
+
await hdc.longClick(point.x, point.y);
|
|
619
|
+
}
|
|
620
|
+
async typeText(text, element, shouldReplace, options) {
|
|
621
|
+
if (!text) return;
|
|
622
|
+
const resolvedInputOptions = resolveTextInputOptions(options, this.options);
|
|
623
|
+
const hdc = await this.getHdc();
|
|
624
|
+
const typeDelay = resolvedInputOptions.keyboardTypeDelay;
|
|
625
|
+
const inputSequentially = shouldInputSequentially(resolvedInputOptions);
|
|
626
|
+
let x;
|
|
627
|
+
let y;
|
|
628
|
+
if (element) [x, y] = element.center;
|
|
629
|
+
else if (this.lastTapPosition) {
|
|
630
|
+
x = this.lastTapPosition.x;
|
|
631
|
+
y = this.lastTapPosition.y;
|
|
632
|
+
} else {
|
|
633
|
+
const { width, height } = await this.size();
|
|
634
|
+
x = Math.round(width / 2);
|
|
635
|
+
y = Math.round(height / 2);
|
|
636
|
+
}
|
|
637
|
+
if (shouldReplace) {
|
|
638
|
+
await hdc.click(x, y);
|
|
639
|
+
await sleep(100);
|
|
640
|
+
await hdc.clearTextField();
|
|
641
|
+
await sleep(100);
|
|
642
|
+
}
|
|
643
|
+
if (inputSequentially) await sendTextSequentially(text, {
|
|
644
|
+
sendCharacter: (character)=>hdc.inputText(x, y, character),
|
|
645
|
+
wait: sleep
|
|
646
|
+
}, {
|
|
647
|
+
delayMs: typeDelay,
|
|
648
|
+
delayAfterLast: 'legacy' === resolvedInputOptions.inputStrategy
|
|
649
|
+
});
|
|
650
|
+
else await hdc.inputText(x, y, text);
|
|
651
|
+
const shouldAutoDismissKeyboard = options?.autoDismissKeyboard ?? this.options?.autoDismissKeyboard ?? true;
|
|
652
|
+
if (shouldAutoDismissKeyboard) await this.hideKeyboard(options);
|
|
653
|
+
}
|
|
654
|
+
async clearInput(element) {
|
|
655
|
+
const hdc = await this.getHdc();
|
|
656
|
+
if (element) {
|
|
657
|
+
await hdc.click(element.center[0], element.center[1]);
|
|
658
|
+
await sleep(100);
|
|
659
|
+
}
|
|
660
|
+
await hdc.clearTextField();
|
|
661
|
+
}
|
|
662
|
+
async pressKey(key) {
|
|
663
|
+
const keyCodes = resolveHarmonyKeyCodes(key);
|
|
664
|
+
const hdc = await this.getHdc();
|
|
665
|
+
await hdc.keyEvent(...keyCodes);
|
|
666
|
+
}
|
|
667
|
+
async scroll(deltaX, deltaY, speed) {
|
|
668
|
+
if (0 === deltaX && 0 === deltaY) throw new Error('Scroll distance cannot be zero in both directions');
|
|
669
|
+
const { width, height } = await this.size();
|
|
670
|
+
const n = scrollQuadrantDivisions;
|
|
671
|
+
const axisStart = (delta, extent)=>Math.round(0 === delta ? extent / 2 : delta < 0 ? extent / n : (n - 1) * extent / n);
|
|
672
|
+
const startX = axisStart(deltaX, width);
|
|
673
|
+
const startY = axisStart(deltaY, height);
|
|
674
|
+
const maxPositiveDeltaX = startX;
|
|
675
|
+
const maxNegativeDeltaX = width - startX;
|
|
676
|
+
const maxPositiveDeltaY = startY;
|
|
677
|
+
const maxNegativeDeltaY = height - startY;
|
|
678
|
+
deltaX = Math.max(-maxNegativeDeltaX, Math.min(deltaX, maxPositiveDeltaX));
|
|
679
|
+
deltaY = Math.max(-maxNegativeDeltaY, Math.min(deltaY, maxPositiveDeltaY));
|
|
680
|
+
const endX = Math.round(Math.max(screenEdgeMargin, Math.min(width - screenEdgeMargin, startX - deltaX)));
|
|
681
|
+
const endY = Math.round(Math.max(screenEdgeMargin, Math.min(height - screenEdgeMargin, startY - deltaY)));
|
|
682
|
+
const hdc = await this.getHdc();
|
|
683
|
+
await hdc.fling(startX, startY, endX, endY, speed ?? defaultFastSwipeSpeed);
|
|
684
|
+
}
|
|
685
|
+
assertScrollDirection(start, end, direction, size) {
|
|
686
|
+
node_assert([
|
|
687
|
+
start.x,
|
|
688
|
+
start.y,
|
|
689
|
+
end.x,
|
|
690
|
+
end.y
|
|
691
|
+
].every(Number.isFinite) && start.x >= 0 && start.x < size.width && start.y >= 0 && start.y < size.height, 'Scroll origin must be finite and inside the viewport');
|
|
692
|
+
const travel = {
|
|
693
|
+
down: start.y - end.y,
|
|
694
|
+
up: end.y - start.y,
|
|
695
|
+
left: end.x - start.x,
|
|
696
|
+
right: start.x - end.x
|
|
697
|
+
}[direction];
|
|
698
|
+
node_assert(Number.isFinite(travel) && travel > 0, `No space to scroll ${direction} from the located point; choose a point farther from the screen edge`);
|
|
699
|
+
}
|
|
700
|
+
async scrollInDirection(direction, distance, startPoint) {
|
|
701
|
+
const { width, height } = await this.size();
|
|
702
|
+
const isVertical = 'up' === direction || 'down' === direction;
|
|
703
|
+
if (void 0 === distance && !startPoint) {
|
|
704
|
+
const { from, to } = resolveDefaultMobileScroll(direction, {
|
|
705
|
+
width,
|
|
706
|
+
height
|
|
707
|
+
});
|
|
708
|
+
const hdc = await this.getHdc();
|
|
709
|
+
await hdc.fling(from.x, from.y, to.x, to.y, resolveSwipeSpeed(from, to, 1000));
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
const scrollDistance = Math.round(distance ?? (isVertical ? height : width));
|
|
713
|
+
if (startPoint) {
|
|
714
|
+
const hdc = await this.getHdc();
|
|
715
|
+
const sx = Math.round(startPoint.left);
|
|
716
|
+
const sy = Math.round(startPoint.top);
|
|
717
|
+
const endPoints = {
|
|
718
|
+
down: {
|
|
719
|
+
x: sx,
|
|
720
|
+
y: Math.max(screenEdgeMargin, sy - scrollDistance)
|
|
721
|
+
},
|
|
722
|
+
up: {
|
|
723
|
+
x: sx,
|
|
724
|
+
y: Math.min(height - screenEdgeMargin, sy + scrollDistance)
|
|
725
|
+
},
|
|
726
|
+
left: {
|
|
727
|
+
x: Math.min(width - screenEdgeMargin, sx + scrollDistance),
|
|
728
|
+
y: sy
|
|
729
|
+
},
|
|
730
|
+
right: {
|
|
731
|
+
x: Math.max(screenEdgeMargin, sx - scrollDistance),
|
|
732
|
+
y: sy
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
const end = endPoints[direction];
|
|
736
|
+
this.assertScrollDirection({
|
|
737
|
+
x: sx,
|
|
738
|
+
y: sy
|
|
739
|
+
}, end, direction, {
|
|
740
|
+
width,
|
|
741
|
+
height
|
|
742
|
+
});
|
|
743
|
+
await hdc.fling(sx, sy, end.x, end.y, defaultFastSwipeSpeed);
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const deltas = {
|
|
747
|
+
down: [
|
|
748
|
+
0,
|
|
749
|
+
scrollDistance
|
|
750
|
+
],
|
|
751
|
+
up: [
|
|
752
|
+
0,
|
|
753
|
+
-scrollDistance
|
|
754
|
+
],
|
|
755
|
+
left: [
|
|
756
|
+
-scrollDistance,
|
|
757
|
+
0
|
|
758
|
+
],
|
|
759
|
+
right: [
|
|
760
|
+
scrollDistance,
|
|
761
|
+
0
|
|
762
|
+
]
|
|
763
|
+
};
|
|
764
|
+
const [dx, dy] = deltas[direction];
|
|
765
|
+
await this.scroll(dx, dy);
|
|
766
|
+
}
|
|
767
|
+
async scrollDown(distance, startPoint) {
|
|
768
|
+
await this.scrollInDirection('down', distance, startPoint);
|
|
769
|
+
}
|
|
770
|
+
async scrollUp(distance, startPoint) {
|
|
771
|
+
await this.scrollInDirection('up', distance, startPoint);
|
|
772
|
+
}
|
|
773
|
+
async scrollLeft(distance, startPoint) {
|
|
774
|
+
await this.scrollInDirection('left', distance, startPoint);
|
|
775
|
+
}
|
|
776
|
+
async scrollRight(distance, startPoint) {
|
|
777
|
+
await this.scrollInDirection('right', distance, startPoint);
|
|
778
|
+
}
|
|
779
|
+
async scrollUntilEdge(direction, startPoint) {
|
|
780
|
+
if (startPoint) {
|
|
781
|
+
const { width, height } = await this.size();
|
|
782
|
+
const hdc = await this.getHdc();
|
|
783
|
+
const sx = Math.round(startPoint.left);
|
|
784
|
+
const sy = Math.round(startPoint.top);
|
|
785
|
+
const flingTargets = {
|
|
786
|
+
up: {
|
|
787
|
+
x: sx,
|
|
788
|
+
y: Math.round(height) - screenEdgeMargin
|
|
789
|
+
},
|
|
790
|
+
down: {
|
|
791
|
+
x: sx,
|
|
792
|
+
y: screenEdgeMargin
|
|
793
|
+
},
|
|
794
|
+
left: {
|
|
795
|
+
x: Math.round(width) - screenEdgeMargin,
|
|
796
|
+
y: sy
|
|
797
|
+
},
|
|
798
|
+
right: {
|
|
799
|
+
x: screenEdgeMargin,
|
|
800
|
+
y: sy
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
const target = flingTargets[direction];
|
|
804
|
+
this.assertScrollDirection({
|
|
805
|
+
x: sx,
|
|
806
|
+
y: sy
|
|
807
|
+
}, target, direction, {
|
|
808
|
+
width,
|
|
809
|
+
height
|
|
810
|
+
});
|
|
811
|
+
await repeat(defaultScrollUntilTimes, ()=>hdc.fling(sx, sy, target.x, target.y, defaultFastSwipeSpeed));
|
|
812
|
+
await sleep(1000);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
const deltas = {
|
|
816
|
+
up: [
|
|
817
|
+
0,
|
|
818
|
+
-maxScrollDistance
|
|
819
|
+
],
|
|
820
|
+
down: [
|
|
821
|
+
0,
|
|
822
|
+
maxScrollDistance
|
|
823
|
+
],
|
|
824
|
+
left: [
|
|
825
|
+
-maxScrollDistance,
|
|
826
|
+
0
|
|
827
|
+
],
|
|
828
|
+
right: [
|
|
829
|
+
maxScrollDistance,
|
|
830
|
+
0
|
|
831
|
+
]
|
|
832
|
+
};
|
|
833
|
+
const [dx, dy] = deltas[direction];
|
|
834
|
+
await repeat(defaultScrollUntilTimes, ()=>this.scroll(dx, dy, defaultFastSwipeSpeed));
|
|
835
|
+
await sleep(1000);
|
|
836
|
+
}
|
|
837
|
+
async scrollUntilTop(startPoint) {
|
|
838
|
+
await this.scrollUntilEdge('up', startPoint);
|
|
839
|
+
}
|
|
840
|
+
async scrollUntilBottom(startPoint) {
|
|
841
|
+
await this.scrollUntilEdge('down', startPoint);
|
|
842
|
+
}
|
|
843
|
+
async scrollUntilLeft(startPoint) {
|
|
844
|
+
await this.scrollUntilEdge('left', startPoint);
|
|
845
|
+
}
|
|
846
|
+
async scrollUntilRight(startPoint) {
|
|
847
|
+
await this.scrollUntilEdge('right', startPoint);
|
|
848
|
+
}
|
|
849
|
+
async back() {
|
|
850
|
+
const hdc = await this.getHdc();
|
|
851
|
+
await hdc.keyEvent('Back');
|
|
852
|
+
}
|
|
853
|
+
async home() {
|
|
854
|
+
const hdc = await this.getHdc();
|
|
855
|
+
await hdc.keyEvent('Home');
|
|
856
|
+
}
|
|
857
|
+
async recentApps() {
|
|
858
|
+
const hdc = await this.getHdc();
|
|
859
|
+
await hdc.keyEvent('10011');
|
|
860
|
+
}
|
|
861
|
+
async hideKeyboard(options) {
|
|
862
|
+
const hdc = await this.getHdc();
|
|
863
|
+
const keyboardDismissStrategy = options?.keyboardDismissStrategy ?? this.options?.keyboardDismissStrategy ?? 'esc-first';
|
|
864
|
+
if ('back-first' === keyboardDismissStrategy) return void await hdc.keyEvent('Back');
|
|
865
|
+
await hdc.keyEvent(...resolveHarmonyKeyCodes('Escape'));
|
|
866
|
+
}
|
|
867
|
+
async getDeviceLocalTimeString(format = 'YYYY-MM-DD HH:mm:ss') {
|
|
868
|
+
const hdc = await this.getHdc();
|
|
869
|
+
try {
|
|
870
|
+
const stdout = await hdc.shell('date +%Y-%m-%dT%H:%M:%S');
|
|
871
|
+
const match = stdout.trim().match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/);
|
|
872
|
+
if (!match) throw new Error(`Invalid device time format: ${stdout}`);
|
|
873
|
+
const [, year, month, day, hours, minutes, seconds] = match;
|
|
874
|
+
const timeString = format.replace('YYYY', year).replace('MM', month).replace('DD', day).replace('HH', hours).replace('mm', minutes).replace('ss', seconds);
|
|
875
|
+
debugDevice(`Got device local time: ${timeString}`);
|
|
876
|
+
return `${timeString} (${format})`;
|
|
877
|
+
} catch (error) {
|
|
878
|
+
debugDevice(`Failed to get device local time: ${error}`);
|
|
879
|
+
throw new Error(`Failed to get device local time: ${error}`);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
async destroy() {
|
|
883
|
+
if (this.destroyed) return;
|
|
884
|
+
this.destroyed = true;
|
|
885
|
+
this.cachedScreenSize = null;
|
|
886
|
+
this.hdc = null;
|
|
887
|
+
this.connecting = null;
|
|
888
|
+
}
|
|
889
|
+
constructor(deviceId, options){
|
|
890
|
+
device_define_property(this, "deviceId", void 0);
|
|
891
|
+
device_define_property(this, "hdc", null);
|
|
892
|
+
device_define_property(this, "connecting", null);
|
|
893
|
+
device_define_property(this, "destroyed", false);
|
|
894
|
+
device_define_property(this, "descriptionText", void 0);
|
|
895
|
+
device_define_property(this, "customActions", void 0);
|
|
896
|
+
device_define_property(this, "cachedScreenSize", null);
|
|
897
|
+
device_define_property(this, "appNameMapping", {});
|
|
898
|
+
device_define_property(this, "lastTapPosition", null);
|
|
899
|
+
device_define_property(this, "interfaceType", 'harmony');
|
|
900
|
+
device_define_property(this, "uri", void 0);
|
|
901
|
+
device_define_property(this, "options", void 0);
|
|
902
|
+
device_define_property(this, "inputPrimitives", {
|
|
903
|
+
pointer: {
|
|
904
|
+
tap: (point)=>this.tapPoint(point),
|
|
905
|
+
doubleClick: (point)=>this.doubleTapPoint(point),
|
|
906
|
+
longPress: (point)=>this.longPressPoint(point),
|
|
907
|
+
dragAndDrop: async (from, to)=>{
|
|
908
|
+
const hdc = await this.getHdc();
|
|
909
|
+
await hdc.drag(from.x, from.y, to.x, to.y);
|
|
910
|
+
}
|
|
911
|
+
},
|
|
912
|
+
keyboard: {
|
|
913
|
+
keyboardPress: (keyName)=>this.pressKey(keyName),
|
|
914
|
+
typeText: (value, opts)=>{
|
|
915
|
+
const harmonyOpts = opts;
|
|
916
|
+
return harmonyOpts?.focusOnly ? Promise.resolve() : this.typeText(value, harmonyOpts?.target, harmonyOpts?.replace ?? true, {
|
|
917
|
+
autoDismissKeyboard: harmonyOpts?.autoDismissKeyboard,
|
|
918
|
+
keyboardDismissStrategy: harmonyOpts?.keyboardDismissStrategy,
|
|
919
|
+
keyboardTypeDelay: harmonyOpts?.keyboardTypeDelay,
|
|
920
|
+
inputStrategy: harmonyOpts?.inputStrategy
|
|
921
|
+
});
|
|
922
|
+
},
|
|
923
|
+
clearInput: (target)=>this.clearInput(target),
|
|
924
|
+
cursorMove: async (direction, times = 1)=>{
|
|
925
|
+
const arrowKey = 'left' === direction ? 'ArrowLeft' : 'ArrowRight';
|
|
926
|
+
for(let i = 0; i < times; i++)await this.pressKey(arrowKey);
|
|
927
|
+
}
|
|
928
|
+
},
|
|
929
|
+
touch: {
|
|
930
|
+
swipe: async (start, end, opts)=>{
|
|
931
|
+
const speed = resolveSwipeSpeed(start, end, opts?.duration);
|
|
932
|
+
const repeatCount = opts?.repeat ?? 1;
|
|
933
|
+
const hdc = await this.getHdc();
|
|
934
|
+
for(let i = 0; i < repeatCount; i++)await hdc.swipe(start.x, start.y, end.x, end.y, speed);
|
|
935
|
+
}
|
|
936
|
+
},
|
|
937
|
+
scroll: {
|
|
938
|
+
scroll: (param, opts)=>this.performActionScroll(param, opts)
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
device_define_property(this, "remoteScreenshotPath", '/data/local/tmp/ms_screen.jpeg');
|
|
942
|
+
device_define_property(this, "localScreenshotPath", null);
|
|
943
|
+
node_assert(deviceId, 'deviceId is required for HarmonyDevice');
|
|
944
|
+
this.deviceId = deviceId;
|
|
945
|
+
this.options = options;
|
|
946
|
+
this.customActions = options?.customActions;
|
|
947
|
+
if (options?.screenshotResizeScale !== void 0 && !screenshotResizeScaleWarned) {
|
|
948
|
+
screenshotResizeScaleWarned = true;
|
|
949
|
+
console.warn('[midscene] screenshotResizeScale is deprecated. Use screenshotShrinkFactor in AgentOpt instead.');
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
const runHdcShellParamSchema = z.object({
|
|
954
|
+
command: z.string().describe('HDC shell command to execute')
|
|
955
|
+
});
|
|
956
|
+
const launchParamSchema = z.object({
|
|
957
|
+
uri: z.string().describe('App name, bundle name, or URL to launch. Prioritize using the exact bundle name or URL the user has provided. If none provided, use the accurate app name.')
|
|
958
|
+
});
|
|
959
|
+
const terminateParamSchema = z.object({
|
|
960
|
+
uri: z.string().describe('Bundle name or app name to terminate. Prioritize using the exact bundle name the user provided. If the bundle is unknown, use the accurate app name shown on screen, such as Settings or Music.')
|
|
961
|
+
});
|
|
962
|
+
const createPlatformActions = (device)=>({
|
|
963
|
+
RunHdcShell: defineAction({
|
|
964
|
+
name: 'RunHdcShell',
|
|
965
|
+
description: 'Execute HDC shell command on HarmonyOS device',
|
|
966
|
+
interfaceAlias: 'runHdcShell',
|
|
967
|
+
paramSchema: runHdcShellParamSchema,
|
|
968
|
+
sample: {
|
|
969
|
+
command: 'hidumper -s WindowManagerService -a'
|
|
970
|
+
},
|
|
971
|
+
call: async (param)=>{
|
|
972
|
+
if (!param.command || '' === param.command.trim()) throw new Error('RunHdcShell requires a non-empty command parameter');
|
|
973
|
+
const hdc = await device.getHdc();
|
|
974
|
+
return await hdc.shell(param.command);
|
|
975
|
+
}
|
|
976
|
+
}),
|
|
977
|
+
Launch: defineAction({
|
|
978
|
+
name: 'Launch',
|
|
979
|
+
description: 'Launch a HarmonyOS app or URL',
|
|
980
|
+
interfaceAlias: 'launch',
|
|
981
|
+
paramSchema: launchParamSchema,
|
|
982
|
+
sample: {
|
|
983
|
+
uri: 'com.example.app'
|
|
984
|
+
},
|
|
985
|
+
call: async (param)=>{
|
|
986
|
+
if (!param.uri || '' === param.uri.trim()) throw new Error('Launch requires a non-empty uri parameter');
|
|
987
|
+
await device.launch(param.uri);
|
|
988
|
+
}
|
|
989
|
+
}),
|
|
990
|
+
Terminate: defineAction({
|
|
991
|
+
name: 'Terminate',
|
|
992
|
+
description: 'Terminate (force-stop) a HarmonyOS app by bundle name or mapped app name',
|
|
993
|
+
interfaceAlias: 'terminate',
|
|
994
|
+
paramSchema: terminateParamSchema,
|
|
995
|
+
call: async (param)=>{
|
|
996
|
+
if (!param.uri || '' === param.uri.trim()) throw new Error('Terminate requires a non-empty uri parameter');
|
|
997
|
+
await device.terminate(param.uri);
|
|
998
|
+
}
|
|
999
|
+
}),
|
|
1000
|
+
HarmonyBackButton: defineAction({
|
|
1001
|
+
name: 'HarmonyBackButton',
|
|
1002
|
+
supportedBySystemOne: true,
|
|
1003
|
+
description: 'Trigger the system "back" operation on HarmonyOS devices',
|
|
1004
|
+
call: async ()=>{
|
|
1005
|
+
await device.back();
|
|
1006
|
+
}
|
|
1007
|
+
}),
|
|
1008
|
+
HarmonyHomeButton: defineAction({
|
|
1009
|
+
name: 'HarmonyHomeButton',
|
|
1010
|
+
supportedBySystemOne: true,
|
|
1011
|
+
description: 'Trigger the system "home" operation on HarmonyOS devices',
|
|
1012
|
+
call: async ()=>{
|
|
1013
|
+
await device.home();
|
|
1014
|
+
}
|
|
1015
|
+
}),
|
|
1016
|
+
HarmonyRecentAppsButton: defineAction({
|
|
1017
|
+
name: 'HarmonyRecentAppsButton',
|
|
1018
|
+
supportedBySystemOne: true,
|
|
1019
|
+
description: 'Trigger the system "recent apps" operation on HarmonyOS devices',
|
|
1020
|
+
call: async ()=>{
|
|
1021
|
+
await device.recentApps();
|
|
1022
|
+
}
|
|
1023
|
+
})
|
|
1024
|
+
});
|
|
1025
|
+
const defaultAppNameMapping = {
|
|
1026
|
+
设置: 'com.huawei.hmos.settings',
|
|
1027
|
+
Settings: 'com.huawei.hmos.settings',
|
|
1028
|
+
相机: 'com.huawei.hmos.camera',
|
|
1029
|
+
Camera: 'com.huawei.hmos.camera',
|
|
1030
|
+
图库: 'com.huawei.hmos.photos',
|
|
1031
|
+
Gallery: 'com.huawei.hmos.photos',
|
|
1032
|
+
日历: 'com.huawei.hmos.calendar',
|
|
1033
|
+
Calendar: 'com.huawei.hmos.calendar',
|
|
1034
|
+
时钟: 'com.huawei.hmos.clock',
|
|
1035
|
+
Clock: 'com.huawei.hmos.clock',
|
|
1036
|
+
计算器: 'com.huawei.hmos.calculator',
|
|
1037
|
+
Calculator: 'com.huawei.hmos.calculator',
|
|
1038
|
+
文件管理: 'com.huawei.hmos.filemanager',
|
|
1039
|
+
备忘录: 'com.huawei.hmos.notepad',
|
|
1040
|
+
联系人: 'com.huawei.hmos.contacts',
|
|
1041
|
+
电话: 'com.huawei.hmos.phone',
|
|
1042
|
+
信息: 'com.huawei.hmos.message',
|
|
1043
|
+
邮件: 'com.huawei.hmos.email',
|
|
1044
|
+
浏览器: 'com.huawei.hmos.browser',
|
|
1045
|
+
Browser: 'com.huawei.hmos.browser',
|
|
1046
|
+
应用市场: 'com.huawei.appmarket',
|
|
1047
|
+
AppGallery: 'com.huawei.appmarket',
|
|
1048
|
+
华为音乐: 'com.huawei.hmsapp.music',
|
|
1049
|
+
Music: 'com.huawei.hmsapp.music',
|
|
1050
|
+
华为视频: 'com.huawei.hmos.video',
|
|
1051
|
+
天气: 'com.huawei.hmos.weather',
|
|
1052
|
+
Weather: 'com.huawei.hmos.weather',
|
|
1053
|
+
抖音: 'com.ss.hm.ugc.aweme',
|
|
1054
|
+
支付宝: 'com.alipay.mobile.client',
|
|
1055
|
+
高德地图: 'com.amap.hmapp',
|
|
1056
|
+
百度: 'com.baidu.baiduapp',
|
|
1057
|
+
携程: 'com.ctrip.harmonynext'
|
|
1058
|
+
};
|
|
1059
|
+
const debugUtils = getDebug('harmony:utils');
|
|
1060
|
+
async function getConnectedDevices(hdcPath, options = {}) {
|
|
1061
|
+
try {
|
|
1062
|
+
const hdc = new HdcClient({
|
|
1063
|
+
hdcPath,
|
|
1064
|
+
timeout: options.timeout
|
|
1065
|
+
});
|
|
1066
|
+
const targets = await hdc.listTargets();
|
|
1067
|
+
const devices = targets.map((deviceId)=>({
|
|
1068
|
+
deviceId
|
|
1069
|
+
}));
|
|
1070
|
+
debugUtils(`Found ${devices.length} connected devices: `, devices);
|
|
1071
|
+
return devices;
|
|
1072
|
+
} catch (error) {
|
|
1073
|
+
console.error('Failed to get device list:', error);
|
|
1074
|
+
throw new Error(`Unable to get connected HarmonyOS device list, please ensure HDC is properly configured: ${error.message}`, {
|
|
1075
|
+
cause: error
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
function agent_define_property(obj, key, value) {
|
|
1080
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
1081
|
+
value: value,
|
|
1082
|
+
enumerable: true,
|
|
1083
|
+
configurable: true,
|
|
1084
|
+
writable: true
|
|
1085
|
+
});
|
|
1086
|
+
else obj[key] = value;
|
|
1087
|
+
return obj;
|
|
1088
|
+
}
|
|
1089
|
+
const debugAgent = getDebug('harmony:agent');
|
|
1090
|
+
class HarmonyAgent extends Agent {
|
|
1091
|
+
static getTestRunnerNodeDefinitions() {
|
|
1092
|
+
return [
|
|
1093
|
+
...Agent.getTestRunnerNodeDefinitions(),
|
|
1094
|
+
...harmonyAgentTestRunnerNodeDefinitions
|
|
1095
|
+
];
|
|
1096
|
+
}
|
|
1097
|
+
async launch(uri) {
|
|
1098
|
+
const action = this.wrapActionInActionSpace('Launch');
|
|
1099
|
+
return action({
|
|
1100
|
+
uri
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
async terminate(uri) {
|
|
1104
|
+
const action = this.wrapActionInActionSpace('Terminate');
|
|
1105
|
+
return action({
|
|
1106
|
+
uri
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
async runHdcShell(command) {
|
|
1110
|
+
const action = this.wrapActionInActionSpace('RunHdcShell');
|
|
1111
|
+
return action({
|
|
1112
|
+
command
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
createActionWrapper(name) {
|
|
1116
|
+
const action = this.wrapActionInActionSpace(name);
|
|
1117
|
+
return (...args)=>action(args[0]);
|
|
1118
|
+
}
|
|
1119
|
+
constructor(device, opts){
|
|
1120
|
+
super(device, opts), agent_define_property(this, "back", void 0), agent_define_property(this, "home", void 0), agent_define_property(this, "recentApps", void 0);
|
|
1121
|
+
const appNameMapping = mergeAndNormalizeAppNameMapping(defaultAppNameMapping, opts?.appNameMapping);
|
|
1122
|
+
device.setAppNameMapping(appNameMapping);
|
|
1123
|
+
this.back = this.createActionWrapper('HarmonyBackButton');
|
|
1124
|
+
this.home = this.createActionWrapper('HarmonyHomeButton');
|
|
1125
|
+
this.recentApps = this.createActionWrapper('HarmonyRecentAppsButton');
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
async function agentFromHdcDevice(deviceId, opts) {
|
|
1129
|
+
if (!deviceId) {
|
|
1130
|
+
const devices = await getConnectedDevices(opts?.hdcPath);
|
|
1131
|
+
if (0 === devices.length) throw new Error('No HarmonyOS devices found. Please connect a HarmonyOS device and ensure HDC is properly configured. Run `hdc list targets` to verify device connection.');
|
|
1132
|
+
deviceId = devices[0].deviceId;
|
|
1133
|
+
debugAgent('deviceId not specified, will use the first device (id = %s)', deviceId);
|
|
1134
|
+
}
|
|
1135
|
+
const device = new HarmonyDevice(deviceId, opts || {});
|
|
1136
|
+
await device.connect();
|
|
1137
|
+
return new HarmonyAgent(device, opts);
|
|
1138
|
+
}
|
|
1139
|
+
export { HarmonyAgent, HarmonyDevice, agentFromHdcDevice, createReportCliCommands, getConnectedDevices, getDebug, z };
|