d02d5ea11d
Root causes found in release web captures (1280x800 and 560x900): - Follow mode undid small wheel/trackpad ticks at the end: each tick is its own scroll, re-judged as following within 32 px and jumped back. A user scroll toward older rows now releases the end however small; the jump control appears once streamed content moves the end beyond the threshold. - The end-of-scroll jump ran inside the position's activity switch, so a touch or scrollbar drag released near the end recursed without end (stack overflow during the gesture). The jump now runs after the notification. - Horizontally scrolling code blocks and tables claimed diagonal trackpad gestures whole. HorizontalScrollView gives mostly vertical gestures to the enclosing list; sideways and Shift + wheel still scroll the block. - The wheel over the floating chat bar, composer and jump control moved nothing. VerticalWheelForwarder scrolls the transcript from them unless something under the pointer claims the event. Adds widget tests per content kind and scroll mode, follow/touch/Load older tests, a capture-kit check (tool/capture/examples/chat-wheel-scroll.json) with eval/expect steps, wheel dx/repeat, --params and eval: targets, a transcript scroll probe and fixture options, and fixes the kit ignoring a script's size. Notes in notes/chat-wheel-scroll.md.
165 lines
7.9 KiB
JavaScript
165 lines
7.9 KiB
JavaScript
#!/usr/bin/env node
|
|
// Generic browser capture kit for the Flutter app. See README.md beside this file.
|
|
//
|
|
// node tool/capture/capture.mjs --build build/probe-app --out /tmp/shots \
|
|
// open=/runs/7 shot=summary click=Artifacts shot=artifacts
|
|
//
|
|
// Serves a build (or takes --url), launches private headless Chrome (or
|
|
// attaches with --cdp), runs the steps, writes NN-<name>.png files plus
|
|
// report.json into --out, and always removes Chrome, its profile and the server.
|
|
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
import { join, resolve } from 'node:path';
|
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
import { openBrowser } from './browser.mjs';
|
|
import { serveBuild } from './serve.mjs';
|
|
import { contactSheet, pickFrames } from './sheet.mjs';
|
|
import { describe, flutterPage, parseTarget } from './page.mjs';
|
|
|
|
const USAGE = `usage: capture.mjs (--build <dir> | --url <url>) --out <dir> [options] [steps...]
|
|
options: --script <steps.json> --size WxH (880x1000) --platform material|cupertino
|
|
--theme light|dark --q <search> --wait <ms after actions, 1000>
|
|
--film-ms <ms, 900> --cdp http://127.0.0.1:<port> --probe true|false
|
|
--params <query, e.g. chats=2&rich=1, added to every open>
|
|
steps: open=<route>[@WxH] wait=<ms> shot=<name> click=<target> waitfor=<target>
|
|
type=<text> press=<Enter|Escape|Tab|...> scroll=<dy> size=WxH
|
|
film=<target> labels=<name> eval=<js> expect=<js>
|
|
target: <label> | label:<label> | key:<semantics identifier> | x,y`;
|
|
|
|
const opts = { wait: '1000', 'film-ms': '900' };
|
|
const cliSteps = [];
|
|
const argv = process.argv.slice(2);
|
|
for (let i = 0; i < argv.length; i++) {
|
|
if (argv[i] === '-h' || argv[i] === '--help') { console.log(USAGE); process.exit(0); }
|
|
if (argv[i].startsWith('--')) opts[argv[i].slice(2)] = argv[++i];
|
|
else {
|
|
const at = argv[i].indexOf('=');
|
|
if (at < 1) { console.error(`bad step ${JSON.stringify(argv[i])}\n${USAGE}`); process.exit(2); }
|
|
cliSteps.push({ [argv[i].slice(0, at)]: argv[i].slice(at + 1) });
|
|
}
|
|
}
|
|
if (!opts.out || (!opts.build && !opts.url)) { console.error(USAGE); process.exit(2); }
|
|
|
|
let steps = cliSteps;
|
|
if (opts.script) {
|
|
const script = JSON.parse(await readFile(opts.script, 'utf8'));
|
|
const list = Array.isArray(script) ? script : script.steps;
|
|
if (!Array.isArray(script)) for (const k of ['size', 'platform', 'theme', 'q', 'params']) if (script[k] && !opts[k]) opts[k] = script[k];
|
|
steps = [...list, ...cliSteps];
|
|
}
|
|
// The default size applies only when neither the command line nor the script sets one.
|
|
opts.size ??= '880x1000';
|
|
const out = resolve(opts.out);
|
|
await mkdir(out, { recursive: true });
|
|
const probe = opts.probe ? opts.probe === 'true' : Boolean(opts.build);
|
|
const settle = Number(opts.wait);
|
|
const size = (s) => { const [w, h] = String(s).split('x').map(Number); if (!w || !h) throw new Error(`bad size ${s}`); return { width: w, height: h }; };
|
|
const log = (line) => console.log(line);
|
|
|
|
let server, browser;
|
|
let cleaning;
|
|
const cleanup = () => cleaning ??= (async () => {
|
|
await browser?.close().catch((e) => log(`chrome cleanup: ${e.message}`));
|
|
await server?.close().catch(() => {});
|
|
})();
|
|
for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => cleanup().then(() => process.exit(130)));
|
|
|
|
const report = { started: new Date().toISOString(), steps: [], files: [], errors: [] };
|
|
let n = 0;
|
|
const file = (name, ext = 'png') => join(out, `${String(++n).padStart(2, '0')}-${name.replace(/[^\w.-]+/g, '_')}.${ext}`);
|
|
const save = async (path, data) => { await writeFile(path, data); report.files.push(path); log(` wrote ${path}`); return path; };
|
|
|
|
function routeUrl(step, base) {
|
|
const route = step.open;
|
|
const params = { platform: step.platform ?? opts.platform, theme: step.theme ?? opts.theme, q: step.q ?? opts.q };
|
|
let url;
|
|
if (/^https?:/.test(route)) url = new URL(route);
|
|
else if (probe) { url = new URL('/', base); url.searchParams.set('route', route); }
|
|
else url = new URL(route, base);
|
|
for (const [k, v] of Object.entries(params)) if (v) url.searchParams.set(k, v);
|
|
for (const [k, v] of new URLSearchParams(step.params ?? opts.params ?? '')) url.searchParams.set(k, v);
|
|
return url.href;
|
|
}
|
|
|
|
let page;
|
|
let exitCode = 0;
|
|
try {
|
|
const base = opts.build ? (server = await serveBuild(opts.build)).origin : opts.url;
|
|
browser = await openBrowser({ cdp: opts.cdp });
|
|
page = flutterPage(browser, log);
|
|
for (const step of steps) {
|
|
const [verb] = Object.keys(step);
|
|
const value = step[verb];
|
|
log(`${verb} ${typeof value === 'object' ? JSON.stringify(value) : value}`);
|
|
report.steps.push(step);
|
|
switch (verb) {
|
|
case 'open': {
|
|
const [route, at] = String(value).split('@');
|
|
const { width, height } = size(step.size ?? at ?? opts.size);
|
|
const url = routeUrl({ ...step, open: route }, base);
|
|
log(` ${url} at ${width}x${height}`);
|
|
await page.open(url, { width, height, theme: step.theme ?? opts.theme });
|
|
break;
|
|
}
|
|
case 'size': await page.resize(size(value).width, size(value).height); await delay(settle); break;
|
|
case 'wait': await delay(Number(value)); break;
|
|
case 'shot': await save(file(value || 'shot'), await page.shot()); break;
|
|
case 'click': {
|
|
const hit = await page.click(value, step.timeout);
|
|
log(` clicked ${describe(parseTarget(value))} at ${Math.round(hit.x)},${Math.round(hit.y)} ${hit.text ? JSON.stringify(hit.text) : ''}`);
|
|
await delay(step.wait ?? settle);
|
|
break;
|
|
}
|
|
case 'waitfor': await page.waitFor(value, step.timeout ?? 10000); break;
|
|
case 'type': await page.type(String(value)); await delay(step.wait ?? settle); break;
|
|
case 'press': await page.press(String(value)); await delay(step.wait ?? settle); break;
|
|
case 'scroll': {
|
|
const at = await page.scroll(Number(value), step.at, { dx: Number(step.dx ?? 0), repeat: Number(step.repeat ?? 1), gap: Number(step.gap ?? 16) });
|
|
log(` wheel at ${Math.round(at.x)},${Math.round(at.y)}`);
|
|
await delay(step.wait ?? settle);
|
|
break;
|
|
}
|
|
case 'eval': {
|
|
const result = await page.evaluate(String(value));
|
|
log(` = ${JSON.stringify(result)}`);
|
|
(report.evals ??= []).push({ expression: value, result });
|
|
break;
|
|
}
|
|
case 'expect': {
|
|
const result = await page.evaluate(String(value));
|
|
(report.checks ??= []).push({ name: step.name ?? value, expression: value, pass: Boolean(result) });
|
|
if (!result) throw new Error(`expectation failed: ${step.name ?? value}`);
|
|
log(` ok ${step.name ?? ''}`);
|
|
break;
|
|
}
|
|
case 'labels': await save(file(value || 'labels', 'json'), JSON.stringify(await page.labels(), null, 2)); break;
|
|
case 'film': {
|
|
const name = step.name ?? `film-${describe(parseTarget(value)).replace(/\W+/g, '-').replace(/^-|-$/g, '')}`;
|
|
const frames = await page.film(value, Number(step.ms ?? opts['film-ms']));
|
|
const dir = join(out, name.replace(/[^\w.-]+/g, '_'));
|
|
await mkdir(dir, { recursive: true });
|
|
for (const [i, f] of frames.entries()) {
|
|
await writeFile(join(dir, `f${String(i).padStart(3, '0')}_${f.ms}ms.png`), Buffer.from(f.data, 'base64'));
|
|
}
|
|
log(` ${frames.length} frames in ${dir}`);
|
|
if (frames.length) {
|
|
const picks = pickFrames(frames, Number(step.frames ?? 6)).map((f) => ({ data: f.data, label: `${f.ms} ms` }));
|
|
await save(file(`${name}-sheet`), await contactSheet(browser, picks));
|
|
}
|
|
break;
|
|
}
|
|
default: throw new Error(`unknown step ${verb}\n${USAGE}`);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
exitCode = 1;
|
|
log(`FAILED: ${error.message}`);
|
|
report.failure = error.message;
|
|
if (page) await save(file('FAILED'), await page.shot()).catch(() => {});
|
|
} finally {
|
|
report.errors = page?.errors ?? [];
|
|
if (report.errors.length) log(`page errors:\n ${report.errors.join('\n ')}`);
|
|
await writeFile(join(out, 'report.json'), JSON.stringify(report, null, 2));
|
|
await cleanup();
|
|
}
|
|
process.exit(exitCode);
|