lazyscout 0.4.2 → 0.4.4

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.
Files changed (2) hide show
  1. package/dist/index.js +171 -32
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -547,6 +547,37 @@ function collectPageData() {
547
547
  const value = el.getAttribute(name);
548
548
  return value && value.trim() ? value.trim() : void 0;
549
549
  }
550
+ function renderedText(el) {
551
+ let text = "";
552
+ const walk = (node) => {
553
+ if (node.nodeType === Node.TEXT_NODE) {
554
+ text += node.textContent ?? "";
555
+ return;
556
+ }
557
+ if (node.nodeType !== Node.ELEMENT_NODE)
558
+ return;
559
+ const element = node;
560
+ const style = window.getComputedStyle(element);
561
+ if (style.display === "none" || style.visibility === "hidden")
562
+ return;
563
+ if (element instanceof HTMLImageElement) {
564
+ const alt = element.getAttribute("alt");
565
+ if (alt)
566
+ text += ` ${alt} `;
567
+ return;
568
+ }
569
+ const inline = style.display === "inline" || style.display === "contents";
570
+ if (!inline)
571
+ text += " ";
572
+ for (const child of Array.from(element.childNodes))
573
+ walk(child);
574
+ if (!inline)
575
+ text += " ";
576
+ };
577
+ for (const child of Array.from(el.childNodes))
578
+ walk(child);
579
+ return text;
580
+ }
550
581
  function accessibleName(el) {
551
582
  const ariaLabel = cleanText(el.getAttribute("aria-label"));
552
583
  if (ariaLabel)
@@ -581,7 +612,7 @@ function collectPageData() {
581
612
  return cleanText(el.value);
582
613
  return cleanText(el.getAttribute("name"));
583
614
  }
584
- const ownText = cleanText(el.textContent);
615
+ const ownText = cleanText(renderedText(el));
585
616
  if (ownText)
586
617
  return ownText;
587
618
  const image = el.querySelector("img[alt]");
@@ -865,11 +896,12 @@ function collectPageData() {
865
896
  const kind = role === "tab" || dataTab ? "tab" : dataDrawer ? "drawer" : popup === "dialog" || toggle === "modal" || dataModal ? "dialog" : popup === "menu" || popup === "listbox" || toggle === "dropdown" ? "dropdown" : accordion ? "accordion" : element instanceof HTMLSelectElement ? "dropdown" : element.matches("[aria-expanded]") ? "accordion" : "dialog";
866
897
  const controls = element.getAttribute("aria-controls");
867
898
  const targetSelector = element.getAttribute("data-target") || element.getAttribute("data-bs-target") || element.getAttribute("data-modal") || element.getAttribute("data-drawer");
899
+ const clickable = element.tagName === "DETAILS" ? element.querySelector("summary") ?? element : element;
868
900
  return {
869
901
  kind,
870
902
  name: accessibleName(element),
871
903
  role,
872
- cssSelector: cssSelector(element),
904
+ cssSelector: cssSelector(clickable),
873
905
  expanded: expanded === null ? void 0 : expanded === "true",
874
906
  visible: true,
875
907
  controlsSelector: controls ? `#${CSS.escape(controls)}` : targetSelector || void 0,
@@ -1159,20 +1191,53 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1159
1191
  function isSessionEnding(name, text) {
1160
1192
  return isSessionEndingLabel(name, text);
1161
1193
  }
1162
- async function discoverNavigationRegions(page) {
1194
+ async function discoverNavigationRegions(page, currentState) {
1163
1195
  const candidates = [];
1164
1196
  try {
1165
1197
  const navItems = await page.evaluate(() => {
1198
+ const renderedText = (element) => {
1199
+ let text = "";
1200
+ const walk = (node) => {
1201
+ if (node.nodeType === Node.TEXT_NODE) {
1202
+ text += node.textContent ?? "";
1203
+ return;
1204
+ }
1205
+ if (node.nodeType !== Node.ELEMENT_NODE)
1206
+ return;
1207
+ const child = node;
1208
+ const style = window.getComputedStyle(child);
1209
+ if (style.visibility === "hidden")
1210
+ return;
1211
+ if (child instanceof HTMLImageElement) {
1212
+ const alt = child.getAttribute("alt");
1213
+ if (alt)
1214
+ text += ` ${alt} `;
1215
+ return;
1216
+ }
1217
+ const inline = style.display === "inline" || style.display === "contents";
1218
+ if (!inline)
1219
+ text += " ";
1220
+ for (const grandChild of Array.from(child.childNodes))
1221
+ walk(grandChild);
1222
+ if (!inline)
1223
+ text += " ";
1224
+ };
1225
+ for (const child of Array.from(element.childNodes))
1226
+ walk(child);
1227
+ return text.replace(/\s+/g, " ").trim();
1228
+ };
1166
1229
  const results = [];
1167
1230
  const navRegions = document.querySelectorAll('nav a[href], aside a[href], [role="navigation"] a[href], [role="menu"] [role="menuitem"], [role="menubar"] [role="menuitem"]');
1168
1231
  navRegions.forEach((el) => {
1169
1232
  const anchor = el;
1170
- const name = el.getAttribute("aria-label") || el.textContent?.replace(/\s+/g, " ").trim() || "";
1171
- if (!name || name.length > 80)
1233
+ if (el.getClientRects().length === 0)
1234
+ return;
1235
+ const name = el.getAttribute("aria-label")?.trim() || renderedText(el);
1236
+ if (!name)
1172
1237
  return;
1173
- const cssSel = el.id ? `#${CSS.escape(el.id)}` : `${el.tagName.toLowerCase()}[href="${anchor.getAttribute("href") || ""}"]`;
1238
+ const cssSel = el.id ? `#${CSS.escape(el.id)}` : `${el.tagName.toLowerCase()}[href="${(anchor.getAttribute("href") || "").replace(/["\\]/g, "\\$&")}"]`;
1174
1239
  results.push({
1175
- name: name.slice(0, 80),
1240
+ name,
1176
1241
  role: el.getAttribute("role") || "link",
1177
1242
  href: anchor.href || null,
1178
1243
  cssSelector: cssSel
@@ -1184,7 +1249,9 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1184
1249
  if (!item.href)
1185
1250
  continue;
1186
1251
  const action = {
1187
- id: `nav|${item.cssSelector}`,
1252
+ // Scoped to the state, so failing here cannot blacklist the same link
1253
+ // in a state where it is reachable.
1254
+ id: `${currentState.id}|nav|${item.cssSelector}`,
1188
1255
  type: "navigate",
1189
1256
  description: `Navigate to \u201C${item.name}\u201D via sidebar`,
1190
1257
  target: item.name,
@@ -1203,7 +1270,7 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1203
1270
  candidates.push({
1204
1271
  priority: 2,
1205
1272
  action,
1206
- locator: { role: item.role, name: item.name },
1273
+ locator: { role: item.role, name: item.name, cssSelector: item.cssSelector },
1207
1274
  kind: "sidebar-nav",
1208
1275
  restoreStrategy: "goto"
1209
1276
  });
@@ -1228,7 +1295,7 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1228
1295
  safe: false,
1229
1296
  reason: "session-ending action"
1230
1297
  },
1231
- locator: { role: link.role, name: link.accessibleName },
1298
+ locator: { role: link.role, name: link.accessibleName, cssSelector: link.cssSelector },
1232
1299
  kind: "navigation-link",
1233
1300
  restoreStrategy: "goto"
1234
1301
  });
@@ -1247,13 +1314,13 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1247
1314
  safe: !blocked,
1248
1315
  reason: blocked ? "Action matched the Project click filter" : void 0
1249
1316
  },
1250
- locator: { role: link.role, name: link.accessibleName },
1317
+ locator: { role: link.role, name: link.accessibleName, cssSelector: link.cssSelector },
1251
1318
  kind: "navigation-link",
1252
1319
  restoreStrategy: "goto"
1253
1320
  });
1254
1321
  }
1255
1322
  try {
1256
- const navCandidates = await discoverNavigationRegions(page);
1323
+ const navCandidates = await discoverNavigationRegions(page, currentState);
1257
1324
  candidates.push(...navCandidates);
1258
1325
  } catch {
1259
1326
  }
@@ -1271,7 +1338,7 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1271
1338
  safe: !blocked,
1272
1339
  reason: blocked ? "Action matched the Project click filter" : void 0
1273
1340
  },
1274
- locator: { role: interaction.role, name: interaction.name },
1341
+ locator: { role: interaction.role, name: interaction.name, cssSelector: interaction.cssSelector },
1275
1342
  kind: "tab",
1276
1343
  restoreStrategy: "select-tab"
1277
1344
  });
@@ -1326,7 +1393,7 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1326
1393
  safe: !blocked,
1327
1394
  reason: blocked ? "Action matched the Project click filter" : void 0
1328
1395
  },
1329
- locator: { role: interaction.role, name: interaction.name },
1396
+ locator: { role: interaction.role, name: interaction.name, cssSelector: interaction.cssSelector },
1330
1397
  kind: "modal-opener",
1331
1398
  restoreStrategy: "close-dialog"
1332
1399
  });
