File size: 2,291 Bytes
0e0e768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { Page } from '@playwright/test';

let _depth = 0;
export function rbEnter(): void { _depth += 1; }
export function rbBack(): void { _depth = Math.max(0, _depth - 1); }
export function rbResetDepth(): number { _depth = 0; return _depth; }

/** Snapshot visible, meaningful elements as a set of "role|name" strings.
 *  The web analog of RecreationBench rb_showing(root). Bounded + discriminating:
 *  only landmarks, headings, links, buttons, [role], and dialogs. */
export async function rbShowing(page: Page): Promise<Set<string>> {
  const items: string[] = await page.evaluate(() => {
    const SEL = 'h1,h2,h3,h4,h5,h6,a[href],button,[role],nav,main,header,footer,aside,dialog,[aria-modal="true"]';
    const out: string[] = [];
    const seen = new Set<string>();
    const nodes = Array.from(document.querySelectorAll(SEL)).slice(0, 4000);
    for (const el of nodes) {
      const r = (el as HTMLElement).getBoundingClientRect();
      const s = getComputedStyle(el as HTMLElement);
      const visible = r.width > 0 && r.height > 0 &&
        s.visibility !== 'hidden' && s.display !== 'none' &&
        parseFloat(s.opacity || '1') > 0.01;
      if (!visible) continue;
      const role = el.getAttribute('role') || el.tagName.toLowerCase();
      const name = (el.getAttribute('aria-label') || (el as HTMLElement).innerText || el.textContent || '')
        .replace(/\s+/g, ' ').trim().slice(0, 80);
      const key = `${role}|${name}`;
      if (!seen.has(key)) { seen.add(key); out.push(key); }
    }
    return out;
  });
  return new Set(items);
}

/** True-jump verdict: target's discriminating element newly APPEARED and
 *  (if source given) source's element DISAPPEARED. Verbatim port of rb_reach. */
export function rbReach(
  before: Iterable<string>, after: Iterable<string>,
  target: string, source?: string,
): boolean {
  const b = new Set(before), a = new Set(after);
  const t = target.toLowerCase();
  const appeared = [...a].some(k => !b.has(k) && k.toLowerCase().includes(t));
  if (source === undefined) return appeared;
  const sName = source.toLowerCase();
  const srcBefore = [...b].some(k => k.toLowerCase().includes(sName));
  const srcAfter = [...a].some(k => k.toLowerCase().includes(sName));
  return appeared && srcBefore && !srcAfter;
}