Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/cdp/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function look(ctx: BrowserContext, opts: LookOptions = {}): Promise

// a11y (default)
const tree = await ctx.session.send<{ nodes: AXNode[] }>("Accessibility.getFullAXTree", {});
const { refs, refMap, truncated } = compactAxTree(tree.nodes ?? []);
const { refs, refMap, truncated, truncatedCount } = compactAxTree(tree.nodes ?? []);

ctx.lastRefs.clear();
for (const [k, v] of refMap) ctx.lastRefs.set(k, v);
Expand All @@ -74,7 +74,11 @@ export async function look(ctx: BrowserContext, opts: LookOptions = {}): Promise
filtered = refs.filter((r) => r.role.toLowerCase().includes(f) || r.name.toLowerCase().includes(f));
}
const header = `${title}\n${url}\n${filtered.length} of ${refs.length} refs${truncated ? " (TRUNCATED at 200)" : ""}`;
return { snapshot, text: `${header}\n${renderRefs(filtered)}` };
let text = `${header}\n${renderRefs(filtered)}`;
if (truncated && truncatedCount > 0) {
text += `\n… ${truncatedCount} more elements below — scroll or filter`;
}
return { snapshot, text };
}

function delta(snapshot: Snapshot, summary: string): SnapshotDelta {
Expand Down
6 changes: 4 additions & 2 deletions packages/cdp/src/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ const tree: AXNode[] = [
];

describe("compactAxTree", () => {
const { refs, refMap, truncated } = compactAxTree(tree);
const { refs, refMap, truncated, truncatedCount } = compactAxTree(tree);

it("keeps only interactive + named-role nodes that are not ignored and have a backend id", () => {
expect(refs.map((r) => r.role)).toEqual(["button", "textbox", "checkbox", "heading"]);
expect(refs.map((r) => r.name)).toEqual(["Sign in", "Email", "Remember me", "Welcome"]);
expect(truncated).toBe(false);
expect(truncatedCount).toBe(0);
});

it("assigns sequential ref ids from 1", () => {
Expand Down Expand Up @@ -117,8 +118,9 @@ describe("compactAxTree truncation", () => {
backendDOMNodeId: i + 1,
}),
);
const { refs, truncated } = compactAxTree(many);
const { refs, truncated, truncatedCount } = compactAxTree(many);
expect(refs.length).toBe(200);
expect(truncated).toBe(true);
expect(truncatedCount).toBe(50);
});
});
7 changes: 5 additions & 2 deletions packages/cdp/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface CompactionResult {
/** ref id -> resolution info needed by act/fill. */
refMap: Map<number, { backendDOMNodeId: number; role: string; name: string }>;
truncated: boolean;
truncatedCount: number;
}

/**
Expand All @@ -100,6 +101,7 @@ export function compactAxTree(nodes: AXNode[]): CompactionResult {
const refMap = new Map<number, { backendDOMNodeId: number; role: string; name: string }>();
let counter = 0;
let truncated = false;
let truncatedCount = 0;

for (const node of nodes) {
if (node.ignored) continue;
Expand All @@ -115,7 +117,8 @@ export function compactAxTree(nodes: AXNode[]): CompactionResult {

if (refs.length >= MAX_REFS) {
truncated = true;
break;
truncatedCount++;
continue;
}

const ref = ++counter;
Expand All @@ -124,7 +127,7 @@ export function compactAxTree(nodes: AXNode[]): CompactionResult {
refMap.set(ref, { backendDOMNodeId: node.backendDOMNodeId, role, name });
}

return { refs, refMap, truncated };
return { refs, refMap, truncated, truncatedCount };
}

/** Render refs as compact "[ref] role \"name\" (state)" lines. */
Expand Down