- docs/e2e: 将 DAG 演示面板(dag-panel.mjs/html/data.mjs/sh/DAG演示.command)用 git mv 归入 dag-panel/ 子目录;修正相对依赖 ../lib、../results/dag-runs;清理 _diag_*.mjs 等临时诊断脚本(git rm 已跟踪项,rm 未跟踪项) - web: 修复头部用户名称切账号不变(新增 utils/user.ts 响应式 userName,登录/挂载/路由切换时拉取 /oauth/CurrentUser) - .gitignore: 新增 __pycache__/*.pyc 忽略规则(排除 demo 构建产物) - demo: 多个演示脚本与算子集市用例更新 - .workbuddy/memory: 补充本次工作记录 注意:docs/e2e/sswflow 与 **/__pycache__/*.pyc 按要求排除,未纳入。
305 lines
12 KiB
JavaScript
305 lines
12 KiB
JavaScript
/**
|
||
* human.mjs —— 人类行为模拟工具
|
||
*
|
||
* 把 Playwright 的"程序式点击"(mouse 直接 jump 到目标 + 瞬间 click)改成"真人操作":
|
||
* - 鼠标移动走贝塞尔曲线路径(中间帧带 jitter),不是直线跳
|
||
* - 点击前短暂悬停(150–400ms),模拟"瞄一下"
|
||
* - 字符级 type,每字符 30–120ms 随机间隔
|
||
* - 操作间自然停顿(80–280ms),不会瞬切
|
||
* - 视口偶尔微动(页面打开后、关键节点前),模拟"看屏幕"
|
||
* - 滚动自然加减速
|
||
*
|
||
* 所有包装函数都接受 Playwright 的 Page/Locator,与 DSL 现有调用无缝兼容:
|
||
* await humanClick(page, locator) // 取代 await locator.click()
|
||
* await humanFill(page, locator, '文本') // 取代 await locator.fill('文本')
|
||
*
|
||
* 仅"导航到 URL"是程序式的(浏览器跳转必须程序化),但导航后立刻视口微动。
|
||
*/
|
||
|
||
const DEFAULT_VIEWPORT_W = 1440
|
||
const DEFAULT_VIEWPORT_H = 900
|
||
|
||
/**
|
||
* 随机延迟,单位 ms
|
||
*/
|
||
export function jitter(min, max) {
|
||
const lo = Math.min(min, max)
|
||
const hi = Math.max(min, max)
|
||
return Math.round(lo + Math.random() * (hi - lo))
|
||
}
|
||
|
||
/**
|
||
* 鼠标当前位置;不可知时返回视口中央稍偏左
|
||
*/
|
||
async function currentPos(page) {
|
||
return await page.evaluate(() => {
|
||
// mouse 只在 page 上有 .mouse 私有 API,外部拿不到当前位置;
|
||
// 用视口中央 + 一点 jitter 作为"上次落点"的近似
|
||
const w = window.innerWidth || 1440
|
||
const h = window.innerHeight || 900
|
||
const jx = (Math.sin(performance.now() / 1000) * 0.5 + 0.5) * w * 0.3 + w * 0.35
|
||
const jy = (Math.sin(performance.now() / 700 + 1) * 0.5 + 0.5) * h * 0.2 + h * 0.4
|
||
return { x: jx, y: jy }
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 三阶贝塞尔曲线生成器 —— 起点 → 控制点1 → 控制点2 → 终点
|
||
* 中间控制点带 ±40% 路径长度的随机偏移,模拟"手抖"
|
||
*/
|
||
function bezierPath(from, to, steps) {
|
||
const dx = to.x - from.x
|
||
const dy = to.y - from.y
|
||
const dist = Math.sqrt(dx * dx + dy * dy)
|
||
const wobble = Math.min(120, dist * 0.18)
|
||
// 控制点1:在 from→to 路径 30% 处,垂直方向偏移
|
||
const a1x = from.x + dx * 0.3 + (Math.random() - 0.5) * wobble
|
||
const a1y = from.y + dy * 0.3 + (Math.random() - 0.5) * wobble
|
||
// 控制点2:在 from→to 路径 70% 处,垂直方向偏移
|
||
const a2x = from.x + dx * 0.7 + (Math.random() - 0.5) * wobble
|
||
const a2y = from.y + dy * 0.7 + (Math.random() - 0.5) * wobble
|
||
const pts = []
|
||
for (let i = 0; i <= steps; i++) {
|
||
const t = i / steps
|
||
const u = 1 - t
|
||
const x = u * u * u * from.x + 3 * u * u * t * a1x + 3 * u * t * t * a2x + t * t * t * to.x
|
||
const y = u * u * u * from.y + 3 * u * u * t * a1y + 3 * u * t * t * a2y + t * t * t * to.y
|
||
pts.push({ x, y })
|
||
}
|
||
return pts
|
||
}
|
||
|
||
/**
|
||
* 取元素中心点(带小抖动,不永远正中)
|
||
*/
|
||
async function centerOf(locator) {
|
||
const box = await locator.boundingBox()
|
||
if (!box) return null
|
||
const cx = box.x + box.width / 2
|
||
const cy = box.y + box.height / 2
|
||
// 抖动范围 ±30% 元素尺寸,最小 ±3px —— 不超出元素边界
|
||
const jx = Math.max(3, box.width * 0.15) * (Math.random() - 0.5)
|
||
const jy = Math.max(3, box.height * 0.15) * (Math.random() - 0.5)
|
||
return { x: Math.round(cx + jx), y: Math.round(cy + jy) }
|
||
}
|
||
|
||
/**
|
||
* 沿贝塞尔曲线 mouse.move 到目标坐标
|
||
* steps 与每步延迟基于路径距离自适应:长路径走多步、短路径走少步
|
||
*/
|
||
async function moveTo(page, target, { from = null, steps = null } = {}) {
|
||
const start = from || (await currentPos(page))
|
||
const dist = Math.sqrt((target.x - start.x) ** 2 + (target.y - start.y) ** 2)
|
||
// 8–28 步,距离越长步数越多;每步 4–12ms(让 CDP dispatch 跟得上)
|
||
const n = steps ?? Math.max(8, Math.min(28, Math.round(dist / 40)))
|
||
const path = bezierPath(start, target, n)
|
||
for (const p of path) {
|
||
await page.mouse.move(p.x, p.y, { steps: 1 })
|
||
await page.waitForTimeout(jitter(2, 6))
|
||
}
|
||
await page.mouse.move(target.x, target.y, { steps: 1 })
|
||
}
|
||
|
||
/**
|
||
* 真实人类点击:移动 → 悬停 → mousedown 50–110ms → mouseup
|
||
*
|
||
* opts:
|
||
* - button: 'left' (默认) | 'right' | 'middle'
|
||
* - hoverMs: 悬停时长区间 [min, max]
|
||
* - skipHover: true 时跳过悬停(连续点击同一区域时)
|
||
* - preMove: true 时(默认)从当前位置移动;false 时直接 click(不推荐)
|
||
*/
|
||
export async function humanClick(page, locator, opts = {}) {
|
||
const { button = 'left', hoverMs = [120, 320], skipHover = false } = opts
|
||
const target = await centerOf(locator)
|
||
if (!target) throw new Error('元素未在视口/无 boundingBox')
|
||
// 1) 滚动到元素可见(真人会先用滚轮让它进视口)
|
||
await locator.scrollIntoViewIfNeeded().catch(() => {})
|
||
// 2) 重新计算中心(滚动后位置变了)
|
||
const fresh = await centerOf(locator)
|
||
if (fresh) Object.assign(target, fresh)
|
||
// 3) 鼠标沿曲线移动到目标
|
||
await moveTo(page, target)
|
||
// 4) 短暂悬停(瞄一下)
|
||
if (!skipHover) await page.waitForTimeout(jitter(hoverMs[0], hoverMs[1]))
|
||
// 5) 真实按下/抬起(带 jitter,模拟手指物理延迟)
|
||
await page.mouse.down({ button })
|
||
await page.waitForTimeout(jitter(40, 110))
|
||
await page.mouse.up({ button })
|
||
// 6) 抬起后短暂停顿
|
||
await page.waitForTimeout(jitter(60, 180))
|
||
}
|
||
|
||
/**
|
||
* 双击:两次 click 中间间隔 80–180ms(真人双击间隔比单击慢)
|
||
*/
|
||
export async function humanDblClick(page, locator, opts = {}) {
|
||
await humanClick(page, locator, opts)
|
||
await page.waitForTimeout(jitter(80, 180))
|
||
await humanClick(page, locator, { ...opts, skipHover: true })
|
||
}
|
||
|
||
/**
|
||
* 真实人类输入:清空 → 字符级 type(每字符 jitter 间隔)
|
||
*
|
||
* 与 locator.fill 不同:fill 是"瞬间设值"(直接派发 input 事件,不走键盘),
|
||
* humanType 走 page.keyboard.type,触发完整 keydown/keypress/keyup,
|
||
* 而且每字符间隔不同(人类打字有快有慢、有停顿)。
|
||
*
|
||
* 对 antd 等受控组件,input/change 事件被监听 → 真实键入才是"人"。
|
||
*/
|
||
export async function humanFill(page, locator, text, opts = {}) {
|
||
const { clearFirst = true, perChar = [25, 110], fastThreshold = 30 } = opts
|
||
const s = text != null ? String(text) : ''
|
||
// 1) 点中元素拿到焦点
|
||
await humanClick(page, locator, { hoverMs: [80, 200] })
|
||
// 2) 清空:用真实人类按键(带按下/抬起抖动)
|
||
// 注:Ctrl+A 走 keyboard.down 会被识别为「Unknown key 'Control+A'」,
|
||
// 这里用 page.keyboard.press 让 Playwright 内部处理修饰键语义,
|
||
// 前后各加 waitForTimeout 模拟人类按下时的反应时间
|
||
if (clearFirst) {
|
||
await page.keyboard.press('Control+A')
|
||
await page.waitForTimeout(jitter(60, 140))
|
||
await page.keyboard.press('Delete')
|
||
await page.waitForTimeout(jitter(80, 180))
|
||
}
|
||
// 3) 字符级 type:长文本(> fastThreshold)走"模拟粘贴"——playwright fill() 一次性写入并派发 input 事件,
|
||
// antd v-model 会同步。模拟用户从剪贴板粘贴:focus + Meta+V。但 clipboard 需 user gesture,
|
||
// 用 fill() 配合 dispatchEvent('input', {bubbles:true}) 是最稳的"人类粘贴等价"。对短文本仍走逐字符 type。
|
||
if (s.length > fastThreshold) {
|
||
await locator.fill(s)
|
||
} else if (s !== '') {
|
||
for (let i = 0; i < s.length; i++) {
|
||
const ch = s[i]
|
||
await page.keyboard.type(ch, { delay: jitter(perChar[0], perChar[1]) })
|
||
// 中文/标点后偶尔多停一下("思考")
|
||
if (/[,。!?、;:]/.test(ch) && Math.random() < 0.4) {
|
||
await page.waitForTimeout(jitter(120, 260))
|
||
}
|
||
}
|
||
}
|
||
// 4) 收尾短暂停顿(让 onBlur 之类生效)
|
||
await page.waitForTimeout(jitter(80, 220))
|
||
}
|
||
|
||
/**
|
||
* 在已聚焦元素上追加输入(不清空)
|
||
*/
|
||
export async function humanType(page, text, opts = {}) {
|
||
const { perChar = [25, 110] } = opts
|
||
if (text == null || text === '') return
|
||
const s = String(text)
|
||
for (let i = 0; i < s.length; i++) {
|
||
await page.keyboard.type(s[i], { delay: jitter(perChar[0], perChar[1]) })
|
||
}
|
||
await page.waitForTimeout(jitter(60, 180))
|
||
}
|
||
|
||
/**
|
||
* 真实人类悬停:移动 + 停留
|
||
*/
|
||
export async function humanHover(page, locator, opts = {}) {
|
||
const { holdMs = [200, 600] } = opts
|
||
await locator.scrollIntoViewIfNeeded().catch(() => {})
|
||
const target = await centerOf(locator)
|
||
if (!target) return
|
||
await moveTo(page, target)
|
||
await page.waitForTimeout(jitter(holdMs[0], holdMs[1]))
|
||
}
|
||
|
||
/**
|
||
* 自然滚动到元素:先朝元素方向滑几帧,每帧位置递减(自然减速)
|
||
*/
|
||
export async function humanScrollTo(page, locator, { overshoot = 0.15 } = {}) {
|
||
await locator.scrollIntoViewIfNeeded().catch(() => {})
|
||
await page.waitForTimeout(jitter(80, 220))
|
||
// 微调:先 scrollIntoView 滚过头再回退(真人滑过头会修正)
|
||
if (Math.random() < 0.35) {
|
||
const box = await locator.boundingBox().catch(() => null)
|
||
if (box) {
|
||
const direction = box.y < 0 ? 1 : -1
|
||
await page.mouse.wheel(0, direction * Math.round(Math.abs(box.y) * 0.3 * (1 + overshoot)))
|
||
await page.waitForTimeout(jitter(80, 200))
|
||
await locator.scrollIntoViewIfNeeded().catch(() => {})
|
||
}
|
||
}
|
||
await page.waitForTimeout(jitter(60, 180))
|
||
}
|
||
|
||
/**
|
||
* 视口自然微动:页面打开后或长时间无操作时触发,模拟"看屏幕"
|
||
*
|
||
* - 在视口中央 ±80px 内随机移动鼠标
|
||
* - 偶尔触发 wheel 一次小幅度滚动(向上 5–25px)
|
||
* - 总耗时 200–500ms
|
||
*/
|
||
export async function humanViewportFlicker(page, opts = {}) {
|
||
const { intensity = 'normal' } = opts
|
||
if (!page || page.isClosed()) return
|
||
const w = await page.evaluate(() => window.innerWidth || 1440).catch(() => 1440)
|
||
const h = await page.evaluate(() => window.innerHeight || 900).catch(() => 900)
|
||
const cx = w / 2
|
||
const cy = h / 2
|
||
const range = intensity === 'high' ? 160 : intensity === 'low' ? 30 : 80
|
||
// 1) 鼠标移动到视口中央
|
||
await page.mouse.move(cx + (Math.random() - 0.5) * range, cy + (Math.random() - 0.5) * range, { steps: 4 })
|
||
await page.waitForTimeout(jitter(40, 120))
|
||
// 2) 随机小幅度移动 + wheel(35% 概率)
|
||
await page.mouse.move(cx + (Math.random() - 0.5) * range, cy + (Math.random() - 0.5) * range, { steps: 3 })
|
||
await page.waitForTimeout(jitter(30, 90))
|
||
if (Math.random() < 0.35) {
|
||
const dy = jitter(-25, 25)
|
||
await page.mouse.wheel(0, dy)
|
||
await page.waitForTimeout(jitter(80, 200))
|
||
// 50% 概率往回滚一点
|
||
if (Math.random() < 0.5) {
|
||
await page.mouse.wheel(0, -dy)
|
||
await page.waitForTimeout(jitter(60, 140))
|
||
}
|
||
}
|
||
// 3) 最终回到视口中央
|
||
await page.mouse.move(cx, cy, { steps: 3 })
|
||
await page.waitForTimeout(jitter(40, 100))
|
||
}
|
||
|
||
/**
|
||
* 按键(带按下/抬起 jitter)
|
||
*/
|
||
export async function humanPress(page, key, opts = {}) {
|
||
const { holdMs = [25, 80] } = opts
|
||
await page.keyboard.down(key)
|
||
await page.waitForTimeout(jitter(holdMs[0], holdMs[1]))
|
||
await page.keyboard.up(key)
|
||
await page.waitForTimeout(jitter(50, 160))
|
||
}
|
||
|
||
/**
|
||
* 拖拽:从源元素拖到目标元素(按贝塞尔曲线 + 缓慢减速)
|
||
*
|
||
* 用于拖拽排序、文件拖入等场景
|
||
*/
|
||
export async function humanDragTo(page, source, target, opts = {}) {
|
||
const { settleMs = [120, 280] } = opts
|
||
const srcBox = await source.boundingBox()
|
||
const tgtBox = await target.boundingBox()
|
||
if (!srcBox || !tgtBox) throw new Error('拖拽源/目标不可见')
|
||
const src = { x: srcBox.x + srcBox.width / 2, y: srcBox.y + srcBox.height / 2 }
|
||
const tgt = { x: tgtBox.x + tgtBox.width / 2, y: tgtBox.y + tgtBox.height / 2 }
|
||
// 1) 移到源
|
||
await moveTo(page, src)
|
||
await page.waitForTimeout(jitter(80, 200))
|
||
// 2) mousedown
|
||
await page.mouse.down()
|
||
await page.waitForTimeout(jitter(60, 140))
|
||
// 3) 拖动路径(沿贝塞尔曲线,每段小停顿)
|
||
const path = bezierPath(src, tgt, 16)
|
||
for (let i = 1; i < path.length; i++) {
|
||
await page.mouse.move(path[i].x, path[i].y, { steps: 1 })
|
||
await page.waitForTimeout(jitter(8, 18))
|
||
}
|
||
// 4) 悬停目标上方一小会儿
|
||
await page.waitForTimeout(jitter(settleMs[0], settleMs[1]))
|
||
// 5) mouseup
|
||
await page.mouse.up()
|
||
await page.waitForTimeout(jitter(80, 200))
|
||
} |