@@ -1345,7 +1412,7 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1345
1412
  safe: !blocked,
1346
1413
  reason: blocked ? "Action matched the Project click filter" : void 0
1347
1414
  },
1348
- locator: { role: interaction.role, name: interaction.name },
1415
+ locator: { role: interaction.role, name: interaction.name, cssSelector: interaction.cssSelector },
1349
1416
  kind: "dropdown",
1350
1417
  restoreStrategy: "goto"
1351
1418
  });
@@ -1364,7 +1431,7 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1364
1431
  safe: !blocked,
1365
1432
  reason: blocked ? "Action matched the Project click filter" : void 0
1366
1433
  },
1367
- locator: { role: interaction.role, name: interaction.name },
1434
+ locator: { role: interaction.role, name: interaction.name, cssSelector: interaction.cssSelector },
1368
1435
  kind: "accordion",
1369
1436
  restoreStrategy: "goto"
1370
1437
  });
@@ -1439,15 +1506,37 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1439
1506
  candidates.sort((a, b) => a.priority - b.priority);
1440
1507
  return candidates;
1441
1508
  }
1442
- async function restoreState(page, targetUrl, strategy, _entryFlow) {
1509
+ async function replayEntryFlow(page, entryFlow) {
1510
+ const inPageSteps = entryFlow.filter((step) => step.action.selector && step.action.type !== "navigate");
1511
+ if (inPageSteps.length === 0)
1512
+ return;
1513
+ for (const step of inPageSteps) {
1514
+ const selector = step.action.selector;
1515
+ if (!selector)
1516
+ continue;
1517
+ try {
1518
+ const target = page.locator(selector).first();
1519
+ if (await target.count() === 0)
1520
+ continue;
1521
+ if (!await target.isVisible())
1522
+ continue;
1523
+ await target.click({ timeout: legacyOptions.actionTimeoutMs });
1524
+ await page.waitForTimeout(legacyOptions.stateDiscoveryTimeoutMs);
1525
+ } catch {
1526
+ }
1527
+ }
1528
+ }
1529
+ async function restoreState(page, targetUrl, strategy, entryFlow) {
1443
1530
  switch (strategy) {
1444
1531
  case "goto":
1445
1532
  await page.goto(targetUrl, { waitUntil: "domcontentloaded", timeout: legacyOptions.pageTimeoutMs }).catch(() => void 0);
1446
1533
  await page.waitForTimeout(legacyOptions.waitAfterNavigationMs);
1534
+ await replayEntryFlow(page, entryFlow);
1447
1535
  break;
1448
1536
  case "reload":
1449
1537
  await page.reload({ waitUntil: "domcontentloaded", timeout: legacyOptions.pageTimeoutMs }).catch(() => void 0);
1450
1538
  await page.waitForTimeout(legacyOptions.waitAfterNavigationMs);
1539
+ await replayEntryFlow(page, entryFlow);
1451
1540
  break;
1452
1541
  case "close-dialog":
1453
1542
  try {
@@ -1460,7 +1549,7 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1460
1549
  await page.waitForTimeout(legacyOptions.stateDiscoveryTimeoutMs);
1461
1550
  break;
1462
1551
  case "entry-replay":
1463
- for (const step of _entryFlow) {
1552
+ for (const step of entryFlow) {
1464
1553
  try {
1465
1554
  if (step.action.selector) {
1466
1555
  await page.locator(step.action.selector).click({ timeout: legacyOptions.actionTimeoutMs }).catch(() => void 0);
@@ -1484,20 +1573,34 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1484
1573
  }
1485
1574
  }
1486
1575
  async function executeWithRetry(page, candidate, currentUrl, entryFlow, maxRetries) {
1576
+ const locatorsFor = () => {
1577
+ const options = [];
1578
+ const { role, name, testId, cssSelector } = candidate.locator;
1579
+ if (testId)
1580
+ options.push(() => page.getByTestId(testId));
1581
+ if (role && name) {
1582
+ options.push(() => page.getByRole(role, { name, exact: true }).first());
1583
+ options.push(() => page.getByRole(role, { name }).first());
1584
+ }
1585
+ if (cssSelector)
1586
+ options.push(() => page.locator(cssSelector).first());
1587
+ return options;
1588
+ };
1487
1589
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
1488
1590
  try {
1489
- let locator3;
1490
- if (candidate.locator.cssSelector) {
1491
- locator3 = page.locator(candidate.locator.cssSelector).first();
1492
- } else if (candidate.locator.role && candidate.locator.name) {
1493
- locator3 = page.getByRole(candidate.locator.role, {
1494
- name: candidate.locator.name
1495
- });
1496
- } else if (candidate.locator.testId) {
1497
- locator3 = page.getByTestId(candidate.locator.testId);
1498
- } else {
1591
+ const options = locatorsFor();
1592
+ if (options.length === 0)
1499
1593
  return { success: false };
1594
+ let locator3;
1595
+ for (const build2 of options) {
1596
+ const next = build2();
1597
+ if (await next.count() > 0) {
1598
+ locator3 = next;
1599
+ break;
1600
+ }
1500
1601
  }
1602
+ if (!locator3)
1603
+ throw new Error("No locator candidate matched");
1501
1604
  await locator3.click({ timeout: legacyOptions.actionTimeoutMs });
1502
1605
  await page.waitForTimeout(legacyOptions.stateDiscoveryTimeoutMs);
1503
1606
  const newUrl = page.url();
@@ -1727,8 +1830,13 @@ async function exploreWithScope(startUrl, explorationConfig = {}, exploreOptions
1727
1830
  break;
1728
1831
  }
1729
1832
  }
1730
- const raw2 = await page.evaluate(collectPageData);
1731
- const currentModel = mapToPageModel(raw2, { url: item.url, finalUrl: page.url(), depth: item.depth, statusCode: 200 }, legacyOptions.blockedKeywords);
1833
+ const collect = async () => mapToPageModel(await page.evaluate(collectPageData), { url: item.url, finalUrl: page.url(), depth: item.depth, statusCode: 200 }, legacyOptions.blockedKeywords);
1834
+ let currentModel = await collect();
1835
+ const wantedState = item.stateId ? actionGraph.states.find((state) => state.id === item.stateId) : void 0;
1836
+ if (wantedState && currentModel.state && currentModel.state.fingerprint !== wantedState.fingerprint) {
1837
+ await replayEntryFlow(page, item.entryFlow);
1838
+ currentModel = await collect();
1839
+ }
1732
1840
  const currentState = currentModel.state;
1733
1841
  if (!currentState)
1734
1842
  continue;
@@ -2005,6 +2113,37 @@ function recorderInitScript() {
2005
2113
  const wrapping = element.closest("label");
2006
2114
  return clean(wrapping?.textContent) || void 0;
2007
2115
  };
2116
+ const renderedText = (el) => {
2117
+ let text = "";
2118
+ const walk = (node) => {
2119
+ if (node.nodeType === Node.TEXT_NODE) {
2120
+ text += node.textContent ?? "";
2121
+ return;
2122
+ }
2123
+ if (node.nodeType !== Node.ELEMENT_NODE)
2124
+ return;
2125
+ const child = node;
2126
+ const style = window.getComputedStyle(child);
2127
+ if (style.display === "none" || style.visibility === "hidden")
2128
+ return;
2129
+ if (child instanceof HTMLImageElement) {
2130
+ const alt = child.getAttribute("alt");
2131
+ if (alt)
2132
+ text += ` ${alt} `;
2133
+ return;
2134
+ }
2135
+ const inline = style.display === "inline" || style.display === "contents";
2136
+ if (!inline)
2137
+ text += " ";
2138
+ for (const grandChild of Array.from(child.childNodes))
2139
+ walk(grandChild);
2140
+ if (!inline)
2141
+ text += " ";
2142
+ };
2143
+ for (const child of Array.from(el.childNodes))
2144
+ walk(child);
2145
+ return text;
2146
+ };
2008
2147
  const accessibleName = (element) => {
2009
2148
  const aria = clean(element.getAttribute("aria-label"));
2010
2149
  if (aria)
@@ -2034,7 +2173,7 @@ function recorderInitScript() {
2034
2173
  }
2035
2174
  return void 0;
2036
2175
  }
2037
- return clean(element.textContent) || void 0;
2176
+ return clean(renderedText(element)) || void 0;
2038
2177
  };
2039
2178
  const TEST_ID_ATTRIBUTES = ["data-testid", "data-test-id", "data-test", "data-qa", "data-cy"];
2040
2179
  const testIdOf = (element) => {
@@ -5519,7 +5658,7 @@ async function openInBrowser(url) {
5519
5658
  }
5520
5659
 
5521
5660
  // src/version.ts
5522
- var VERSION = true ? "0.4.2" : "0.0.0-dev";
5661
+ var VERSION = true ? "0.4.4" : "0.0.0-dev";
5523
5662
 
5524
5663
  // src/commands/serve.ts
5525
5664
  var DEFAULT_PORT = 4321;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lazyscout",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Local-first QA assistant for website exploration, test-case drafting, and Playwright/Cypress automation code generation.",
5
5
  "type": "module",
6
6
  "license": "MIT",