@open-take/runtime 0.3.1 → 0.4.1

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.
@@ -0,0 +1,3 @@
1
+ import { captureTakeCDP } from "./cdp-capture-Dhp54F9S.js";
2
+
3
+ export { captureTakeCDP };
@@ -437,13 +437,14 @@ function evalValue(raw) {
437
437
  return deepParse(top);
438
438
  }
439
439
  const NAME_JS = "function names(e){var raw=[e.getAttribute('aria-label'),e.textContent,e.getAttribute('title'),e.getAttribute('alt'),e.getAttribute('placeholder')];var out=[];for(var i=0;i<raw.length;i++){var s=(raw[i]||'').replace(/\\s+/g,' ').trim();if(s&&out.indexOf(s)===-1)out.push(s);}return out;}function dispName(e){var a=[e.getAttribute('aria-label'),e.textContent,e.getAttribute('title'),e.getAttribute('alt')];for(var i=0;i<a.length;i++){var s=(a[i]||'').replace(/\\s+/g,' ').trim();if(s)return s;}return '';}function pick(els,t){var i,n,j;for(i=0;i<els.length;i++){if(names(els[i]).indexOf(t)!==-1)return els[i];}for(i=0;i<els.length;i++){n=names(els[i]);for(j=0;j<n.length;j++){if(n[j].indexOf(t)!==-1)return els[i];}}return null;}";
440
+ const CLICK_TAIL_JS = "if(m.tagName==='SELECT')return 'SELECTINERT';var r=m.getBoundingClientRect();if(r.top<0||r.bottom>window.innerHeight){m.scrollIntoView({block:'center',behavior:'instant'});r=m.getBoundingClientRect();}var b={x:Math.round(r.x),y:Math.round(r.y),w:Math.round(r.width),h:Math.round(r.height)};var cx=Math.round(r.x+r.width/2),cy=Math.round(r.y+r.height/2);var hit=(r.width>0&&r.height>0&&document.elementFromPoint)?document.elementFromPoint(cx,cy):null;if(hit&&(hit===m||(m.contains&&m.contains(hit)))){b.cx=cx;b.cy=cy;return JSON.stringify(b);}m.click();return JSON.stringify(b);";
440
441
  function clickByTextJs(text) {
441
442
  const t = JSON.stringify(text);
442
- return `(function(){var t=${t};var els=Array.prototype.slice.call(document.querySelectorAll('button,a,[role=button],[role=link],[role=menuitem],input[type=submit],input[type=button]'));` + NAME_JS + "var m=pick(els,t);if(!m)return 'NOTFOUND';var r=m.getBoundingClientRect();if(r.top<0||r.bottom>window.innerHeight){m.scrollIntoView({block:'center'});r=m.getBoundingClientRect();}var b={x:Math.round(r.x),y:Math.round(r.y),w:Math.round(r.width),h:Math.round(r.height)};m.click();return JSON.stringify(b);})()";
443
+ return `(function(){var t=${t};var els=Array.prototype.slice.call(document.querySelectorAll('button,a,[role=button],[role=link],[role=menuitem],input[type=submit],input[type=button]'));` + NAME_JS + "var m=pick(els,t);if(!m)return 'NOTFOUND';" + CLICK_TAIL_JS + `})()`;
443
444
  }
444
445
  function clickBySelectorJs(selector) {
445
446
  const s = JSON.stringify(selector);
446
- return `(function(){var m=document.querySelector(${s});if(!m)return 'NOTFOUND';var r=m.getBoundingClientRect();if(r.top<0||r.bottom>window.innerHeight){m.scrollIntoView({block:'center'});r=m.getBoundingClientRect();}var b={x:Math.round(r.x),y:Math.round(r.y),w:Math.round(r.width),h:Math.round(r.height)};m.click();return JSON.stringify(b);})()`;
447
+ return `(function(){var m=document.querySelector(${s});if(!m)return 'NOTFOUND';` + CLICK_TAIL_JS + `})()`;
447
448
  }
