@humanjs/playwright 0.7.0 → 0.8.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/README.md +22 -0
- package/dist/chunk-CCZEDNOF.js +1915 -0
- package/dist/chunk-CCZEDNOF.js.map +1 -0
- package/dist/chunk-RCMSDC3N.cjs +1998 -0
- package/dist/chunk-RCMSDC3N.cjs.map +1 -0
- package/dist/index.cjs +37 -1785
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +100 -4
- package/dist/index.d.ts +100 -4
- package/dist/index.js +1 -1759
- package/dist/index.js.map +1 -1
- package/dist/test.cjs +26 -0
- package/dist/test.cjs.map +1 -0
- package/dist/test.d.cts +32 -0
- package/dist/test.d.ts +32 -0
- package/dist/test.js +21 -0
- package/dist/test.js.map +1 -0
- package/package.json +28 -4
package/dist/index.js
CHANGED
|
@@ -1,1761 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export { applyMicroJitter, applyVelocityProfile, bezierPath, blend, careful, computeReadingDwellMs, countWords, createRng, distracted, fast, humanizePath, planScroll, planTypeKeystrokes, precise, resolvePersonality, sleep } from '@humanjs/core';
|
|
3
|
-
import { spawn } from 'child_process';
|
|
4
|
-
import { rmSync } from 'fs';
|
|
5
|
-
import { mkdir, writeFile, mkdtemp, rm } from 'fs/promises';
|
|
6
|
-
import { dirname, extname, join } from 'path';
|
|
7
|
-
import ffmpegStatic from 'ffmpeg-static';
|
|
8
|
-
import { tmpdir } from 'os';
|
|
9
|
-
export { chromium, firefox, webkit } from 'playwright';
|
|
10
|
-
|
|
11
|
-
// src/index.ts
|
|
12
|
-
|
|
13
|
-
// src/internal/timing.ts
|
|
14
|
-
function sleep(ms) {
|
|
15
|
-
return ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve();
|
|
16
|
-
}
|
|
17
|
-
function speedModeFactor(speed) {
|
|
18
|
-
switch (speed) {
|
|
19
|
-
case "fast":
|
|
20
|
-
return 0.5;
|
|
21
|
-
case "instant":
|
|
22
|
-
return 0;
|
|
23
|
-
default:
|
|
24
|
-
return 1;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function computeDwellTime(meanMs, jitter, personality, speed, rng) {
|
|
28
|
-
if (meanMs <= 0) return 0;
|
|
29
|
-
const jitterMag = meanMs * jitter;
|
|
30
|
-
const offset = rng.nextFloat(-jitterMag, jitterMag);
|
|
31
|
-
return Math.max(0, (meanMs + offset) * personality.speed * speedModeFactor(speed));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// src/keyboard/index.ts
|
|
35
|
-
async function executeType(target, value, ctx) {
|
|
36
|
-
const locator = typeof target === "string" ? ctx.page.locator(target) : target;
|
|
37
|
-
if (value.length === 0) {
|
|
38
|
-
return { characters: 0, typos: 0, corrections: 0 };
|
|
39
|
-
}
|
|
40
|
-
if (ctx.speed === "instant") {
|
|
41
|
-
await locator.pressSequentially(value, { delay: 0 });
|
|
42
|
-
return { characters: value.length, typos: 0, corrections: 0 };
|
|
43
|
-
}
|
|
44
|
-
await locator.focus();
|
|
45
|
-
const plan = planTypeKeystrokes(value, ctx.personality.typing, ctx.rng, {
|
|
46
|
-
personalitySpeed: ctx.personality.speed,
|
|
47
|
-
speedFactor: speedModeFactor(ctx.speed)
|
|
48
|
-
});
|
|
49
|
-
let typos = 0;
|
|
50
|
-
let corrections = 0;
|
|
51
|
-
for (const step of plan) {
|
|
52
|
-
if (step.delayBeforeMs > 0) await sleep(step.delayBeforeMs);
|
|
53
|
-
await dispatchKey(ctx.page, step.key);
|
|
54
|
-
if (step.isTypo) typos++;
|
|
55
|
-
if (step.isCorrection) corrections++;
|
|
56
|
-
}
|
|
57
|
-
return { characters: value.length, typos, corrections };
|
|
58
|
-
}
|
|
59
|
-
async function dispatchKey(page, key) {
|
|
60
|
-
if (key.length > 1 || key.charCodeAt(0) < 128) {
|
|
61
|
-
await page.keyboard.press(key);
|
|
62
|
-
} else {
|
|
63
|
-
await page.keyboard.insertText(key);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async function executePaste(target, value, ctx) {
|
|
67
|
-
if (value.length === 0) return { characters: 0 };
|
|
68
|
-
const locator = typeof target === "string" ? ctx.page.locator(target) : target;
|
|
69
|
-
await locator.focus();
|
|
70
|
-
await ctx.page.keyboard.insertText(value);
|
|
71
|
-
return { characters: value.length };
|
|
72
|
-
}
|
|
73
|
-
async function executePress(key, ctx) {
|
|
74
|
-
const dispatched = resolveChord(key);
|
|
75
|
-
await ctx.page.keyboard.press(dispatched);
|
|
76
|
-
return { dispatched };
|
|
77
|
-
}
|
|
78
|
-
function resolveChord(key) {
|
|
79
|
-
const parts = key.split("+").map((p) => p.trim()).filter((p) => p.length > 0);
|
|
80
|
-
if (parts.length === 0) {
|
|
81
|
-
throw new Error(`Invalid key: ${JSON.stringify(key)} \u2014 empty or only separators`);
|
|
82
|
-
}
|
|
83
|
-
const keyToken = parts[parts.length - 1];
|
|
84
|
-
if (keyToken === void 0) {
|
|
85
|
-
throw new Error(`Invalid key: ${JSON.stringify(key)} \u2014 missing key`);
|
|
86
|
-
}
|
|
87
|
-
const modifierTokens = parts.slice(0, -1);
|
|
88
|
-
const modifiers = [];
|
|
89
|
-
for (const token of modifierTokens) {
|
|
90
|
-
const resolved = resolveModifier(token);
|
|
91
|
-
if (resolved === null) {
|
|
92
|
-
throw new Error(
|
|
93
|
-
`Invalid key modifier: ${JSON.stringify(token)} in ${JSON.stringify(key)}. Use one of: Mod/CmdOrCtrl/CommandOrControl, Cmd/Command/Meta/Win/Super, Ctrl/Control, Alt/Option/Opt, Shift.`
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
modifiers.push(resolved);
|
|
97
|
-
}
|
|
98
|
-
return [...modifiers, normalizeKey(keyToken)].join("+");
|
|
99
|
-
}
|
|
100
|
-
function resolveModifier(token) {
|
|
101
|
-
const lower = token.toLowerCase();
|
|
102
|
-
switch (lower) {
|
|
103
|
-
case "mod":
|
|
104
|
-
case "cmdorctrl":
|
|
105
|
-
case "commandorcontrol":
|
|
106
|
-
return isMac() ? "Meta" : "Control";
|
|
107
|
-
case "cmd":
|
|
108
|
-
case "command":
|
|
109
|
-
case "meta":
|
|
110
|
-
case "win":
|
|
111
|
-
case "super":
|
|
112
|
-
return "Meta";
|
|
113
|
-
case "ctrl":
|
|
114
|
-
case "control":
|
|
115
|
-
return "Control";
|
|
116
|
-
case "alt":
|
|
117
|
-
case "option":
|
|
118
|
-
case "opt":
|
|
119
|
-
return "Alt";
|
|
120
|
-
case "shift":
|
|
121
|
-
return "Shift";
|
|
122
|
-
default:
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
function normalizeKey(key) {
|
|
127
|
-
if (key.length === 1) return key.toUpperCase();
|
|
128
|
-
return key.charAt(0).toUpperCase() + key.slice(1);
|
|
129
|
-
}
|
|
130
|
-
function isMac() {
|
|
131
|
-
return process.platform === "darwin";
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// src/internal/mouse-walk.ts
|
|
135
|
-
async function walkMouseAlongPath(page, path, durationMs) {
|
|
136
|
-
if (path.length === 0) return;
|
|
137
|
-
const stepDelayMs = path.length > 1 && durationMs > 0 ? durationMs / (path.length - 1) : 0;
|
|
138
|
-
for (let i = 0; i < path.length; i++) {
|
|
139
|
-
const point = path[i];
|
|
140
|
-
if (!point) continue;
|
|
141
|
-
await page.mouse.move(point.x, point.y);
|
|
142
|
-
if (i < path.length - 1 && stepDelayMs > 0) {
|
|
143
|
-
await sleep(stepDelayMs);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
var RESERVED_TARGETS = /* @__PURE__ */ new Set(["natural", "end", "top"]);
|
|
148
|
-
async function executeScroll(target, ctx, options = {}) {
|
|
149
|
-
const { page, personality, rng, speed } = ctx;
|
|
150
|
-
const speedFactor = speedModeFactor(speed);
|
|
151
|
-
const axis = options.axis ?? "y";
|
|
152
|
-
const container = resolveWithin(options.within, ctx);
|
|
153
|
-
const geom = container ? await readContainerGeometry(container, axis) : await readWindowGeometry(page, axis);
|
|
154
|
-
if (!geom) {
|
|
155
|
-
return { from: 0, to: 0, distance: 0, durationMs: 0 };
|
|
156
|
-
}
|
|
157
|
-
const from = geom.current;
|
|
158
|
-
const targetPos = await resolveTarget(target, ctx, geom, container, axis, options.block);
|
|
159
|
-
const to = clamp(targetPos, 0, Math.max(0, geom.total - geom.viewport));
|
|
160
|
-
const distance = to - from;
|
|
161
|
-
if (distance === 0) {
|
|
162
|
-
return { from, to, distance: 0, durationMs: 0 };
|
|
163
|
-
}
|
|
164
|
-
if (speed === "instant") {
|
|
165
|
-
if (container) {
|
|
166
|
-
await container.evaluate(
|
|
167
|
-
(el, args) => {
|
|
168
|
-
const a = args;
|
|
169
|
-
if (a.axis === "x") el.scrollTo(a.pos, el.scrollTop);
|
|
170
|
-
else el.scrollTo(el.scrollLeft, a.pos);
|
|
171
|
-
},
|
|
172
|
-
{ axis, pos: to }
|
|
173
|
-
);
|
|
174
|
-
} else {
|
|
175
|
-
await page.evaluate(
|
|
176
|
-
(args) => {
|
|
177
|
-
if (args.axis === "x") window.scrollTo(args.pos, window.scrollY);
|
|
178
|
-
else window.scrollTo(window.scrollX, args.pos);
|
|
179
|
-
},
|
|
180
|
-
{ axis, pos: to }
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
return { from, to, distance, durationMs: 0 };
|
|
184
|
-
}
|
|
185
|
-
const segments = planScroll(from, to, personality.scroll, rng, {
|
|
186
|
-
forceOvershoot: options.overshoot,
|
|
187
|
-
withPauses: options.withPauses,
|
|
188
|
-
personalitySpeed: personality.speed,
|
|
189
|
-
speedFactor
|
|
190
|
-
});
|
|
191
|
-
if (container && geom.hover) {
|
|
192
|
-
await page.mouse.move(geom.hover.x, geom.hover.y);
|
|
193
|
-
}
|
|
194
|
-
const startedAt = Date.now();
|
|
195
|
-
await walkSegments(page, segments, axis, container);
|
|
196
|
-
const durationMs = Date.now() - startedAt;
|
|
197
|
-
return { from, to, distance, durationMs };
|
|
198
|
-
}
|
|
199
|
-
function resolveWithin(within, ctx) {
|
|
200
|
-
if (!within) return null;
|
|
201
|
-
return typeof within === "string" ? ctx.page.locator(within) : within;
|
|
202
|
-
}
|
|
203
|
-
async function readWindowGeometry(page, axis) {
|
|
204
|
-
const g = await page.evaluate((a) => {
|
|
205
|
-
if (a === "x") {
|
|
206
|
-
return {
|
|
207
|
-
current: window.scrollX,
|
|
208
|
-
viewport: window.innerWidth,
|
|
209
|
-
total: Math.max(document.documentElement.scrollWidth, document.body?.scrollWidth ?? 0)
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
return {
|
|
213
|
-
current: window.scrollY,
|
|
214
|
-
viewport: window.innerHeight,
|
|
215
|
-
total: Math.max(document.documentElement.scrollHeight, document.body?.scrollHeight ?? 0)
|
|
216
|
-
};
|
|
217
|
-
}, axis);
|
|
218
|
-
return { current: g.current, viewport: g.viewport, total: g.total };
|
|
219
|
-
}
|
|
220
|
-
async function readContainerGeometry(container, axis) {
|
|
221
|
-
return container.evaluate((el, a) => {
|
|
222
|
-
const rect = el.getBoundingClientRect();
|
|
223
|
-
const isX = a === "x";
|
|
224
|
-
return {
|
|
225
|
-
current: isX ? el.scrollLeft : el.scrollTop,
|
|
226
|
-
viewport: isX ? el.clientWidth : el.clientHeight,
|
|
227
|
-
total: isX ? el.scrollWidth : el.scrollHeight,
|
|
228
|
-
hover: {
|
|
229
|
-
x: rect.left + rect.width / 2,
|
|
230
|
-
y: rect.top + rect.height / 2
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
}, axis).catch(() => null);
|
|
234
|
-
}
|
|
235
|
-
async function resolveTarget(target, ctx, geom, container, axis, block = "start") {
|
|
236
|
-
if (target === void 0 || target === "natural") return geom.current + geom.viewport;
|
|
237
|
-
if (target === "end") return geom.total;
|
|
238
|
-
if (target === "top") return 0;
|
|
239
|
-
if (typeof target === "object" && "by" in target) return geom.current + target.by;
|
|
240
|
-
if (typeof target === "object" && "to" in target) return target.to;
|
|
241
|
-
const elementLocator = typeof target === "string" && !RESERVED_TARGETS.has(target) ? ctx.page.locator(target) : typeof target === "string" ? null : target;
|
|
242
|
-
if (!elementLocator) return geom.current + geom.viewport;
|
|
243
|
-
return container ? resolveElementWithinContainer(elementLocator, container, geom, axis, block) : resolveElementInWindow(elementLocator, geom, axis, block);
|
|
244
|
-
}
|
|
245
|
-
async function resolveElementInWindow(elementLocator, geom, axis, block) {
|
|
246
|
-
const rect = await elementLocator.boundingBox().catch(() => null);
|
|
247
|
-
if (!rect) return geom.current;
|
|
248
|
-
const relStart = axis === "x" ? rect.x : rect.y;
|
|
249
|
-
const length = axis === "x" ? rect.width : rect.height;
|
|
250
|
-
const absoluteStart = geom.current + relStart;
|
|
251
|
-
const absoluteEnd = absoluteStart + length;
|
|
252
|
-
if (block === "start") return absoluteStart;
|
|
253
|
-
if (block === "end") return absoluteEnd - geom.viewport;
|
|
254
|
-
if (block === "nearest") {
|
|
255
|
-
if (relStart >= 0 && relStart + length <= geom.viewport) return geom.current;
|
|
256
|
-
if (relStart < 0) return absoluteStart;
|
|
257
|
-
return absoluteEnd - geom.viewport;
|
|
258
|
-
}
|
|
259
|
-
return absoluteStart - (geom.viewport - length) / 2;
|
|
260
|
-
}
|
|
261
|
-
async function resolveElementWithinContainer(elementLocator, container, geom, axis, block) {
|
|
262
|
-
const rects = await container.evaluate(
|
|
263
|
-
(containerEl, args) => {
|
|
264
|
-
const elementEl = args.sel ? document.querySelector(args.sel) : null;
|
|
265
|
-
const targetEl = elementEl ?? containerEl.querySelector(":scope > *");
|
|
266
|
-
if (!targetEl) return null;
|
|
267
|
-
const cRect = containerEl.getBoundingClientRect();
|
|
268
|
-
const eRect = targetEl.getBoundingClientRect();
|
|
269
|
-
return args.axis === "x" ? { relStart: eRect.left - cRect.left, length: eRect.width } : { relStart: eRect.top - cRect.top, length: eRect.height };
|
|
270
|
-
},
|
|
271
|
-
{ sel: await locatorSelector(elementLocator), axis }
|
|
272
|
-
).catch(() => null);
|
|
273
|
-
if (!rects) return geom.current;
|
|
274
|
-
const offsetStart = rects.relStart + geom.current;
|
|
275
|
-
const offsetEnd = offsetStart + rects.length;
|
|
276
|
-
if (block === "start") return offsetStart;
|
|
277
|
-
if (block === "end") return offsetEnd - geom.viewport;
|
|
278
|
-
if (block === "nearest") {
|
|
279
|
-
if (rects.relStart >= 0 && rects.relStart + rects.length <= geom.viewport) {
|
|
280
|
-
return geom.current;
|
|
281
|
-
}
|
|
282
|
-
if (rects.relStart < 0) return offsetStart;
|
|
283
|
-
return offsetEnd - geom.viewport;
|
|
284
|
-
}
|
|
285
|
-
return offsetStart - (geom.viewport - rects.length) / 2;
|
|
286
|
-
}
|
|
287
|
-
async function locatorSelector(locator) {
|
|
288
|
-
const s = locator.toString?.();
|
|
289
|
-
if (typeof s !== "string") return null;
|
|
290
|
-
const match = /locator\(['"](.+?)['"]/.exec(s);
|
|
291
|
-
if (!match) return null;
|
|
292
|
-
const raw = match[1] ?? "";
|
|
293
|
-
const eq = raw.indexOf("=");
|
|
294
|
-
return eq > 0 && /^[a-z]+$/.test(raw.slice(0, eq)) ? raw.slice(eq + 1) : raw;
|
|
295
|
-
}
|
|
296
|
-
async function walkSegments(page, segments, axis, container) {
|
|
297
|
-
for (const segment of segments) {
|
|
298
|
-
if (segment.delayBeforeMs > 0) await sleep(segment.delayBeforeMs);
|
|
299
|
-
if (segment.delta === 0) continue;
|
|
300
|
-
if (container) {
|
|
301
|
-
await container.evaluate(
|
|
302
|
-
(el, args) => {
|
|
303
|
-
const a = args;
|
|
304
|
-
if (a.axis === "x") el.scrollLeft += a.delta;
|
|
305
|
-
else el.scrollTop += a.delta;
|
|
306
|
-
},
|
|
307
|
-
{ axis, delta: segment.delta }
|
|
308
|
-
);
|
|
309
|
-
} else if (axis === "x") {
|
|
310
|
-
await page.mouse.wheel(segment.delta, 0);
|
|
311
|
-
} else {
|
|
312
|
-
await page.mouse.wheel(0, segment.delta);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
function clamp(value, min, max) {
|
|
317
|
-
return value < min ? min : value > max ? max : value;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
// src/mouse/index.ts
|
|
321
|
-
async function executeClick(target, ctx, options = {}) {
|
|
322
|
-
const button = options.button ?? "left";
|
|
323
|
-
if (ctx.speed === "instant") {
|
|
324
|
-
if (isPoint(target)) {
|
|
325
|
-
await ctx.page.mouse.click(target.x, target.y, { button });
|
|
326
|
-
ctx.setMousePosition(target);
|
|
327
|
-
return { target };
|
|
328
|
-
}
|
|
329
|
-
const locator = typeof target === "string" ? ctx.page.locator(target) : target;
|
|
330
|
-
const box2 = await locator.boundingBox();
|
|
331
|
-
await locator.click({ button });
|
|
332
|
-
const center = box2 ? { x: box2.x + box2.width / 2, y: box2.y + box2.height / 2 } : ctx.getMousePosition();
|
|
333
|
-
ctx.setMousePosition(center);
|
|
334
|
-
return { target: center };
|
|
335
|
-
}
|
|
336
|
-
const { point: targetPoint, box } = await resolveTargetPointAndBox(target, ctx, "click");
|
|
337
|
-
await maybeMisclickBeat(ctx, box, targetPoint);
|
|
338
|
-
await walkBezierTo(targetPoint, ctx);
|
|
339
|
-
const preClickMs = computeDwellTime(
|
|
340
|
-
ctx.personality.dwell.preClickMs,
|
|
341
|
-
ctx.personality.dwell.preClickJitter,
|
|
342
|
-
ctx.personality,
|
|
343
|
-
ctx.speed,
|
|
344
|
-
ctx.rng
|
|
345
|
-
);
|
|
346
|
-
if (preClickMs > 0) await sleep(preClickMs);
|
|
347
|
-
ctx.setMousePosition(targetPoint);
|
|
348
|
-
await ctx.page.mouse.click(targetPoint.x, targetPoint.y, { button });
|
|
349
|
-
const postActionMs = computeDwellTime(
|
|
350
|
-
ctx.personality.dwell.postActionMs,
|
|
351
|
-
ctx.personality.dwell.postActionJitter,
|
|
352
|
-
ctx.personality,
|
|
353
|
-
ctx.speed,
|
|
354
|
-
ctx.rng
|
|
355
|
-
);
|
|
356
|
-
if (postActionMs > 0) await sleep(postActionMs);
|
|
357
|
-
return { target: targetPoint };
|
|
358
|
-
}
|
|
359
|
-
async function executeHover(target, ctx) {
|
|
360
|
-
if (ctx.speed === "instant") {
|
|
361
|
-
const box = await readBoxWithAutoScroll(target, ctx, "hover");
|
|
362
|
-
const center = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
|
|
363
|
-
await ctx.page.mouse.move(center.x, center.y);
|
|
364
|
-
ctx.setMousePosition(center);
|
|
365
|
-
return { target: center };
|
|
366
|
-
}
|
|
367
|
-
const targetPoint = await moveToTarget(target, ctx);
|
|
368
|
-
const dwellMs = computeDwellTime(
|
|
369
|
-
ctx.personality.dwell.preClickMs,
|
|
370
|
-
ctx.personality.dwell.preClickJitter,
|
|
371
|
-
ctx.personality,
|
|
372
|
-
ctx.speed,
|
|
373
|
-
ctx.rng
|
|
374
|
-
);
|
|
375
|
-
if (dwellMs > 0) await sleep(dwellMs);
|
|
376
|
-
ctx.setMousePosition(targetPoint);
|
|
377
|
-
return { target: targetPoint };
|
|
378
|
-
}
|
|
379
|
-
async function executeDrag(from, to, ctx) {
|
|
380
|
-
const scrollYBeforeResolve = await readScrollY(ctx.page);
|
|
381
|
-
let { point: fromPoint, box: fromBox } = await resolveTargetPointAndBox(from, ctx, "drag");
|
|
382
|
-
let { point: toPoint, box: toBox } = await resolveTargetPointAndBox(to, ctx, "drag");
|
|
383
|
-
const resolveScrollDelta = await readScrollY(ctx.page) - scrollYBeforeResolve;
|
|
384
|
-
if (resolveScrollDelta !== 0) {
|
|
385
|
-
if (isPoint(from)) fromPoint = { x: fromPoint.x, y: fromPoint.y - resolveScrollDelta };
|
|
386
|
-
if (isPoint(to)) toPoint = { x: toPoint.x, y: toPoint.y - resolveScrollDelta };
|
|
387
|
-
}
|
|
388
|
-
if (ctx.speed === "instant") {
|
|
389
|
-
await ctx.page.mouse.move(fromPoint.x, fromPoint.y);
|
|
390
|
-
await ctx.page.mouse.down();
|
|
391
|
-
await ctx.page.mouse.move(toPoint.x, toPoint.y);
|
|
392
|
-
await ctx.page.mouse.up();
|
|
393
|
-
ctx.setMousePosition(toPoint);
|
|
394
|
-
return { from: fromPoint, to: toPoint };
|
|
395
|
-
}
|
|
396
|
-
if (fromBox && toBox) {
|
|
397
|
-
const scrollDelta = computeCurveScrollDelta(
|
|
398
|
-
fromPoint,
|
|
399
|
-
toPoint,
|
|
400
|
-
ctx.page.viewportSize(),
|
|
401
|
-
ctx.personality.mouse.curvature
|
|
402
|
-
);
|
|
403
|
-
if (scrollDelta !== 0) {
|
|
404
|
-
await executeScroll({ by: scrollDelta }, ctx, {});
|
|
405
|
-
const refreshedFrom = await resolveTargetPointAndBox(from, ctx, "drag");
|
|
406
|
-
fromPoint = refreshedFrom.point;
|
|
407
|
-
fromBox = refreshedFrom.box;
|
|
408
|
-
const refreshedTo = await resolveTargetPointAndBox(to, ctx, "drag");
|
|
409
|
-
toPoint = refreshedTo.point;
|
|
410
|
-
toBox = refreshedTo.box;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
await maybeMisclickBeat(ctx, fromBox, fromPoint);
|
|
414
|
-
await walkBezierTo(fromPoint, ctx);
|
|
415
|
-
ctx.setMousePosition(fromPoint);
|
|
416
|
-
const preDragMs = computeDwellTime(
|
|
417
|
-
ctx.personality.dwell.preClickMs,
|
|
418
|
-
ctx.personality.dwell.preClickJitter,
|
|
419
|
-
ctx.personality,
|
|
420
|
-
ctx.speed,
|
|
421
|
-
ctx.rng
|
|
422
|
-
);
|
|
423
|
-
if (preDragMs > 0) await sleep(preDragMs);
|
|
424
|
-
await ctx.page.mouse.down();
|
|
425
|
-
await maybeMisclickBeat(ctx, toBox, toPoint);
|
|
426
|
-
await walkBezierTo(toPoint, ctx);
|
|
427
|
-
await ctx.page.mouse.up();
|
|
428
|
-
ctx.setMousePosition(toPoint);
|
|
429
|
-
const postActionMs = computeDwellTime(
|
|
430
|
-
ctx.personality.dwell.postActionMs,
|
|
431
|
-
ctx.personality.dwell.postActionJitter,
|
|
432
|
-
ctx.personality,
|
|
433
|
-
ctx.speed,
|
|
434
|
-
ctx.rng
|
|
435
|
-
);
|
|
436
|
-
if (postActionMs > 0) await sleep(postActionMs);
|
|
437
|
-
return { from: fromPoint, to: toPoint };
|
|
438
|
-
}
|
|
439
|
-
async function executeMove(target, ctx) {
|
|
440
|
-
const point = await resolveTargetPoint(target, ctx, "move");
|
|
441
|
-
if (ctx.speed === "instant") {
|
|
442
|
-
await ctx.page.mouse.move(point.x, point.y);
|
|
443
|
-
ctx.setMousePosition(point);
|
|
444
|
-
return { target: point };
|
|
445
|
-
}
|
|
446
|
-
await walkBezierTo(point, ctx);
|
|
447
|
-
ctx.setMousePosition(point);
|
|
448
|
-
return { target: point };
|
|
449
|
-
}
|
|
450
|
-
async function moveToTarget(target, ctx) {
|
|
451
|
-
const box = await readBoxWithAutoScroll(target, ctx, "hover");
|
|
452
|
-
const targetPoint = pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
|
|
453
|
-
await walkBezierTo(targetPoint, ctx);
|
|
454
|
-
return targetPoint;
|
|
455
|
-
}
|
|
456
|
-
async function walkBezierTo(to, ctx) {
|
|
457
|
-
const startPoint = ctx.getMousePosition();
|
|
458
|
-
const rawPath = bezierPath(startPoint, to, ctx.rng, {
|
|
459
|
-
curvature: ctx.personality.mouse.curvature
|
|
460
|
-
});
|
|
461
|
-
const path = humanizePath(rawPath, ctx.rng);
|
|
462
|
-
const travelMs = computeTravelTime(path, ctx.personality, ctx.speed, ctx.rng);
|
|
463
|
-
await walkMouseAlongPath(ctx.page, path, travelMs);
|
|
464
|
-
}
|
|
465
|
-
async function resolveTargetPoint(target, ctx, action) {
|
|
466
|
-
if (isPoint(target)) return target;
|
|
467
|
-
return resolveLocatorPoint(target, ctx, action);
|
|
468
|
-
}
|
|
469
|
-
async function resolveTargetPointAndBox(target, ctx, action) {
|
|
470
|
-
if (isPoint(target)) return { point: target, box: null };
|
|
471
|
-
const box = await readBoxWithAutoScroll(target, ctx, action);
|
|
472
|
-
const point = pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
|
|
473
|
-
return { point, box };
|
|
474
|
-
}
|
|
475
|
-
async function resolveLocatorPoint(target, ctx, action) {
|
|
476
|
-
const box = await readBoxWithAutoScroll(target, ctx, action);
|
|
477
|
-
return pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
|
|
478
|
-
}
|
|
479
|
-
async function readBoxWithAutoScroll(target, ctx, action) {
|
|
480
|
-
const locator = typeof target === "string" ? ctx.page.locator(target) : target;
|
|
481
|
-
let box = await locator.boundingBox();
|
|
482
|
-
if (!box) {
|
|
483
|
-
throw new Error(
|
|
484
|
-
`Cannot ${action}: element not found or has no bounding box (target: ${describeTarget(target)})`
|
|
485
|
-
);
|
|
486
|
-
}
|
|
487
|
-
const viewport = ctx.page.viewportSize();
|
|
488
|
-
if (viewport && !isBoxCenterInViewport(box, viewport)) {
|
|
489
|
-
if (ctx.speed === "instant") {
|
|
490
|
-
await locator.scrollIntoViewIfNeeded();
|
|
491
|
-
} else {
|
|
492
|
-
await executeScroll(locator, ctx, { block: "center" });
|
|
493
|
-
}
|
|
494
|
-
box = await locator.boundingBox();
|
|
495
|
-
if (!box) {
|
|
496
|
-
throw new Error(
|
|
497
|
-
`Cannot ${action}: element disappeared after scrolling into view (target: ${describeTarget(target)})`
|
|
498
|
-
);
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
return box;
|
|
502
|
-
}
|
|
503
|
-
function isBoxCenterInViewport(box, viewport) {
|
|
504
|
-
const cx = box.x + box.width / 2;
|
|
505
|
-
const cy = box.y + box.height / 2;
|
|
506
|
-
return cx >= 0 && cx <= viewport.width && cy >= 0 && cy <= viewport.height;
|
|
507
|
-
}
|
|
508
|
-
async function readScrollY(page) {
|
|
509
|
-
if (typeof page.evaluate !== "function") return 0;
|
|
510
|
-
return page.evaluate(() => window.scrollY);
|
|
511
|
-
}
|
|
512
|
-
var CURVE_VIEWPORT_MARGIN = 20;
|
|
513
|
-
function computeCurveScrollDelta(from, to, viewport, curvature) {
|
|
514
|
-
if (!viewport) return 0;
|
|
515
|
-
const distance = Math.hypot(to.x - from.x, to.y - from.y);
|
|
516
|
-
const perpendicularExtent = distance * curvature;
|
|
517
|
-
const minY = Math.min(from.y, to.y) - perpendicularExtent;
|
|
518
|
-
const maxY = Math.max(from.y, to.y) + perpendicularExtent;
|
|
519
|
-
const topOverflow = -minY + CURVE_VIEWPORT_MARGIN;
|
|
520
|
-
const bottomOverflow = maxY + CURVE_VIEWPORT_MARGIN - viewport.height;
|
|
521
|
-
if (bottomOverflow > 0 && bottomOverflow >= topOverflow) return bottomOverflow;
|
|
522
|
-
if (topOverflow > 0) return -topOverflow;
|
|
523
|
-
return 0;
|
|
524
|
-
}
|
|
525
|
-
function isPoint(target) {
|
|
526
|
-
return typeof target === "object" && target !== null && !("boundingBox" in target) && typeof target.x === "number" && typeof target.y === "number";
|
|
527
|
-
}
|
|
528
|
-
var MISCLICK_OFFSET_MIN = 5;
|
|
529
|
-
var MISCLICK_OFFSET_MAX = 15;
|
|
530
|
-
async function maybeMisclickBeat(ctx, box, targetPoint) {
|
|
531
|
-
if (!ctx.rng.chance(ctx.personality.mouse.misclickProbability)) return;
|
|
532
|
-
if (cursorAlreadyOnTarget(ctx.getMousePosition(), box, targetPoint)) return;
|
|
533
|
-
const viewport = ctx.page.viewportSize();
|
|
534
|
-
const misclickPoint = box ? pickMisclickOutsideBox(box, ctx.rng, viewport) : pickMisclickAroundPoint(targetPoint, ctx.rng, viewport);
|
|
535
|
-
if (misclickPoint === null) return;
|
|
536
|
-
await walkBezierTo(misclickPoint, ctx);
|
|
537
|
-
ctx.setMousePosition(misclickPoint);
|
|
538
|
-
const realizeMs = computeDwellTime(
|
|
539
|
-
ctx.personality.dwell.preClickMs,
|
|
540
|
-
ctx.personality.dwell.preClickJitter,
|
|
541
|
-
ctx.personality,
|
|
542
|
-
ctx.speed,
|
|
543
|
-
ctx.rng
|
|
544
|
-
);
|
|
545
|
-
if (realizeMs > 0) await sleep(realizeMs);
|
|
546
|
-
}
|
|
547
|
-
function cursorAlreadyOnTarget(current, box, targetPoint) {
|
|
548
|
-
if (box) {
|
|
549
|
-
return current.x >= box.x && current.x <= box.x + box.width && current.y >= box.y && current.y <= box.y + box.height;
|
|
550
|
-
}
|
|
551
|
-
const dx = current.x - targetPoint.x;
|
|
552
|
-
const dy = current.y - targetPoint.y;
|
|
553
|
-
return Math.hypot(dx, dy) < MISCLICK_OFFSET_MIN;
|
|
554
|
-
}
|
|
555
|
-
function pickMisclickOutsideBox(box, rng, viewport) {
|
|
556
|
-
const edge = rng.nextInt(0, 4);
|
|
557
|
-
const offset = rng.nextFloat(MISCLICK_OFFSET_MIN, MISCLICK_OFFSET_MAX);
|
|
558
|
-
const along = rng.nextFloat(0.2, 0.8);
|
|
559
|
-
let x;
|
|
560
|
-
let y;
|
|
561
|
-
if (edge === 0) {
|
|
562
|
-
x = box.x + box.width * along;
|
|
563
|
-
y = box.y - offset;
|
|
564
|
-
} else if (edge === 1) {
|
|
565
|
-
x = box.x + box.width + offset;
|
|
566
|
-
y = box.y + box.height * along;
|
|
567
|
-
} else if (edge === 2) {
|
|
568
|
-
x = box.x + box.width * along;
|
|
569
|
-
y = box.y + box.height + offset;
|
|
570
|
-
} else {
|
|
571
|
-
x = box.x - offset;
|
|
572
|
-
y = box.y + box.height * along;
|
|
573
|
-
}
|
|
574
|
-
if (viewport) {
|
|
575
|
-
x = clamp2(x, 0, viewport.width - 1);
|
|
576
|
-
y = clamp2(y, 0, viewport.height - 1);
|
|
577
|
-
}
|
|
578
|
-
const insideBox = x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height;
|
|
579
|
-
if (insideBox) return null;
|
|
580
|
-
return { x, y };
|
|
581
|
-
}
|
|
582
|
-
function pickMisclickAroundPoint(target, rng, viewport) {
|
|
583
|
-
const angle = rng.nextFloat(0, Math.PI * 2);
|
|
584
|
-
const distance = rng.nextFloat(MISCLICK_OFFSET_MIN, MISCLICK_OFFSET_MAX);
|
|
585
|
-
let x = target.x + Math.cos(angle) * distance;
|
|
586
|
-
let y = target.y + Math.sin(angle) * distance;
|
|
587
|
-
if (viewport) {
|
|
588
|
-
x = clamp2(x, 0, viewport.width - 1);
|
|
589
|
-
y = clamp2(y, 0, viewport.height - 1);
|
|
590
|
-
}
|
|
591
|
-
if (x === target.x && y === target.y) return null;
|
|
592
|
-
return { x, y };
|
|
593
|
-
}
|
|
594
|
-
function pickClickPoint(box, rng, clickSpread) {
|
|
595
|
-
const cx = box.x + box.width / 2;
|
|
596
|
-
const cy = box.y + box.height / 2;
|
|
597
|
-
const sigmaX = box.width * clickSpread;
|
|
598
|
-
const sigmaY = box.height * clickSpread;
|
|
599
|
-
const x = clamp2(cx + rng.nextGaussian(0, sigmaX), box.x, box.x + box.width);
|
|
600
|
-
const y = clamp2(cy + rng.nextGaussian(0, sigmaY), box.y, box.y + box.height);
|
|
601
|
-
return { x, y };
|
|
602
|
-
}
|
|
603
|
-
function computeTravelTime(path, personality, speed, rng) {
|
|
604
|
-
let distance = 0;
|
|
605
|
-
for (let i = 1; i < path.length; i++) {
|
|
606
|
-
const prev = path[i - 1];
|
|
607
|
-
const curr = path[i];
|
|
608
|
-
if (!prev || !curr) continue;
|
|
609
|
-
distance += Math.hypot(curr.x - prev.x, curr.y - prev.y);
|
|
610
|
-
}
|
|
611
|
-
const baseTime = distance / 1e3 * personality.mouse.travelTimeMs;
|
|
612
|
-
const jitterMag = baseTime * personality.mouse.travelTimeJitter;
|
|
613
|
-
const jitter = rng.nextFloat(-jitterMag, jitterMag);
|
|
614
|
-
const total = (baseTime + jitter) * personality.speed * speedModeFactor(speed);
|
|
615
|
-
return Math.max(0, total);
|
|
616
|
-
}
|
|
617
|
-
function describeTarget(target) {
|
|
618
|
-
if (isPoint(target)) return `point(${target.x}, ${target.y})`;
|
|
619
|
-
return typeof target === "string" ? target : target.toString?.() ?? "locator";
|
|
620
|
-
}
|
|
621
|
-
function clamp2(value, min, max) {
|
|
622
|
-
return value < min ? min : value > max ? max : value;
|
|
623
|
-
}
|
|
624
|
-
async function executeRead(target, ctx, options = {}) {
|
|
625
|
-
let words = 0;
|
|
626
|
-
let locator;
|
|
627
|
-
if (typeof target === "string") {
|
|
628
|
-
locator = ctx.page.locator(target);
|
|
629
|
-
} else if ("words" in target) {
|
|
630
|
-
words = target.words;
|
|
631
|
-
} else if ("text" in target) {
|
|
632
|
-
words = countWords(target.text);
|
|
633
|
-
} else {
|
|
634
|
-
locator = target;
|
|
635
|
-
}
|
|
636
|
-
let autoDetectedKind;
|
|
637
|
-
if (locator) {
|
|
638
|
-
if (options.scrollIntoView) {
|
|
639
|
-
await locator.scrollIntoViewIfNeeded();
|
|
640
|
-
}
|
|
641
|
-
const text = await locator.innerText().catch(() => "");
|
|
642
|
-
words = countWords(text);
|
|
643
|
-
if (options.kind === void 0) {
|
|
644
|
-
autoDetectedKind = await detectKindFromTag(locator);
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
const kind = options.kind ?? autoDetectedKind ?? "prose";
|
|
648
|
-
const durationMs = computeReadingDwellMs(words, ctx.personality.reading, ctx.rng, {
|
|
649
|
-
kind,
|
|
650
|
-
wpmMultiplier: options.wpmMultiplier,
|
|
651
|
-
personalitySpeed: ctx.personality.speed,
|
|
652
|
-
speedFactor: speedModeFactor(ctx.speed)
|
|
653
|
-
});
|
|
654
|
-
const withMotion = options.withMotion ?? true;
|
|
655
|
-
if (withMotion && locator && durationMs > 0) {
|
|
656
|
-
const box = await locator.boundingBox().catch(() => null);
|
|
657
|
-
if (box) {
|
|
658
|
-
const lineRects = await getLineRects(locator).catch(() => []);
|
|
659
|
-
const path = planReadingScan(box, ctx.rng, {
|
|
660
|
-
start: ctx.getMousePosition(),
|
|
661
|
-
lineRects: lineRects.length > 0 ? lineRects : void 0
|
|
662
|
-
});
|
|
663
|
-
await walkMouseAlongPath(ctx.page, path, durationMs);
|
|
664
|
-
const final = path[path.length - 1];
|
|
665
|
-
if (final) ctx.setMousePosition(final);
|
|
666
|
-
return { words, durationMs, kind };
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
if (durationMs > 0) await sleep(durationMs);
|
|
670
|
-
return { words, durationMs, kind };
|
|
671
|
-
}
|
|
672
|
-
async function getLineRects(locator) {
|
|
673
|
-
const result = await locator.evaluate((el) => {
|
|
674
|
-
const walker = el.ownerDocument.createTreeWalker(el, 4);
|
|
675
|
-
const rects = [];
|
|
676
|
-
let node = walker.nextNode();
|
|
677
|
-
while (node) {
|
|
678
|
-
const text = node.textContent ?? "";
|
|
679
|
-
if (text.trim().length > 0) {
|
|
680
|
-
const range = el.ownerDocument.createRange();
|
|
681
|
-
range.selectNodeContents(node);
|
|
682
|
-
for (const r of Array.from(range.getClientRects())) {
|
|
683
|
-
if (r.width > 0 && r.height > 0) {
|
|
684
|
-
rects.push({ x: r.x, y: r.y, width: r.width, height: r.height });
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
node = walker.nextNode();
|
|
689
|
-
}
|
|
690
|
-
rects.sort((a, b) => a.y - b.y || a.x - b.x);
|
|
691
|
-
const merged = [];
|
|
692
|
-
for (const r of rects) {
|
|
693
|
-
const last = merged[merged.length - 1];
|
|
694
|
-
if (last && Math.abs(last.y - r.y) < 1 && r.x - (last.x + last.width) < 6) {
|
|
695
|
-
const right = Math.max(last.x + last.width, r.x + r.width);
|
|
696
|
-
const bottom = Math.max(last.y + last.height, r.y + r.height);
|
|
697
|
-
last.width = right - last.x;
|
|
698
|
-
last.height = bottom - last.y;
|
|
699
|
-
} else {
|
|
700
|
-
merged.push({ ...r });
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
return merged;
|
|
704
|
-
});
|
|
705
|
-
if (!Array.isArray(result)) return [];
|
|
706
|
-
return result.filter(
|
|
707
|
-
(r) => r != null && typeof r === "object" && typeof r.x === "number" && typeof r.y === "number" && typeof r.width === "number" && typeof r.height === "number"
|
|
708
|
-
);
|
|
709
|
-
}
|
|
710
|
-
async function detectKindFromTag(locator) {
|
|
711
|
-
const tag = await locator.evaluate((el) => el.tagName?.toLowerCase() ?? "").catch(() => "");
|
|
712
|
-
if (tag === "pre" || tag === "code") return "code";
|
|
713
|
-
return void 0;
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
// src/recording/codegen.ts
|
|
717
|
-
var POINT_RE = /^point\((-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)\)$/;
|
|
718
|
-
var POINT_COMMENT = " // raw coordinate \u2014 replace with a locator for a stable selector";
|
|
719
|
-
var UNCAPTURED_COMMENT = " // input not captured (masked or captureInputs disabled) \u2014 fill in (e.g. process.env.X)";
|
|
720
|
-
function q(value) {
|
|
721
|
-
return `'${String(value ?? "").replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r")}'`;
|
|
722
|
-
}
|
|
723
|
-
function targetArg(desc) {
|
|
724
|
-
const s = String(desc ?? "");
|
|
725
|
-
const m = s.match(POINT_RE);
|
|
726
|
-
if (m) return { code: `{ x: ${m[1]}, y: ${m[2]} }`, isPoint: true };
|
|
727
|
-
return { code: q(s), isPoint: false };
|
|
728
|
-
}
|
|
729
|
-
function createHumanOptions(timeline, ciSpeed = false) {
|
|
730
|
-
const parts = [` personality: ${q(timeline.personality)},`];
|
|
731
|
-
if (timeline.seed !== null) parts.push(` seed: ${q(timeline.seed)},`);
|
|
732
|
-
parts.push(
|
|
733
|
-
ciSpeed ? ` speed: process.env.CI ? 'instant' : ${q(timeline.speed)},` : ` speed: ${q(timeline.speed)},`
|
|
734
|
-
);
|
|
735
|
-
return `{
|
|
736
|
-
${parts.join("\n")}
|
|
737
|
-
}`;
|
|
738
|
-
}
|
|
739
|
-
function emitScroll(target) {
|
|
740
|
-
const s = String(target ?? "natural");
|
|
741
|
-
if (s === "natural") return " await human.scroll('natural');";
|
|
742
|
-
const by = s.match(/^by:(-?\d+(?:\.\d+)?)$/);
|
|
743
|
-
if (by) return ` await human.scroll({ by: ${by[1]} });`;
|
|
744
|
-
const to = s.match(/^to:(-?\d+(?:\.\d+)?)$/);
|
|
745
|
-
if (to) return ` await human.scroll({ to: ${to[1]} });`;
|
|
746
|
-
return ` await human.scroll(${q(s)});`;
|
|
747
|
-
}
|
|
748
|
-
function emitAction(e, opts = {}) {
|
|
749
|
-
const p = e.params;
|
|
750
|
-
switch (e.type) {
|
|
751
|
-
case "goto": {
|
|
752
|
-
const url = String(p.url ?? "");
|
|
753
|
-
if (opts.baseOrigin && url.startsWith(opts.baseOrigin)) {
|
|
754
|
-
return ` await human.goto(${q(url.slice(opts.baseOrigin.length) || "/")});`;
|
|
755
|
-
}
|
|
756
|
-
return ` await human.goto(${q(url)});`;
|
|
757
|
-
}
|
|
758
|
-
case "click":
|
|
759
|
-
case "rightClick":
|
|
760
|
-
case "hover":
|
|
761
|
-
case "move": {
|
|
762
|
-
const { code, isPoint: isPoint2 } = targetArg(p.target);
|
|
763
|
-
return ` await human.${e.type}(${code});${isPoint2 ? POINT_COMMENT : ""}`;
|
|
764
|
-
}
|
|
765
|
-
case "drag": {
|
|
766
|
-
const from = targetArg(p.from);
|
|
767
|
-
const to = targetArg(p.to);
|
|
768
|
-
const comment = from.isPoint || to.isPoint ? POINT_COMMENT : "";
|
|
769
|
-
return ` await human.drag(${from.code}, ${to.code});${comment}`;
|
|
770
|
-
}
|
|
771
|
-
case "type":
|
|
772
|
-
case "paste": {
|
|
773
|
-
const { code, isPoint: isPoint2 } = targetArg(p.target);
|
|
774
|
-
if (e.inputValue === void 0) {
|
|
775
|
-
return ` await human.${e.type}(${code}, '');${UNCAPTURED_COMMENT}`;
|
|
776
|
-
}
|
|
777
|
-
const call = ` await human.${e.type}(${code}, ${q(e.inputValue)});`;
|
|
778
|
-
if (opts.asserts && !isPoint2) {
|
|
779
|
-
return `${call}
|
|
780
|
-
await expect(page.locator(${code})).toHaveValue(${q(e.inputValue)});`;
|
|
781
|
-
}
|
|
782
|
-
return call;
|
|
783
|
-
}
|
|
784
|
-
case "press":
|
|
785
|
-
return ` await human.press(${q(p.key)});`;
|
|
786
|
-
case "scroll":
|
|
787
|
-
return emitScroll(p.target);
|
|
788
|
-
case "read": {
|
|
789
|
-
const desc = String(p.target ?? "");
|
|
790
|
-
if (/^\d+ words$/.test(desc) || /^text:\d+ chars$/.test(desc)) {
|
|
791
|
-
return ` // human.read(...) \u2014 ${desc}; original target not captured`;
|
|
792
|
-
}
|
|
793
|
-
const call = ` await human.read(${q(desc)});`;
|
|
794
|
-
if (opts.asserts) return `${call}
|
|
795
|
-
await expect(page.locator(${q(desc)})).toBeVisible();`;
|
|
796
|
-
return call;
|
|
797
|
-
}
|
|
798
|
-
case "sleep":
|
|
799
|
-
return ` await sleep(${Number(p.ms) || 0});`;
|
|
800
|
-
case "reload":
|
|
801
|
-
return " await human.reload();";
|
|
802
|
-
case "goBack":
|
|
803
|
-
return " await human.goBack();";
|
|
804
|
-
case "goForward":
|
|
805
|
-
return " await human.goForward();";
|
|
806
|
-
default:
|
|
807
|
-
return ` // unsupported action: ${e.type}`;
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
function needsSleepImport(timeline) {
|
|
811
|
-
return timeline.events.some((e) => e.type === "sleep");
|
|
812
|
-
}
|
|
813
|
-
function generateHumanJS(timeline) {
|
|
814
|
-
const imports = needsSleepImport(timeline) ? "import { chromium, createHuman, sleep } from '@humanjs/playwright';" : "import { chromium, createHuman } from '@humanjs/playwright';";
|
|
815
|
-
const body = timeline.events.map((e) => emitAction(e)).join("\n");
|
|
816
|
-
return `${imports}
|
|
817
|
-
|
|
818
|
-
async function main() {
|
|
819
|
-
const browser = await chromium.launch({ headless: false });
|
|
820
|
-
const page = await browser.newPage();
|
|
821
|
-
const human = await createHuman(page, ${createHumanOptions(timeline)});
|
|
822
|
-
|
|
823
|
-
${body}
|
|
824
|
-
|
|
825
|
-
await browser.close();
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
main();
|
|
829
|
-
`;
|
|
830
|
-
}
|
|
831
|
-
var NAV_TYPES = /* @__PURE__ */ new Set(["goto", "reload", "goBack", "goForward"]);
|
|
832
|
-
function sharedGotoOrigin(events) {
|
|
833
|
-
let origin;
|
|
834
|
-
for (const e of events) {
|
|
835
|
-
if (e.type !== "goto") continue;
|
|
836
|
-
try {
|
|
837
|
-
const o = new URL(String(e.params.url ?? "")).origin;
|
|
838
|
-
if (origin === void 0) origin = o;
|
|
839
|
-
else if (origin !== o) return void 0;
|
|
840
|
-
} catch {
|
|
841
|
-
return void 0;
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
return origin;
|
|
845
|
-
}
|
|
846
|
-
function indentLines(block, pad) {
|
|
847
|
-
return block.split("\n").map((line) => line.length > 0 ? pad + line : line).join("\n");
|
|
848
|
-
}
|
|
849
|
-
function stepLabel(event, index, baseOrigin) {
|
|
850
|
-
switch (event.type) {
|
|
851
|
-
case "goto": {
|
|
852
|
-
const url = String(event.params.url ?? "");
|
|
853
|
-
const path = baseOrigin && url.startsWith(baseOrigin) ? url.slice(baseOrigin.length) || "/" : url;
|
|
854
|
-
return `go to ${path}`;
|
|
855
|
-
}
|
|
856
|
-
case "reload":
|
|
857
|
-
return "reload";
|
|
858
|
-
case "goBack":
|
|
859
|
-
return "go back";
|
|
860
|
-
case "goForward":
|
|
861
|
-
return "go forward";
|
|
862
|
-
default:
|
|
863
|
-
return `step ${index + 1}`;
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
function emitSteps(events, opts) {
|
|
867
|
-
const groups = [];
|
|
868
|
-
for (const e of events) {
|
|
869
|
-
const last = groups[groups.length - 1];
|
|
870
|
-
if (last === void 0 || NAV_TYPES.has(e.type)) groups.push([e]);
|
|
871
|
-
else last.push(e);
|
|
872
|
-
}
|
|
873
|
-
return groups.map((group, i) => {
|
|
874
|
-
const [first] = group;
|
|
875
|
-
const label = first ? stepLabel(first, i, opts.baseOrigin) : `step ${i + 1}`;
|
|
876
|
-
const inner = group.map((e) => indentLines(emitAction(e, opts), " ")).join("\n");
|
|
877
|
-
return ` await test.step(${q(label)}, async () => {
|
|
878
|
-
${inner}
|
|
879
|
-
});`;
|
|
880
|
-
}).join("\n\n");
|
|
881
|
-
}
|
|
882
|
-
function generatePlaywrightTest(timeline, options = {}) {
|
|
883
|
-
const events = options.keepSleeps ? timeline.events : timeline.events.filter((e) => e.type !== "sleep");
|
|
884
|
-
const baseOrigin = options.baseUrl ? sharedGotoOrigin(events) : void 0;
|
|
885
|
-
const emitOpts = { asserts: true, baseOrigin };
|
|
886
|
-
const body = options.steps ? emitSteps(events, emitOpts) : events.map((e) => emitAction(e, emitOpts)).join("\n");
|
|
887
|
-
const needsSleep = events.some((e) => e.type === "sleep");
|
|
888
|
-
const hasAsserts = body.includes("await expect(");
|
|
889
|
-
const testImport = hasAsserts ? "import { expect, test } from '@playwright/test';" : "import { test } from '@playwright/test';";
|
|
890
|
-
const humanImport = needsSleep ? "import { createHuman, sleep } from '@humanjs/playwright';" : "import { createHuman } from '@humanjs/playwright';";
|
|
891
|
-
const title = options.title ?? timeline.name ?? "recorded session";
|
|
892
|
-
const baseUrlNote = baseOrigin ? ` // Set use.baseURL = ${q(baseOrigin)} in playwright.config.ts for these relative paths.
|
|
893
|
-
|
|
894
|
-
` : "";
|
|
895
|
-
const todo = [
|
|
896
|
-
hasAsserts ? " // TODO: add assertions for the outcome of this flow, e.g.:" : " // TODO: assert the outcome \u2014 import { expect } from '@playwright/test', e.g.:",
|
|
897
|
-
" // await expect(page).toHaveURL(/dashboard/);",
|
|
898
|
-
" // await expect(page.getByText('Welcome back')).toBeVisible();"
|
|
899
|
-
].join("\n");
|
|
900
|
-
return `${testImport}
|
|
901
|
-
${humanImport}
|
|
902
|
-
|
|
903
|
-
test(${q(title)}, async ({ page }) => {
|
|
904
|
-
const human = await createHuman(page, ${createHumanOptions(timeline, true)});
|
|
905
|
-
|
|
906
|
-
${baseUrlNote}${body}
|
|
907
|
-
|
|
908
|
-
${todo}
|
|
909
|
-
});
|
|
910
|
-
`;
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
// src/recording/index.ts
|
|
914
|
-
var pendingFrameCleanups = /* @__PURE__ */ new Set();
|
|
915
|
-
var exitHandlerInstalled = false;
|
|
916
|
-
function ensureExitHandler() {
|
|
917
|
-
if (exitHandlerInstalled) return;
|
|
918
|
-
exitHandlerInstalled = true;
|
|
919
|
-
process.on("exit", () => {
|
|
920
|
-
for (const dir of pendingFrameCleanups) {
|
|
921
|
-
try {
|
|
922
|
-
rmSync(dir, { recursive: true, force: true });
|
|
923
|
-
} catch {
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
pendingFrameCleanups.clear();
|
|
927
|
-
});
|
|
928
|
-
}
|
|
929
|
-
var FFMPEG_PATH = ffmpegStatic;
|
|
930
|
-
var QUALITY_PRESETS = {
|
|
931
|
-
fast: {
|
|
932
|
-
captureFormat: "jpeg",
|
|
933
|
-
captureJpegQuality: 85,
|
|
934
|
-
captureFps: 24,
|
|
935
|
-
crf: 23,
|
|
936
|
-
preset: "fast"
|
|
937
|
-
},
|
|
938
|
-
standard: {
|
|
939
|
-
captureFormat: "jpeg",
|
|
940
|
-
captureJpegQuality: 90,
|
|
941
|
-
captureFps: 30,
|
|
942
|
-
crf: 20,
|
|
943
|
-
preset: "fast"
|
|
944
|
-
},
|
|
945
|
-
high: {
|
|
946
|
-
captureFormat: "jpeg",
|
|
947
|
-
captureJpegQuality: 95,
|
|
948
|
-
captureFps: 30,
|
|
949
|
-
crf: 18,
|
|
950
|
-
preset: "slow",
|
|
951
|
-
// 'animation' suits screen content (large solid regions, sharp edges)
|
|
952
|
-
// better than 'film' which is tuned for live-action grain.
|
|
953
|
-
tune: "animation"
|
|
954
|
-
},
|
|
955
|
-
lossless: {
|
|
956
|
-
// PNG capture for perceptually lossless source frames. Temp files are
|
|
957
|
-
// 10-20× larger than JPEG; output mp4 still benefits from the extra
|
|
958
|
-
// headroom (no JPEG artifacts to preserve).
|
|
959
|
-
captureFormat: "png",
|
|
960
|
-
captureJpegQuality: 100,
|
|
961
|
-
captureFps: 30,
|
|
962
|
-
crf: 12,
|
|
963
|
-
preset: "veryslow",
|
|
964
|
-
tune: "animation"
|
|
965
|
-
}
|
|
966
|
-
};
|
|
967
|
-
function getCaptureSettingsForQuality(quality) {
|
|
968
|
-
const preset = QUALITY_PRESETS[quality];
|
|
969
|
-
return {
|
|
970
|
-
format: preset.captureFormat,
|
|
971
|
-
quality: preset.captureJpegQuality,
|
|
972
|
-
fps: preset.captureFps
|
|
973
|
-
};
|
|
974
|
-
}
|
|
975
|
-
var Recording = class {
|
|
976
|
-
#capture;
|
|
977
|
-
#windowStartMs;
|
|
978
|
-
#windowEndMs;
|
|
979
|
-
#timelineSource;
|
|
980
|
-
// Frames live on disk until `dispose()` is called. Exporters
|
|
981
|
-
// (`toVideo`, `toGif`) are repeatable and interleavable — they read the
|
|
982
|
-
// same frame source, they don't consume it.
|
|
983
|
-
#disposed = false;
|
|
984
|
-
constructor(capture, windowStartMs, windowEndMs, timelineSource) {
|
|
985
|
-
this.#capture = capture;
|
|
986
|
-
this.#windowStartMs = windowStartMs;
|
|
987
|
-
this.#windowEndMs = windowEndMs;
|
|
988
|
-
this.#timelineSource = timelineSource;
|
|
989
|
-
if (capture !== null) {
|
|
990
|
-
pendingFrameCleanups.add(capture.dir);
|
|
991
|
-
ensureExitHandler();
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
/** Wall-clock duration of the recorded window. */
|
|
995
|
-
get durationMs() {
|
|
996
|
-
return this.#windowEndMs - this.#windowStartMs;
|
|
997
|
-
}
|
|
998
|
-
/** True if frames were captured during this recording. */
|
|
999
|
-
get hasVideo() {
|
|
1000
|
-
return this.#capture !== null;
|
|
1001
|
-
}
|
|
1002
|
-
/**
|
|
1003
|
-
* The structured action timeline of this recording — same data that
|
|
1004
|
-
* `toTimeline()` writes to disk.
|
|
1005
|
-
*/
|
|
1006
|
-
get timeline() {
|
|
1007
|
-
return {
|
|
1008
|
-
version: 1,
|
|
1009
|
-
...this.#timelineSource.name !== void 0 ? { name: this.#timelineSource.name } : {},
|
|
1010
|
-
personality: this.#timelineSource.personality,
|
|
1011
|
-
seed: this.#timelineSource.seed,
|
|
1012
|
-
speed: this.#timelineSource.speed,
|
|
1013
|
-
durationMs: this.durationMs,
|
|
1014
|
-
events: this.#timelineSource.events
|
|
1015
|
-
};
|
|
1016
|
-
}
|
|
1017
|
-
/**
|
|
1018
|
-
* Assembles the captured frames into a video at `outputPath`. The output
|
|
1019
|
-
* format is inferred from the extension — `.mp4` (H.264, re-encoded
|
|
1020
|
-
* with the configured quality) or `.webm` (VP9).
|
|
1021
|
-
*
|
|
1022
|
-
* Repeatable and interleavable with `toGif()` — the frame source is read,
|
|
1023
|
-
* not consumed. Frames live until you call `dispose()` (or `await using`
|
|
1024
|
-
* goes out of scope, or the process exits and the OS reaps `tmpdir`).
|
|
1025
|
-
*
|
|
1026
|
-
* @returns the resolved output path.
|
|
1027
|
-
*/
|
|
1028
|
-
async toVideo(outputPath, options = {}) {
|
|
1029
|
-
if (this.#disposed) {
|
|
1030
|
-
throw new Error("Recording.toVideo() called after dispose() \u2014 the source frames are gone.");
|
|
1031
|
-
}
|
|
1032
|
-
if (this.#capture === null) {
|
|
1033
|
-
throw new Error(
|
|
1034
|
-
"Recording.toVideo() requires video capture, which was disabled for this recording. Call `human.record(cb)` (default captures video) or pass `output` to @humanjs/recorder's `record()`. `toTimeline()` and `.timeline` work without capture."
|
|
1035
|
-
);
|
|
1036
|
-
}
|
|
1037
|
-
const preset = QUALITY_PRESETS[options.quality ?? "high"];
|
|
1038
|
-
const crf = options.crf ?? preset.crf;
|
|
1039
|
-
const ffmpegPreset = options.preset ?? preset.preset;
|
|
1040
|
-
const tune = options.tune ?? preset.tune;
|
|
1041
|
-
const { dir, frames, startedAtMs, stoppedAtMs } = this.#capture;
|
|
1042
|
-
if (frames.length === 0) {
|
|
1043
|
-
throw new Error(
|
|
1044
|
-
"No frames were captured. The recording window may have been too short, or the page may not have rendered any frames before the callback completed."
|
|
1045
|
-
);
|
|
1046
|
-
}
|
|
1047
|
-
await mkdir(dirname(outputPath), { recursive: true });
|
|
1048
|
-
const ext = extname(outputPath).toLowerCase();
|
|
1049
|
-
if (ext !== ".mp4" && ext !== ".webm") {
|
|
1050
|
-
throw new Error(`Unsupported output extension: ${ext || "(none)"}. Use .mp4 or .webm.`);
|
|
1051
|
-
}
|
|
1052
|
-
const concatPath = `${dir}/concat.txt`;
|
|
1053
|
-
const concatBody = buildConcatFile(frames, stoppedAtMs - startedAtMs);
|
|
1054
|
-
await writeFile(concatPath, concatBody, "utf8");
|
|
1055
|
-
const args = ["-y", "-f", "concat", "-safe", "0", "-i", concatPath, "-vsync", "vfr"];
|
|
1056
|
-
if (ext === ".mp4") {
|
|
1057
|
-
args.push(
|
|
1058
|
-
"-c:v",
|
|
1059
|
-
"libx264",
|
|
1060
|
-
"-pix_fmt",
|
|
1061
|
-
"yuv420p",
|
|
1062
|
-
"-crf",
|
|
1063
|
-
String(crf),
|
|
1064
|
-
"-preset",
|
|
1065
|
-
ffmpegPreset
|
|
1066
|
-
);
|
|
1067
|
-
if (tune) args.push("-tune", tune);
|
|
1068
|
-
args.push("-movflags", "+faststart");
|
|
1069
|
-
} else {
|
|
1070
|
-
args.push(
|
|
1071
|
-
"-c:v",
|
|
1072
|
-
"libvpx-vp9",
|
|
1073
|
-
"-pix_fmt",
|
|
1074
|
-
"yuv420p",
|
|
1075
|
-
"-crf",
|
|
1076
|
-
String(crf),
|
|
1077
|
-
"-b:v",
|
|
1078
|
-
"0",
|
|
1079
|
-
"-deadline",
|
|
1080
|
-
ffmpegPreset === "fast" || ffmpegPreset === "veryfast" ? "realtime" : "good"
|
|
1081
|
-
);
|
|
1082
|
-
}
|
|
1083
|
-
args.push(outputPath);
|
|
1084
|
-
await runFfmpeg(args);
|
|
1085
|
-
return outputPath;
|
|
1086
|
-
}
|
|
1087
|
-
/**
|
|
1088
|
-
* Assembles the captured frames into an animated GIF at `outputPath`.
|
|
1089
|
-
* Optimized for embedding in READMEs, PRs, Slack, and docs — uses a
|
|
1090
|
-
* per-recording palette (`palettegen` + `paletteuse`) with Bayer dithering
|
|
1091
|
-
* so gradients stay smooth without exploding the file size.
|
|
1092
|
-
*
|
|
1093
|
-
* Repeatable and interleavable with `toVideo()` — call them in any order,
|
|
1094
|
-
* any number of times. Frames live until you call `dispose()`.
|
|
1095
|
-
*
|
|
1096
|
-
* @returns the resolved output path.
|
|
1097
|
-
*/
|
|
1098
|
-
async toGif(outputPath, options = {}) {
|
|
1099
|
-
if (this.#disposed) {
|
|
1100
|
-
throw new Error("Recording.toGif() called after dispose() \u2014 the source frames are gone.");
|
|
1101
|
-
}
|
|
1102
|
-
if (this.#capture === null) {
|
|
1103
|
-
throw new Error(
|
|
1104
|
-
"Recording.toGif() requires video capture, which was disabled for this recording. Call `human.record(cb)` (default captures video) or pass `output` to @humanjs/recorder's `record()`. `toTimeline()` and `.timeline` work without capture."
|
|
1105
|
-
);
|
|
1106
|
-
}
|
|
1107
|
-
const fps = options.fps ?? 15;
|
|
1108
|
-
const width = options.width;
|
|
1109
|
-
const { dir, frames, startedAtMs, stoppedAtMs } = this.#capture;
|
|
1110
|
-
if (frames.length === 0) {
|
|
1111
|
-
throw new Error(
|
|
1112
|
-
"No frames were captured. The recording window may have been too short, or the page may not have rendered any frames before the callback completed."
|
|
1113
|
-
);
|
|
1114
|
-
}
|
|
1115
|
-
await mkdir(dirname(outputPath), { recursive: true });
|
|
1116
|
-
const ext = extname(outputPath).toLowerCase();
|
|
1117
|
-
if (ext !== ".gif") {
|
|
1118
|
-
throw new Error(`Unsupported output extension: ${ext || "(none)"}. Use .gif.`);
|
|
1119
|
-
}
|
|
1120
|
-
const concatPath = `${dir}/concat.txt`;
|
|
1121
|
-
const concatBody = buildConcatFile(frames, stoppedAtMs - startedAtMs);
|
|
1122
|
-
await writeFile(concatPath, concatBody, "utf8");
|
|
1123
|
-
const filterSteps = [`fps=${fps}`];
|
|
1124
|
-
if (width !== void 0) {
|
|
1125
|
-
filterSteps.push(`scale=${width}:-1:flags=lanczos`);
|
|
1126
|
-
}
|
|
1127
|
-
const preFilter = filterSteps.join(",");
|
|
1128
|
-
const filterComplex = `${preFilter},split [a][b]; [a] palettegen=stats_mode=diff [p]; [b][p] paletteuse=dither=bayer:bayer_scale=5`;
|
|
1129
|
-
const args = [
|
|
1130
|
-
"-y",
|
|
1131
|
-
"-f",
|
|
1132
|
-
"concat",
|
|
1133
|
-
"-safe",
|
|
1134
|
-
"0",
|
|
1135
|
-
"-i",
|
|
1136
|
-
concatPath,
|
|
1137
|
-
"-filter_complex",
|
|
1138
|
-
filterComplex,
|
|
1139
|
-
"-loop",
|
|
1140
|
-
"0",
|
|
1141
|
-
outputPath
|
|
1142
|
-
];
|
|
1143
|
-
await runFfmpeg(args);
|
|
1144
|
-
return outputPath;
|
|
1145
|
-
}
|
|
1146
|
-
/**
|
|
1147
|
-
* Writes the structured action timeline to `outputPath` as JSON.
|
|
1148
|
-
* Independent of `toVideo()` / `toGif()` — call before, after, in between,
|
|
1149
|
-
* or instead. Safe to call multiple times. Unaffected by `dispose()`
|
|
1150
|
-
* (the timeline lives in memory, not in the captured-frames temp dir).
|
|
1151
|
-
*
|
|
1152
|
-
* @returns the resolved output path.
|
|
1153
|
-
*/
|
|
1154
|
-
async toTimeline(outputPath) {
|
|
1155
|
-
await mkdir(dirname(outputPath), { recursive: true });
|
|
1156
|
-
await writeFile(outputPath, `${JSON.stringify(this.timeline, null, 2)}
|
|
1157
|
-
`, "utf8");
|
|
1158
|
-
return outputPath;
|
|
1159
|
-
}
|
|
1160
|
-
/**
|
|
1161
|
-
* Generates a standalone, runnable HumanJS script from the timeline and
|
|
1162
|
-
* writes it to `outputPath`. String selectors round-trip verbatim; typed
|
|
1163
|
-
* values are included when `captureInputs` was on (passwords masked).
|
|
1164
|
-
*
|
|
1165
|
-
* Independent of frame capture — works on timeline-only recordings and is
|
|
1166
|
-
* unaffected by `dispose()`.
|
|
1167
|
-
*
|
|
1168
|
-
* @returns the resolved output path.
|
|
1169
|
-
*/
|
|
1170
|
-
async toHumanJS(outputPath) {
|
|
1171
|
-
await mkdir(dirname(outputPath), { recursive: true });
|
|
1172
|
-
await writeFile(outputPath, generateHumanJS(this.timeline), "utf8");
|
|
1173
|
-
return outputPath;
|
|
1174
|
-
}
|
|
1175
|
-
/**
|
|
1176
|
-
* Generates a `@playwright/test` spec from the timeline — a humanized test
|
|
1177
|
-
* (uses `createHuman` + `human.*`), not raw Playwright — and writes it to
|
|
1178
|
-
* `outputPath`. Runs instant in CI / recorded speed locally, drops timing
|
|
1179
|
-
* `sleep()`s (pass `{ keepSleeps: true }` to keep them), and derives the
|
|
1180
|
-
* assertions it safely can.
|
|
1181
|
-
*
|
|
1182
|
-
* Independent of frame capture — works on timeline-only recordings and is
|
|
1183
|
-
* unaffected by `dispose()`.
|
|
1184
|
-
*
|
|
1185
|
-
* @returns the resolved output path.
|
|
1186
|
-
*/
|
|
1187
|
-
async toPlaywright(outputPath, options) {
|
|
1188
|
-
await mkdir(dirname(outputPath), { recursive: true });
|
|
1189
|
-
await writeFile(outputPath, generatePlaywrightTest(this.timeline, options), "utf8");
|
|
1190
|
-
return outputPath;
|
|
1191
|
-
}
|
|
1192
|
-
/**
|
|
1193
|
-
* Releases the captured-frames temp directory. After this call, `toVideo()`
|
|
1194
|
-
* and `toGif()` throw — but `toTimeline()` and the in-memory `timeline`
|
|
1195
|
-
* still work because those don't depend on the frames.
|
|
1196
|
-
*
|
|
1197
|
-
* **Optional.** A process-exit handler also sweeps any un-disposed frame
|
|
1198
|
-
* dirs, so casual scripts can skip this entirely. Call it explicitly when
|
|
1199
|
-
* you want to release frames proactively (long-running services, batch
|
|
1200
|
-
* jobs, or anywhere you want predictable disk usage).
|
|
1201
|
-
*
|
|
1202
|
-
* Idempotent. Safe to call on a Recording that never had a capture
|
|
1203
|
-
* (timeline-only mode) — no-op there.
|
|
1204
|
-
*
|
|
1205
|
-
* Also wired to `Symbol.asyncDispose`, so the explicit-resource-management
|
|
1206
|
-
* `await using` syntax (TypeScript ≥ 5.2 / Node ≥ 20.4) works:
|
|
1207
|
-
*
|
|
1208
|
-
* ```ts
|
|
1209
|
-
* await using rec = await human.record(fn);
|
|
1210
|
-
* await rec.toVideo('demo.mp4');
|
|
1211
|
-
* await rec.toGif('demo.gif');
|
|
1212
|
-
* // frames cleaned up automatically when `rec` goes out of scope
|
|
1213
|
-
* ```
|
|
1214
|
-
*/
|
|
1215
|
-
async dispose() {
|
|
1216
|
-
if (this.#disposed) return;
|
|
1217
|
-
if (this.#capture !== null) {
|
|
1218
|
-
await this.#capture.cleanup();
|
|
1219
|
-
pendingFrameCleanups.delete(this.#capture.dir);
|
|
1220
|
-
}
|
|
1221
|
-
this.#disposed = true;
|
|
1222
|
-
}
|
|
1223
|
-
async [Symbol.asyncDispose]() {
|
|
1224
|
-
await this.dispose();
|
|
1225
|
-
}
|
|
1226
|
-
};
|
|
1227
|
-
function buildConcatFile(frames, totalMs) {
|
|
1228
|
-
const lines = [];
|
|
1229
|
-
for (let i = 0; i < frames.length; i++) {
|
|
1230
|
-
const frame = frames[i];
|
|
1231
|
-
const next = frames[i + 1];
|
|
1232
|
-
const nextTMs = next ? next.tMs : totalMs;
|
|
1233
|
-
const durationS = Math.max(1e-3, (nextTMs - frame.tMs) / 1e3);
|
|
1234
|
-
lines.push(`file '${frame.path.replaceAll("'", "'\\''")}'`);
|
|
1235
|
-
lines.push(`duration ${durationS.toFixed(6)}`);
|
|
1236
|
-
}
|
|
1237
|
-
const last = frames[frames.length - 1];
|
|
1238
|
-
if (last) {
|
|
1239
|
-
lines.push(`file '${last.path.replaceAll("'", "'\\''")}'`);
|
|
1240
|
-
}
|
|
1241
|
-
return `${lines.join("\n")}
|
|
1242
|
-
`;
|
|
1243
|
-
}
|
|
1244
|
-
function runFfmpeg(args) {
|
|
1245
|
-
if (!FFMPEG_PATH) {
|
|
1246
|
-
return Promise.reject(
|
|
1247
|
-
new Error(
|
|
1248
|
-
"ffmpeg-static did not bundle a binary for this platform. Install system ffmpeg and set FFMPEG_PATH, or run on a supported platform."
|
|
1249
|
-
)
|
|
1250
|
-
);
|
|
1251
|
-
}
|
|
1252
|
-
return new Promise((resolve, reject) => {
|
|
1253
|
-
const proc = spawn(FFMPEG_PATH, [...args]);
|
|
1254
|
-
let stderr = "";
|
|
1255
|
-
proc.stderr?.on("data", (chunk) => {
|
|
1256
|
-
stderr += chunk.toString();
|
|
1257
|
-
});
|
|
1258
|
-
proc.on("error", reject);
|
|
1259
|
-
proc.on("close", (code) => {
|
|
1260
|
-
if (code === 0) resolve();
|
|
1261
|
-
else reject(new Error(`ffmpeg exited with code ${code}
|
|
1262
|
-
${stderr.trim()}`));
|
|
1263
|
-
});
|
|
1264
|
-
});
|
|
1265
|
-
}
|
|
1266
|
-
async function startCapture(page, options = {}) {
|
|
1267
|
-
const format = options.format ?? "jpeg";
|
|
1268
|
-
const quality = options.quality ?? 95;
|
|
1269
|
-
const fps = Math.max(1, Math.min(60, options.fps ?? 30));
|
|
1270
|
-
const intervalMs = 1e3 / fps;
|
|
1271
|
-
const dir = await mkdtemp(join(tmpdir(), "humanjs-capture-"));
|
|
1272
|
-
const frames = [];
|
|
1273
|
-
const ext = format === "png" ? "png" : "jpg";
|
|
1274
|
-
let stopped = false;
|
|
1275
|
-
let frameIndex = 0;
|
|
1276
|
-
const writes = [];
|
|
1277
|
-
const startedAtMs = Date.now();
|
|
1278
|
-
const captureLoop = async () => {
|
|
1279
|
-
while (!stopped) {
|
|
1280
|
-
const loopStart = Date.now();
|
|
1281
|
-
try {
|
|
1282
|
-
const buf = await page.screenshot({
|
|
1283
|
-
type: format,
|
|
1284
|
-
quality: format === "jpeg" ? quality : void 0
|
|
1285
|
-
});
|
|
1286
|
-
if (stopped) return;
|
|
1287
|
-
const idx = frameIndex++;
|
|
1288
|
-
const path = join(dir, `frame_${String(idx).padStart(6, "0")}.${ext}`);
|
|
1289
|
-
const tMs = loopStart - startedAtMs;
|
|
1290
|
-
writes.push(
|
|
1291
|
-
writeFile(path, buf).then(
|
|
1292
|
-
() => {
|
|
1293
|
-
frames.push({ path, tMs });
|
|
1294
|
-
},
|
|
1295
|
-
(err) => {
|
|
1296
|
-
console.warn(`humanjs capture: write failed for frame ${idx}:`, err);
|
|
1297
|
-
}
|
|
1298
|
-
)
|
|
1299
|
-
);
|
|
1300
|
-
} catch (err) {
|
|
1301
|
-
if (stopped) return;
|
|
1302
|
-
console.warn("humanjs capture: screenshot failed, stopping loop:", err);
|
|
1303
|
-
stopped = true;
|
|
1304
|
-
return;
|
|
1305
|
-
}
|
|
1306
|
-
const elapsed = Date.now() - loopStart;
|
|
1307
|
-
const wait = intervalMs - elapsed;
|
|
1308
|
-
if (wait > 0) await sleep$1(wait);
|
|
1309
|
-
}
|
|
1310
|
-
};
|
|
1311
|
-
const loopPromise = captureLoop();
|
|
1312
|
-
const finish = async () => {
|
|
1313
|
-
stopped = true;
|
|
1314
|
-
await loopPromise;
|
|
1315
|
-
await Promise.allSettled(writes);
|
|
1316
|
-
};
|
|
1317
|
-
return {
|
|
1318
|
-
async stop() {
|
|
1319
|
-
await finish();
|
|
1320
|
-
const stoppedAtMs = Date.now();
|
|
1321
|
-
return {
|
|
1322
|
-
dir,
|
|
1323
|
-
frames: [...frames].sort((a, b) => a.tMs - b.tMs),
|
|
1324
|
-
startedAtMs,
|
|
1325
|
-
stoppedAtMs,
|
|
1326
|
-
format,
|
|
1327
|
-
fps,
|
|
1328
|
-
cleanup: () => rm(dir, { recursive: true, force: true }).then(() => void 0)
|
|
1329
|
-
};
|
|
1330
|
-
},
|
|
1331
|
-
async abort() {
|
|
1332
|
-
await finish();
|
|
1333
|
-
await rm(dir, { recursive: true, force: true }).catch(() => void 0);
|
|
1334
|
-
}
|
|
1335
|
-
};
|
|
1336
|
-
}
|
|
1337
|
-
|
|
1338
|
-
// src/mouse-helper/index.ts
|
|
1339
|
-
var CURSOR_PATH = "M 0 0 L 16 6 L 8 9.5 L 5 19 Z";
|
|
1340
|
-
var INSTALLED_FLAG = /* @__PURE__ */ Symbol.for("@humanjs/playwright:mouse-helper:installed");
|
|
1341
|
-
async function installMouseHelper(target, options = {}) {
|
|
1342
|
-
const tagged = target;
|
|
1343
|
-
if (tagged[INSTALLED_FLAG]) return;
|
|
1344
|
-
tagged[INSTALLED_FLAG] = true;
|
|
1345
|
-
const config = {
|
|
1346
|
-
color: options.color ?? "#f5a55c",
|
|
1347
|
-
stroke: "#020203",
|
|
1348
|
-
size: options.size ?? 22,
|
|
1349
|
-
showClicks: options.showClicks ?? true,
|
|
1350
|
-
haloOpacity: options.haloOpacity ?? 0.18,
|
|
1351
|
-
path: CURSOR_PATH
|
|
1352
|
-
};
|
|
1353
|
-
await target.addInitScript(installScript, config);
|
|
1354
|
-
const attachPageHooks = (page) => {
|
|
1355
|
-
page.on("domcontentloaded", () => {
|
|
1356
|
-
page.evaluate(installScript, config).catch(() => void 0);
|
|
1357
|
-
});
|
|
1358
|
-
};
|
|
1359
|
-
const pages = "pages" in target ? target.pages() : [target];
|
|
1360
|
-
for (const page of pages) attachPageHooks(page);
|
|
1361
|
-
if ("on" in target && "newPage" in target) {
|
|
1362
|
-
target.on("page", attachPageHooks);
|
|
1363
|
-
}
|
|
1364
|
-
await Promise.all(
|
|
1365
|
-
pages.map((page) => page.evaluate(installScript, config).catch(() => void 0))
|
|
1366
|
-
);
|
|
1367
|
-
}
|
|
1368
|
-
function installScript(config) {
|
|
1369
|
-
if (document.querySelector("[data-humanjs-cursor]")) return;
|
|
1370
|
-
const attach = () => {
|
|
1371
|
-
const cursor = document.createElement("div");
|
|
1372
|
-
cursor.setAttribute("aria-hidden", "true");
|
|
1373
|
-
cursor.setAttribute("data-humanjs-cursor", "true");
|
|
1374
|
-
cursor.style.cssText = [
|
|
1375
|
-
"position: fixed",
|
|
1376
|
-
"left: 0",
|
|
1377
|
-
"top: 0",
|
|
1378
|
-
`width: ${config.size}px`,
|
|
1379
|
-
`height: ${config.size + 4}px`,
|
|
1380
|
-
"pointer-events: none",
|
|
1381
|
-
"z-index: 2147483647",
|
|
1382
|
-
// Start visible at (0, 0) so the cursor is on screen from the moment
|
|
1383
|
-
// the page loads — without this the helper looks like nothing happened
|
|
1384
|
-
// until the first mousemove arrives.
|
|
1385
|
-
"opacity: 1",
|
|
1386
|
-
"transform: translate(0px, 0px)",
|
|
1387
|
-
// CSS interpolates between successive `mousemove` updates so the
|
|
1388
|
-
// cursor reads as continuous motion instead of discrete hops. Slightly
|
|
1389
|
-
// longer than the path-walker's typical step interval (~30–80ms) so
|
|
1390
|
-
// each tween is still settling when the next move lands → no pauses.
|
|
1391
|
-
"transition: transform 110ms ease-out, opacity 0.18s ease-out",
|
|
1392
|
-
"will-change: transform"
|
|
1393
|
-
].join("; ");
|
|
1394
|
-
const haloRadius = Math.round(config.size * 0.6);
|
|
1395
|
-
cursor.innerHTML = `
|
|
1396
|
-
<svg width="${config.size}" height="${config.size + 4}" viewBox="0 0 22 24" style="overflow: visible;">
|
|
1397
|
-
<circle cx="0" cy="0" r="${haloRadius}" fill="${config.color}" opacity="${config.haloOpacity}" />
|
|
1398
|
-
<path d="${config.path}" fill="${config.color}" stroke="${config.stroke}" stroke-width="0.7" stroke-linejoin="round" />
|
|
1399
|
-
</svg>
|
|
1400
|
-
`;
|
|
1401
|
-
document.body.appendChild(cursor);
|
|
1402
|
-
let lastX = 0;
|
|
1403
|
-
let lastY = 0;
|
|
1404
|
-
const onMove = (e) => {
|
|
1405
|
-
lastX = e.clientX;
|
|
1406
|
-
lastY = e.clientY;
|
|
1407
|
-
cursor.style.transform = `translate(${lastX}px, ${lastY}px)`;
|
|
1408
|
-
cursor.style.opacity = "1";
|
|
1409
|
-
};
|
|
1410
|
-
window.addEventListener("mousemove", onMove, { capture: true, passive: true });
|
|
1411
|
-
document.addEventListener("mousemove", onMove, { capture: true, passive: true });
|
|
1412
|
-
document.addEventListener(
|
|
1413
|
-
"mouseleave",
|
|
1414
|
-
() => {
|
|
1415
|
-
cursor.style.opacity = "0";
|
|
1416
|
-
},
|
|
1417
|
-
{ capture: true, passive: true }
|
|
1418
|
-
);
|
|
1419
|
-
if (config.showClicks) {
|
|
1420
|
-
const styleEl = document.createElement("style");
|
|
1421
|
-
styleEl.textContent = "@keyframes humanjs-ripple { 0% { transform: translate(-50%, -50%) scale(0.4); opacity: 0.9; } 100% { transform: translate(-50%, -50%) scale(2); opacity: 0; } }";
|
|
1422
|
-
document.head.appendChild(styleEl);
|
|
1423
|
-
window.addEventListener(
|
|
1424
|
-
"mousedown",
|
|
1425
|
-
() => {
|
|
1426
|
-
const ripple = document.createElement("div");
|
|
1427
|
-
ripple.style.cssText = [
|
|
1428
|
-
"position: fixed",
|
|
1429
|
-
`left: ${lastX}px`,
|
|
1430
|
-
`top: ${lastY}px`,
|
|
1431
|
-
"width: 28px",
|
|
1432
|
-
"height: 28px",
|
|
1433
|
-
"border-radius: 50%",
|
|
1434
|
-
`border: 1.5px solid ${config.color}`,
|
|
1435
|
-
"pointer-events: none",
|
|
1436
|
-
"z-index: 2147483646",
|
|
1437
|
-
"animation: humanjs-ripple 0.45s ease-out forwards"
|
|
1438
|
-
].join("; ");
|
|
1439
|
-
document.body.appendChild(ripple);
|
|
1440
|
-
window.setTimeout(() => ripple.remove(), 500);
|
|
1441
|
-
},
|
|
1442
|
-
{ capture: true, passive: true }
|
|
1443
|
-
);
|
|
1444
|
-
}
|
|
1445
|
-
};
|
|
1446
|
-
if (document.body) attach();
|
|
1447
|
-
else document.addEventListener("DOMContentLoaded", attach, { once: true });
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1450
|
-
// src/index.ts
|
|
1451
|
-
async function createHuman(page, options = {}) {
|
|
1452
|
-
const personality = resolvePersonality(options.personality ?? "careful");
|
|
1453
|
-
const rng = createRng(options.seed);
|
|
1454
|
-
const speed = options.speed ?? "human";
|
|
1455
|
-
const plugins = options.plugins ?? [];
|
|
1456
|
-
const context = { personality, rng };
|
|
1457
|
-
for (const plugin of plugins) {
|
|
1458
|
-
await plugin.install?.(context);
|
|
1459
|
-
}
|
|
1460
|
-
let hasRecorded = false;
|
|
1461
|
-
let activeRecordingEvents = null;
|
|
1462
|
-
let activeRecordingStartMs = 0;
|
|
1463
|
-
let activeRecordingCaptureInputs = false;
|
|
1464
|
-
async function performAction(action, actionFn, recordMeta) {
|
|
1465
|
-
for (const plugin of plugins) {
|
|
1466
|
-
await plugin.beforeAction?.(action);
|
|
1467
|
-
}
|
|
1468
|
-
const startedAt = Date.now();
|
|
1469
|
-
try {
|
|
1470
|
-
const value = await actionFn();
|
|
1471
|
-
const durationMs = Date.now() - startedAt;
|
|
1472
|
-
const result = { type: action.type, durationMs };
|
|
1473
|
-
if (activeRecordingEvents !== null && action.type !== "record") {
|
|
1474
|
-
activeRecordingEvents.push({
|
|
1475
|
-
type: action.type,
|
|
1476
|
-
params: action.params ?? {},
|
|
1477
|
-
tMs: startedAt - activeRecordingStartMs,
|
|
1478
|
-
durationMs,
|
|
1479
|
-
...recordMeta?.inputValue !== void 0 ? { inputValue: recordMeta.inputValue } : {}
|
|
1480
|
-
});
|
|
1481
|
-
}
|
|
1482
|
-
for (const plugin of plugins) {
|
|
1483
|
-
await plugin.afterAction?.(action, result);
|
|
1484
|
-
}
|
|
1485
|
-
return value;
|
|
1486
|
-
} catch (error) {
|
|
1487
|
-
if (activeRecordingEvents !== null && action.type !== "record") {
|
|
1488
|
-
activeRecordingEvents.push({
|
|
1489
|
-
type: action.type,
|
|
1490
|
-
params: action.params ?? {},
|
|
1491
|
-
tMs: startedAt - activeRecordingStartMs,
|
|
1492
|
-
durationMs: Date.now() - startedAt,
|
|
1493
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1494
|
-
...recordMeta?.inputValue !== void 0 ? { inputValue: recordMeta.inputValue } : {}
|
|
1495
|
-
});
|
|
1496
|
-
}
|
|
1497
|
-
for (const plugin of plugins) {
|
|
1498
|
-
await plugin.onError?.(action, error);
|
|
1499
|
-
}
|
|
1500
|
-
throw error;
|
|
1501
|
-
}
|
|
1502
|
-
}
|
|
1503
|
-
let lastMousePosition = options.initialMousePosition ?? { x: 0, y: 0 };
|
|
1504
|
-
const mouseCtx = () => ({
|
|
1505
|
-
page,
|
|
1506
|
-
personality,
|
|
1507
|
-
rng,
|
|
1508
|
-
speed,
|
|
1509
|
-
getMousePosition: () => lastMousePosition,
|
|
1510
|
-
setMousePosition: (point) => {
|
|
1511
|
-
lastMousePosition = point;
|
|
1512
|
-
}
|
|
1513
|
-
});
|
|
1514
|
-
const describeMouseTarget = (target) => {
|
|
1515
|
-
if (typeof target === "string") return target;
|
|
1516
|
-
if ("x" in target && "y" in target && typeof target.x === "number") {
|
|
1517
|
-
return `point(${target.x}, ${target.y})`;
|
|
1518
|
-
}
|
|
1519
|
-
return target.toString?.() ?? "locator";
|
|
1520
|
-
};
|
|
1521
|
-
const isPointTarget = (target) => typeof target !== "string" && "x" in target && "y" in target && typeof target.x === "number";
|
|
1522
|
-
const captureInputValue = async (target, value) => {
|
|
1523
|
-
if (activeRecordingEvents === null || !activeRecordingCaptureInputs) return void 0;
|
|
1524
|
-
const locator = typeof target === "string" ? page.locator(target) : isPointTarget(target) ? null : target;
|
|
1525
|
-
if (locator !== null) {
|
|
1526
|
-
try {
|
|
1527
|
-
const fieldType = await locator.first().getAttribute("type", { timeout: 1e3 });
|
|
1528
|
-
if (fieldType?.toLowerCase() === "password") {
|
|
1529
|
-
return void 0;
|
|
1530
|
-
}
|
|
1531
|
-
} catch {
|
|
1532
|
-
}
|
|
1533
|
-
}
|
|
1534
|
-
return value;
|
|
1535
|
-
};
|
|
1536
|
-
return {
|
|
1537
|
-
personality,
|
|
1538
|
-
speed,
|
|
1539
|
-
async goto(url) {
|
|
1540
|
-
await performAction({ type: "goto", params: { url } }, async () => {
|
|
1541
|
-
await page.goto(url);
|
|
1542
|
-
});
|
|
1543
|
-
},
|
|
1544
|
-
async click(target) {
|
|
1545
|
-
const description = describeMouseTarget(target);
|
|
1546
|
-
await performAction({ type: "click", params: { target: description } }, async () => {
|
|
1547
|
-
await executeClick(target, mouseCtx());
|
|
1548
|
-
});
|
|
1549
|
-
},
|
|
1550
|
-
async rightClick(target) {
|
|
1551
|
-
const description = describeMouseTarget(target);
|
|
1552
|
-
await performAction({ type: "rightClick", params: { target: description } }, async () => {
|
|
1553
|
-
await executeClick(target, mouseCtx(), { button: "right" });
|
|
1554
|
-
});
|
|
1555
|
-
},
|
|
1556
|
-
async hover(target) {
|
|
1557
|
-
const description = describeMouseTarget(target);
|
|
1558
|
-
await performAction({ type: "hover", params: { target: description } }, async () => {
|
|
1559
|
-
await executeHover(target, mouseCtx());
|
|
1560
|
-
});
|
|
1561
|
-
},
|
|
1562
|
-
async move(target) {
|
|
1563
|
-
const description = describeMouseTarget(target);
|
|
1564
|
-
await performAction({ type: "move", params: { target: description } }, async () => {
|
|
1565
|
-
await executeMove(target, mouseCtx());
|
|
1566
|
-
});
|
|
1567
|
-
},
|
|
1568
|
-
async drag(from, to) {
|
|
1569
|
-
const fromDesc = describeMouseTarget(from);
|
|
1570
|
-
const toDesc = describeMouseTarget(to);
|
|
1571
|
-
await performAction({ type: "drag", params: { from: fromDesc, to: toDesc } }, async () => {
|
|
1572
|
-
await executeDrag(from, to, mouseCtx());
|
|
1573
|
-
});
|
|
1574
|
-
},
|
|
1575
|
-
async type(target, value) {
|
|
1576
|
-
const description = describeMouseTarget(target);
|
|
1577
|
-
const inputValue = await captureInputValue(target, value);
|
|
1578
|
-
await performAction(
|
|
1579
|
-
{ type: "type", params: { target: description, length: value.length } },
|
|
1580
|
-
async () => {
|
|
1581
|
-
if (speed !== "instant" && value.length > 0) {
|
|
1582
|
-
await executeClick(target, mouseCtx());
|
|
1583
|
-
}
|
|
1584
|
-
await executeType(target, value, { page, personality, rng, speed });
|
|
1585
|
-
},
|
|
1586
|
-
inputValue !== void 0 ? { inputValue } : void 0
|
|
1587
|
-
);
|
|
1588
|
-
},
|
|
1589
|
-
async paste(target, value) {
|
|
1590
|
-
const description = describeMouseTarget(target);
|
|
1591
|
-
const inputValue = await captureInputValue(target, value);
|
|
1592
|
-
await performAction(
|
|
1593
|
-
{ type: "paste", params: { target: description, length: value.length } },
|
|
1594
|
-
async () => {
|
|
1595
|
-
if (speed !== "instant" && value.length > 0) {
|
|
1596
|
-
await executeClick(target, mouseCtx());
|
|
1597
|
-
}
|
|
1598
|
-
await executePaste(target, value, { page, personality, rng, speed });
|
|
1599
|
-
},
|
|
1600
|
-
inputValue !== void 0 ? { inputValue } : void 0
|
|
1601
|
-
);
|
|
1602
|
-
},
|
|
1603
|
-
async press(key) {
|
|
1604
|
-
await performAction({ type: "press", params: { key } }, async () => {
|
|
1605
|
-
await executePress(key, { page, personality, rng, speed });
|
|
1606
|
-
});
|
|
1607
|
-
},
|
|
1608
|
-
async read(target, options2) {
|
|
1609
|
-
const description = describeReadTarget(target);
|
|
1610
|
-
return performAction(
|
|
1611
|
-
{
|
|
1612
|
-
type: "read",
|
|
1613
|
-
params: {
|
|
1614
|
-
target: description,
|
|
1615
|
-
kind: options2?.kind
|
|
1616
|
-
}
|
|
1617
|
-
},
|
|
1618
|
-
() => executeRead(
|
|
1619
|
-
target,
|
|
1620
|
-
{
|
|
1621
|
-
page,
|
|
1622
|
-
personality,
|
|
1623
|
-
rng,
|
|
1624
|
-
speed,
|
|
1625
|
-
// Read shares the session's tracked cursor position so an eye
|
|
1626
|
-
// scan starts from where the last click left off, and the next
|
|
1627
|
-
// click starts from where the scan ended.
|
|
1628
|
-
getMousePosition: () => lastMousePosition,
|
|
1629
|
-
setMousePosition: (point) => {
|
|
1630
|
-
lastMousePosition = point;
|
|
1631
|
-
}
|
|
1632
|
-
},
|
|
1633
|
-
options2
|
|
1634
|
-
)
|
|
1635
|
-
);
|
|
1636
|
-
},
|
|
1637
|
-
async scroll(target, options2) {
|
|
1638
|
-
const description = describeScrollTarget(target);
|
|
1639
|
-
return performAction(
|
|
1640
|
-
{
|
|
1641
|
-
type: "scroll",
|
|
1642
|
-
params: { target: description }
|
|
1643
|
-
},
|
|
1644
|
-
() => executeScroll(target, { page, personality, rng, speed }, options2)
|
|
1645
|
-
);
|
|
1646
|
-
},
|
|
1647
|
-
async sleep(ms) {
|
|
1648
|
-
await performAction({ type: "sleep", params: { ms } }, () => sleep$1(ms));
|
|
1649
|
-
},
|
|
1650
|
-
async record(optionsOrFn, maybeFn) {
|
|
1651
|
-
const [recordOptions, fn] = typeof optionsOrFn === "function" ? [{}, optionsOrFn] : [optionsOrFn, maybeFn];
|
|
1652
|
-
if (hasRecorded) {
|
|
1653
|
-
throw new Error(
|
|
1654
|
-
"human.record() can only be called once per session. Create a new browser context (and a new human session) to record a separate clip."
|
|
1655
|
-
);
|
|
1656
|
-
}
|
|
1657
|
-
hasRecorded = true;
|
|
1658
|
-
const captureEnabled = recordOptions.video !== false;
|
|
1659
|
-
const captureQuality = recordOptions.quality ?? "high";
|
|
1660
|
-
let captureSession = null;
|
|
1661
|
-
if (captureEnabled) {
|
|
1662
|
-
const { format, quality, fps } = getCaptureSettingsForQuality(captureQuality);
|
|
1663
|
-
captureSession = await startCapture(page, { format, quality, fps });
|
|
1664
|
-
}
|
|
1665
|
-
const events = [];
|
|
1666
|
-
const windowStartMs = Date.now();
|
|
1667
|
-
activeRecordingEvents = events;
|
|
1668
|
-
activeRecordingStartMs = windowStartMs;
|
|
1669
|
-
activeRecordingCaptureInputs = recordOptions.captureInputs !== false;
|
|
1670
|
-
let windowEndMs = windowStartMs;
|
|
1671
|
-
try {
|
|
1672
|
-
await performAction({ type: "record", params: {} }, async () => {
|
|
1673
|
-
try {
|
|
1674
|
-
await fn();
|
|
1675
|
-
} finally {
|
|
1676
|
-
windowEndMs = Date.now();
|
|
1677
|
-
}
|
|
1678
|
-
});
|
|
1679
|
-
} catch (error) {
|
|
1680
|
-
if (captureSession) await captureSession.abort();
|
|
1681
|
-
throw error;
|
|
1682
|
-
} finally {
|
|
1683
|
-
activeRecordingEvents = null;
|
|
1684
|
-
activeRecordingCaptureInputs = false;
|
|
1685
|
-
}
|
|
1686
|
-
const captureResult = captureSession ? await captureSession.stop() : null;
|
|
1687
|
-
return new Recording(captureResult, windowStartMs, windowEndMs, {
|
|
1688
|
-
name: recordOptions.name,
|
|
1689
|
-
personality: personality.name,
|
|
1690
|
-
seed: options.seed === void 0 ? null : String(options.seed),
|
|
1691
|
-
speed,
|
|
1692
|
-
events
|
|
1693
|
-
});
|
|
1694
|
-
},
|
|
1695
|
-
// ────────────────────────────────────────────────────────────────────
|
|
1696
|
-
// Thin re-exports of common Playwright `Page` methods. See the `Human`
|
|
1697
|
-
// interface for the rationale; implementations forward unchanged.
|
|
1698
|
-
// ────────────────────────────────────────────────────────────────────
|
|
1699
|
-
screenshot(opts) {
|
|
1700
|
-
return page.screenshot(opts);
|
|
1701
|
-
},
|
|
1702
|
-
pageText() {
|
|
1703
|
-
return page.innerText("body");
|
|
1704
|
-
},
|
|
1705
|
-
content() {
|
|
1706
|
-
return page.content();
|
|
1707
|
-
},
|
|
1708
|
-
url() {
|
|
1709
|
-
return page.url();
|
|
1710
|
-
},
|
|
1711
|
-
title() {
|
|
1712
|
-
return page.title();
|
|
1713
|
-
},
|
|
1714
|
-
async reload(opts) {
|
|
1715
|
-
await performAction({ type: "reload", params: {} }, async () => {
|
|
1716
|
-
await page.reload(opts);
|
|
1717
|
-
});
|
|
1718
|
-
},
|
|
1719
|
-
async goBack(opts) {
|
|
1720
|
-
await performAction({ type: "goBack", params: {} }, async () => {
|
|
1721
|
-
await page.goBack(opts);
|
|
1722
|
-
});
|
|
1723
|
-
},
|
|
1724
|
-
async goForward(opts) {
|
|
1725
|
-
await performAction({ type: "goForward", params: {} }, async () => {
|
|
1726
|
-
await page.goForward(opts);
|
|
1727
|
-
});
|
|
1728
|
-
},
|
|
1729
|
-
waitForLoadState(state, opts) {
|
|
1730
|
-
return page.waitForLoadState(state, opts);
|
|
1731
|
-
},
|
|
1732
|
-
waitForURL(url, opts) {
|
|
1733
|
-
return page.waitForURL(url, opts);
|
|
1734
|
-
},
|
|
1735
|
-
setViewportSize(size) {
|
|
1736
|
-
return page.setViewportSize(size);
|
|
1737
|
-
},
|
|
1738
|
-
pdf(opts) {
|
|
1739
|
-
return page.pdf(opts);
|
|
1740
|
-
}
|
|
1741
|
-
};
|
|
1742
|
-
}
|
|
1743
|
-
function describeScrollTarget(target) {
|
|
1744
|
-
if (target === void 0) return "natural";
|
|
1745
|
-
if (typeof target === "string") return target;
|
|
1746
|
-
if ("by" in target) return `by:${target.by}`;
|
|
1747
|
-
if ("to" in target) return `to:${target.to}`;
|
|
1748
|
-
return target.toString?.() ?? "locator";
|
|
1749
|
-
}
|
|
1750
|
-
function describeReadTarget(target) {
|
|
1751
|
-
if (typeof target === "string") return target;
|
|
1752
|
-
if ("words" in target && typeof target.words === "number") return `${target.words} words`;
|
|
1753
|
-
if ("text" in target && typeof target.text === "string") {
|
|
1754
|
-
return `text:${target.text.length} chars`;
|
|
1755
|
-
}
|
|
1756
|
-
return target.toString?.() ?? "locator";
|
|
1757
|
-
}
|
|
1758
|
-
|
|
1759
|
-
export { Recording, createHuman, installMouseHelper };
|
|
1
|
+
export { Recording, applyMicroJitter, applyVelocityProfile, bezierPath, blend, careful, chromium, computeReadingDwellMs, countWords, createHuman, createRng, distracted, fast, firefox, humanizePath, installMouseHelper, planScroll, planTypeKeystrokes, precise, resolvePersonality, sleep, webkit } from './chunk-CCZEDNOF.js';
|
|
1760
2
|
//# sourceMappingURL=index.js.map
|
|
1761
3
|
//# sourceMappingURL=index.js.map
|