448
449
  function focusFieldByTextJs(text) {
449
450
  const t = JSON.stringify(text);
@@ -547,7 +548,7 @@ async function ffprobe(path) {
547
548
  }
548
549
  /** Drive `plan` against the live app over CDP, return the ground-truth log. */
549
550
  async function captureTake(plan, opts) {
550
- const { captureTakeCDP: captureTakeCDP$1 } = await import("./cdp-capture-sAwkZN85.js");
551
+ const { captureTakeCDP: captureTakeCDP$1 } = await import("./cdp-capture-BKGdOShB.js");
551
552
  return captureTakeCDP$1(plan, {
552
553
  ...opts,
553
554
  fps: opts.fps ?? 60
@@ -574,7 +575,8 @@ async function inspectPage(url, opts = {}) {
574
575
  await cdp.send("Page.enable");
575
576
  await cdp.send("Runtime.enable");
576
577
  const inner = await fitViewport(cdp, browser.targetId, vw, vh);
577
- await cdp.send("Page.navigate", { url });
578
+ const nav = await cdp.send("Page.navigate", { url });
579
+ if (nav.errorText) throw new Error(`inspectPage: ${url} did not answer (${nav.errorText}) — is the app running?`);
578
580
  await sleep$2(opts.warmupMs ?? 1500);
579
581
  const evalRaw = async (expr) => {
580
582
  const r = await cdp.send("Runtime.evaluate", {
@@ -604,6 +606,12 @@ async function inspectPage(url, opts = {}) {
604
606
  await browser.close();
605
607
  }
606
608
  }
609
+ function selectOptionJs(target, value) {
610
+ const sel = JSON.stringify(target.selector ?? "");
611
+ const name = JSON.stringify(target.text ?? "");
612
+ const v = JSON.stringify(value);
613
+ return `(function(){var s=${sel},n=${name},want=${v};var m=null;if(s){m=document.querySelector(s);}else if(n){var els=Array.prototype.slice.call(document.querySelectorAll('select'));m=els.find(function(e){var a=(e.getAttribute('aria-label')||'').trim();if(a===n)return true;var id=e.id;if(id){var l=document.querySelector('label[for="'+id+'"]');if(l&&(l.textContent||'').trim().indexOf(n)>=0)return true;}var p=e.closest('label');if(p&&(p.textContent||'').trim().indexOf(n)>=0)return true;return false;})||null;}if(!m)return 'NOTFOUND';if(m.tagName!=='SELECT')return 'NOTASELECT';var opts=Array.prototype.slice.call(m.options);var norm=function(x){return (x||'').replace(/\\s+/g,'').trim();};var o=opts.find(function(x){return x.value===want;})||opts.find(function(x){return norm(x.textContent)===norm(want);})||opts.find(function(x){return norm(x.textContent).indexOf(norm(want))>=0;});if(!o)return 'NOOPTION';var r=m.getBoundingClientRect();if(r.top<0||r.bottom>window.innerHeight){m.scrollIntoView({block:'center'});r=m.getBoundingClientRect();}m.focus();m.value=o.value;m.dispatchEvent(new Event('input',{bubbles:true}));m.dispatchEvent(new Event('change',{bubbles:true}));var b={x:Math.round(r.x),y:Math.round(r.y),w:Math.round(r.width),h:Math.round(r.height)};return JSON.stringify(b);})()`;
614
+ }
607
615
 
608
616
  //#endregion
609
617
  //#region src/nav.ts
@@ -1156,7 +1164,8 @@ async function captureTakeCDP(plan, opts) {
1156
1164
  const inner = await fitViewport(cdp, browser.targetId, vw, vh);
1157
1165
  await installActivityProbe(cdp);
1158
1166
  if (!originAllowed(plan.url)) throw new Error(`captureTakeCDP: plan.url ${plan.url} is outside OPEN_TAKE_ALLOWED_ORIGINS — an unattended run only films the app it was pointed at`);
1159
- await navigate(cdp, plan.url);
1167
+ const navError = await navigate(cdp, plan.url);
1168
+ if (navError) throw new Error(`captureTakeCDP: ${plan.url} did not answer (${navError}) — is the app running? Start it (or fix the URL) and re-make; nothing was recorded.`);
1160
1169
  await installActivityProbe(cdp);
1161
1170
  await awaitFonts(cdp);
1162
1171
  const screencast = new Screencast(cdp, frameDir);
@@ -1264,7 +1273,13 @@ async function captureTakeCDP(plan, opts) {
1264
1273
  skip("navigate", dest, "destination is outside OPEN_TAKE_ALLOWED_ORIGINS");
1265
1274
  continue;
1266
1275
  }
1267
- await navigate(cdp, dest);
1276
+ const midNavError = await navigate(cdp, dest);
1277
+ if (midNavError) {
1278
+ skip("navigate", dest, `destination did not answer (${midNavError})`);
1279
+ await navigate(cdp, current);
1280
+ await hold(1200);
1281
+ continue;
1282
+ }
1268
1283
  await awaitFonts(cdp);
1269
1284
  await hold(1200);
1270
1285
  continue;
@@ -1300,6 +1315,7 @@ async function captureTakeCDP(plan, opts) {
1300
1315
  tMs: tMs$1,
1301
1316
  sel: label$1,
1302
1317
  note: step.note,
1318
+ ...step.caption ? { caption: step.caption } : {},
1303
1319
  text: step.value,
1304
1320
  durationMs: Date.now() - tType,
1305
1321
  ...step.zoom ? { zoom: step.zoom } : {}
@@ -1360,6 +1376,7 @@ async function captureTakeCDP(plan, opts) {
1360
1376
  tMs: tMs$1,
1361
1377
  sel: label$1,
1362
1378
  note: step.note,
1379
+ ...step.caption ? { caption: step.caption } : {},
1363
1380
  durationMs: drawnMs,
1364
1381
  ease: dragEase,
1365
1382
  ...step.zoom ? { zoom: step.zoom } : {}
@@ -1467,6 +1484,7 @@ async function captureTakeCDP(plan, opts) {
1467
1484
  tMs: tMs$1,
1468
1485
  sel: label$1,
1469
1486
  note: step.note,
1487
+ ...step.caption ? { caption: step.caption } : {},
1470
1488
  durationMs: carriedMs,
1471
1489
  ease: dragEase,
1472
1490
  files,
@@ -1507,7 +1525,8 @@ async function captureTakeCDP(plan, opts) {
1507
1525
  tMs: tMs$1,
1508
1526
  dy,
1509
1527
  durationMs: Date.now() - tScroll,
1510
- note: step.note
1528
+ note: step.note,
1529
+ ...step.caption ? { caption: step.caption } : {}
1511
1530
  });
1512
1531
  await hold(800);
1513
1532
  continue;
@@ -1531,6 +1550,7 @@ async function captureTakeCDP(plan, opts) {
1531
1550
  tMs: tMs$1,
1532
1551
  sel: label$1,
1533
1552
  note: step.note,
1553
+ ...step.caption ? { caption: step.caption } : {},
1534
1554
  durationMs: dwell,
1535
1555
  ...step.zoom ? { zoom: step.zoom } : {}
1536
1556
  });
@@ -1538,6 +1558,68 @@ async function captureTakeCDP(plan, opts) {
1538
1558
  await hold(900);
1539
1559
  continue;
1540
1560
  }
1561
+ if (step.action === "select") {
1562
+ const label$1 = step.text ?? step.selector;
1563
+ const tMs$1 = Date.now() - t0;
1564
+ const r = await cdp.send("Runtime.evaluate", {
1565
+ expression: selectOptionJs({
1566
+ selector: step.selector,
1567
+ text: step.text
1568
+ }, step.value),
1569
+ returnByValue: true,
1570
+ awaitPromise: true
1571
+ });
1572
+ const raw = r.result?.value;
1573
+ const str = typeof raw === "string" ? raw : raw == null ? "NOTFOUND" : JSON.stringify(raw);
1574
+ if (str === "NOTFOUND" || str === "NOTASELECT" || str === "NOOPTION") {
1575
+ skip("select", label$1, str === "NOTASELECT" ? "target is not a <select> (use click for custom dropdowns)" : str === "NOOPTION" ? `no option matching ${JSON.stringify(step.value)}` : "target not found");
1576
+ await hold(600);
1577
+ continue;
1578
+ }
1579
+ const box$1 = findBox(evalValue(str));
1580
+ if (!box$1) {
1581
+ skip("select", label$1, "target not found");
1582
+ await hold(600);
1583
+ continue;
1584
+ }
1585
+ events.push({
1586
+ kind: "click",
1587
+ ...center(box$1),
1588
+ box: box$1,
1589
+ tMs: tMs$1,
1590
+ sel: label$1,
1591
+ note: step.note,
1592
+ ...step.caption ? { caption: step.caption } : {},
1593
+ ...step.zoom ? { zoom: step.zoom } : {}
1594
+ });
1595
+ await hold(1300);
1596
+ continue;
1597
+ }
1598
+ if (step.action === "look") {
1599
+ const label$1 = step.text ?? step.selector;
1600
+ const tMs$1 = Date.now() - t0;
1601
+ const box$1 = step.text ? await evalBox(cdp, boxByTextJs(step.text)) : step.selector ? await evalBox(cdp, boxSelectorJs(step.selector)) : null;
1602
+ if (!box$1) {
1603
+ skip("look", label$1, step.text || step.selector ? "target not found" : "no target given");
1604
+ await hold(600);
1605
+ continue;
1606
+ }
1607
+ const dwell = step.durationMs ?? 1800;
1608
+ events.push({
1609
+ kind: "look",
1610
+ ...center(box$1),
1611
+ box: box$1,
1612
+ tMs: tMs$1,
1613
+ sel: label$1,
1614
+ note: step.note,
1615
+ ...step.caption ? { caption: step.caption } : {},
1616
+ durationMs: dwell,
1617
+ zoom: step.zoom ?? "always"
1618
+ });
1619
+ await sleep(dwell);
1620
+ await hold(900);
1621
+ continue;
1622
+ }
1541
1623
  if (step.action === "press") {
1542
1624
  const tMs$1 = Date.now() - t0;
1543
1625
  try {
@@ -1563,6 +1645,7 @@ async function captureTakeCDP(plan, opts) {
1563
1645
  keys: step.keys,
1564
1646
  sel: step.text ?? step.selector,
1565
1647
  note: step.note,
1648
+ ...step.caption ? { caption: step.caption } : {},
1566
1649
  durationMs: dwell,
1567
1650
  ...step.zoom ? { zoom: step.zoom } : {}
1568
1651
  });
@@ -1571,15 +1654,38 @@ async function captureTakeCDP(plan, opts) {
1571
1654
  continue;
1572
1655
  }
1573
1656
  const label = step.text ?? step.selector;
1574
- const tMs = Date.now() - t0;
1575
- const box = step.text ? await evalBox(cdp, clickByTextJs(step.text)) : step.selector ? await evalBox(cdp, clickBySelectorJs(step.selector)) : null;
1657
+ let tMs = Date.now() - t0;
1658
+ const clickRaw = step.text ? await evalString(cdp, clickByTextJs(step.text)) : step.selector ? await evalString(cdp, clickBySelectorJs(step.selector)) : null;
1659
+ if (clickRaw === "SELECTINERT") {
1660
+ skip("click", label, "clicking a native <select> does nothing headless (inert, no popup) — use the `select` action");
1661
+ await hold(600);
1662
+ continue;
1663
+ }
1664
+ const parsed = clickRaw ? evalValue(clickRaw) : null;
1665
+ const box = parsed ? findBox(parsed) : null;
1666
+ const pt = parsed && typeof parsed.cx === "number" && typeof parsed.cy === "number" ? {
1667
+ x: parsed.cx,
1668
+ y: parsed.cy
1669
+ } : null;
1670
+ if (box && pt) {
1671
+ pumpPaused = true;
1672
+ await sleep(160);
1673
+ tMs = Date.now() - t0;
1674
+ mouse(cdp, "mouseMoved", pt.x, pt.y, 0).catch(() => {});
1675
+ mouse(cdp, "mousePressed", pt.x, pt.y, 1).catch(() => {});
1676
+ await sleep(60);
1677
+ mouse(cdp, "mouseReleased", pt.x, pt.y, 0).catch(() => {});
1678
+ await sleep(120);
1679
+ pumpPaused = false;
1680
+ }
1576
1681
  if (box) events.push({
1577
1682
  kind: "click",
1578
- ...center(box),
1683
+ ...pt ?? center(box),
1579
1684
  box,
1580
1685
  tMs,
1581
1686
  sel: label,
1582
1687
  note: step.note,
1688
+ ...step.caption ? { caption: step.caption } : {},
1583
1689
  ...step.zoom ? { zoom: step.zoom } : {}
1584
1690
  });
1585
1691
  else skip("click", label, "target not found");
@@ -1652,15 +1758,21 @@ function credentialFieldProbeJs() {
1652
1758
  return type === "password" || /passw|secret|api[-_]?key|access[-_]?token/.test(smell) ? "secret" : "ok";
1653
1759
  })()`;
1654
1760
  }
1761
+ /** Returns Chrome's `errorText` (e.g. "net::ERR_CONNECTION_REFUSED") when the
1762
+ * navigation never reached a server — the tab is then showing Chrome's own
1763
+ * error page, which fires load events like any document and would otherwise
1764
+ * get filmed as if it were the app. HTTP error PAGES (404/500) carry no
1765
+ * errorText: the app answered, and what it said is what an honest take shows. */
1655
1766
  async function navigate(cdp, url) {
1656
1767
  const loaded = new Promise((res) => {
1657
1768
  cdp.on("Page.loadEventFired", () => res());
1658
1769
  setTimeout(res, 8e3);
1659
1770
  });
1660
- await cdp.send("Page.navigate", { url });
1771
+ const nav = await cdp.send("Page.navigate", { url });
1661
1772
  await loaded;
1773
+ return nav.errorText || void 0;
1662
1774
  }
1663
1775
 
1664
1776
  //#endregion
1665
1777
  export { captureTake, captureTakeCDP, ensureChrome, inspectPage, resolveChrome, resolveNavigateUrl };
1666
- //# sourceMappingURL=cdp-capture-CWNroQPQ.js.map
1778
+ //# sourceMappingURL=cdp-capture-Dhp54F9S.js.map