Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: complete the Accessibility domain

a.dmitryuk7bb85ffd8298
diff --git a/src/NodeRegistry.zig b/src/NodeRegistry.zig
index a9347e36b..8b555a0d6 100644
--- a/src/NodeRegistry.zig
+++ b/src/NodeRegistry.zig
@@ -40,6 +40,10 @@ allocator: Allocator,
node_pool: std.heap.MemoryPool(Node),
lookup_by_id: std.AutoHashMapUnmanaged(Id, *Node),
lookup_by_node: std.HashMapUnmanaged(*DOMNode, *Node, NodeContext, std.hash_map.default_max_load_percentage),
+// Ids of the AX tree's synthetic ListMarker nodes, keyed by their <li>. They
+// come from node_id, so they never collide with a node's id.
+list_markers: std.HashMapUnmanaged(*DOMNode, Id, NodeContext, std.hash_map.default_max_load_percentage),
+list_marker_ids: std.AutoHashMapUnmanaged(Id, *DOMNode),
pub fn init(allocator: Allocator) NodeRegistry {
return .{
@@ -47,6 +51,8 @@ pub fn init(allocator: Allocator) NodeRegistry {
.node_pool = .empty,
.lookup_by_id = .{},
.lookup_by_node = .{},
+ .list_markers = .{},
+ .list_marker_ids = .{},
.allocator = allocator,
};
}
@@ -55,12 +61,16 @@ pub fn deinit(self: *NodeRegistry) void {
const allocator = self.allocator;
self.lookup_by_id.deinit(allocator);
self.lookup_by_node.deinit(allocator);
+ self.list_markers.deinit(allocator);
+ self.list_marker_ids.deinit(allocator);
self.node_pool.deinit(allocator);
}
pub fn reset(self: *NodeRegistry) void {
self.lookup_by_id.clearRetainingCapacity();
self.lookup_by_node.clearRetainingCapacity();
+ self.list_markers.clearRetainingCapacity();
+ self.list_marker_ids.clearRetainingCapacity();
_ = self.node_pool.reset(self.allocator, .{ .retain_with_limit = 1024 });
}
@@ -69,6 +79,8 @@ pub fn reset(self: *NodeRegistry) void {
/// each node's document.
pub fn resetFrame(self: *NodeRegistry, arena: Allocator, frame: *Frame) void {
const page = frame.page;
+ self.resetFrameListMarkers(arena, frame);
+
var doomed: std.ArrayListUnmanaged(*Node) = .empty;
var it = self.lookup_by_id.valueIterator();
while (it.next()) |node_ptr| {
@@ -86,6 +98,48 @@ pub fn resetFrame(self: *NodeRegistry, arena: Allocator, frame: *Frame) void {
}
}
+// The markers of the evicted <li>s go too: a later <li> at the same address
+// mustn't inherit one. Dropping every marker is harmless, so that's the
+// fallback when there's no memory to pick them out.
+fn resetFrameListMarkers(self: *NodeRegistry, arena: Allocator, frame: *Frame) void {
+ const page = frame.page;
+ var doomed: std.ArrayListUnmanaged(*DOMNode) = .empty;
+ var it = self.list_markers.keyIterator();
+ while (it.next()) |li| {
+ if (li.*.getDocument(frame)._page == page) {
+ doomed.append(arena, li.*) catch {
+ self.list_markers.clearRetainingCapacity();
+ self.list_marker_ids.clearRetainingCapacity();
+ return;
+ };
+ }
+ }
+ for (doomed.items) |li| {
+ const marker_id = self.list_markers.fetchRemove(li).?.value;
+ _ = self.list_marker_ids.remove(marker_id);
+ }
+}
+
+/// The id of the ListMarker AX node of `li`, the same on every call.
+pub fn listMarkerId(self: *NodeRegistry, li: *DOMNode) !Id {
+ const gop = try self.list_markers.getOrPut(self.allocator, li);
+ if (gop.found_existing) {
+ return gop.value_ptr.*;
+ }
+ errdefer _ = self.list_markers.remove(li);
+
+ const id = self.node_id;
+ try self.list_marker_ids.putNoClobber(self.allocator, id, li);
+ self.node_id = id + 1;
+ gop.value_ptr.* = id;
+ return id;
+}
+
+/// The <li> of the ListMarker AX node `id`, if `id` is a marker's.
+pub fn listMarkerOwner(self: *const NodeRegistry, id: Id) ?*DOMNode {
+ return self.list_marker_ids.get(id);
+}
+
pub fn register(self: *NodeRegistry, dom_node: *DOMNode) !*Node {
const node_lookup_gop = try self.lookup_by_node.getOrPut(self.allocator, dom_node);
if (node_lookup_gop.found_existing) {
@@ -207,3 +261,26 @@ test "NodeRegistry: reset never reuses an id" {
try testing.expectEqual(null, registry.lookup_by_id.get(first_id));
try testing.expect((try registry.register(dom_node)).id != first_id);
}
+
+test "NodeRegistry: listMarkerId" {
+ var registry = NodeRegistry.init(testing.allocator);
+ defer registry.deinit();
+
+ var page = try testing.pageTest("cdp/ax_relatives.html", .{});
+ defer page.close();
+
+ const frame = page.frame().?;
+ const li = (try frame.window._document.querySelector(.wrap("#item"), frame)).?.asNode();
+
+ const marker_id = try registry.listMarkerId(li);
+ try testing.expectEqual(marker_id, try registry.listMarkerId(li));
+ try testing.expectEqual(li, registry.listMarkerOwner(marker_id));
+
+ // Marker and node ids share one counter.
+ const node = try registry.register(li);
+ try testing.expect(node.id != marker_id);
+ try testing.expectEqual(null, registry.listMarkerOwner(node.id));
+
+ registry.reset();
+ try testing.expectEqual(null, registry.listMarkerOwner(marker_id));
+}
diff --git a/src/browser/tests/cdp/ax_relatives.html b/src/browser/tests/cdp/ax_relatives.html
new file mode 100644
index 000000000..a9037c980
--- /dev/null
+++ b/src/browser/tests/cdp/ax_relatives.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>AX Relatives</title>
+</head>
+<body>
+ <ol><li id="item">First</li></ol>
+ <svg id="pic"><circle r="1"></circle></svg>
+ <div style="display: none"><p id="in-gone">gone</p></div>
+ <iframe id="frame" srcdoc="<button id=inner>Inner</button>">fallback</iframe>
+</body>
+</html>
diff --git a/src/server/cdp/AXNode.zig b/src/server/cdp/AXNode.zig
index c2b88357f..04d62de25 100644
--- a/src/server/cdp/AXNode.zig
+++ b/src/server/cdp/AXNode.zig
@@ -52,6 +52,26 @@ pub const Writer = struct {
// match the filter, in a flat shape.
filter: ?Filter = null,
depth: i32 = 0,
+ scope: Scope = .subtree,
+
+ // Which nodes around `root` to emit when there's no filter.
+ pub const Scope = enum {
+ // `root` and its descendants, down to `depth` (getFullAXTree).
+ subtree,
+ // `root` alone.
+ node,
+ // `root` alone, as a bare AXNode rather than a list (getRootAXNode).
+ bare_node,
+ // `root`, its ancestors, its siblings and its children
+ // (getPartialAXTree with fetchRelatives).
+ relatives,
+ // `root`, then each ancestor up to its frame's document
+ // (getAXNodeAndAncestors). Like getFullAXTree, this stays in the
+ // frame: clients stitch frames together themselves.
+ ancestors,
+ // The children of `root` (getChildAXNodes).
+ children,
+ };
pub const Filter = struct {
role: ?[]const u8 = null,
@@ -61,6 +81,7 @@ pub const Writer = struct {
pub const Opts = struct {
depth: i32 = 0,
filter: ?Filter = null,
+ scope: Scope = .subtree,
};
const ResolvedRole = struct {
@@ -71,6 +92,22 @@ pub const Writer = struct {
promoted_input: ?*DOMNode.Element.Html.Input,
};
+ // A writer bound to `frame`, the frame that owns `root`.
+ pub fn init(frame: *Frame, root: *const NodeRegistry.Node, registry: *NodeRegistry, temp_arena: *lp.Arena, opts: Opts) !Writer {
+ const label_index = try frame.call_arena.create(Label.LabelByForIndex);
+ label_index.* = .{};
+ return .{
+ .frame = frame,
+ .root = root,
+ .registry = registry,
+ .label_index = label_index,
+ .temp_arena = temp_arena,
+ .filter = opts.filter,
+ .depth = opts.depth,
+ .scope = opts.scope,
+ };
+ }
+
pub fn jsonStringify(self: *const Writer, w: anytype) error{WriteFailed}!void {
self.toJSON(w) catch |err| {
// The only error our jsonStringify method can return is
@@ -86,15 +123,135 @@ pub const Writer = struct {
defer cache_arena.release();
var ignore_cache: IgnoreCache = .{ .allocator = cache_arena.allocator() };
+ if (self.scope == .bare_node) {
+ return self.writeScope(&ignore_cache, w);
+ }
try w.beginArray();
if (self.filter != null) {
try self.walkQuery(&ignore_cache, w);
} else {
- try self.writeTree(&ignore_cache, w);
+ try self.writeScope(&ignore_cache, w);
}
return w.endArray();
}
+ fn writeScope(self: *const Writer, ignore_cache: *IgnoreCache, w: anytype) !void {
+ const root = self.root.dom;
+ if (self.scope == .subtree) {
+ return self.writeTree(ignore_cache, w);
+ }
+
+ // Every other scope is about a node in the AX tree. Like Chrome, a
+ // node without an AX node is a lone ignored node, and has no children.
+ const chain = try self.axChain(root) orelse {
+ if (self.scope != .children) {
+ try self.writeNotRendered(self.root.id, w);
+ }
+ return;
+ };
+
+ switch (self.scope) {
+ .subtree => unreachable,
+ .node, .bare_node => try self.writeChain(chain[0..1], ignore_cache, w),
+ // Chrome lists the node first, then its ancestors, siblings and
+ // children.
+ .relatives => {
+ try self.writeChain(chain, ignore_cache, w);
+ if (chain.len > 1) {
+ try self.writeChildren(chain[1].node, root, ignore_cache, w);
+ }
+ try self.writeChildren(root, null, ignore_cache, w);
+ },
+ .ancestors => try self.writeChain(chain, ignore_cache, w),
+ .children => try self.writeChildren(root, null, ignore_cache, w),
+ }
+ }
+
+ const ChainEntry = struct {
+ node: *DOMNode,
+ // Never .pruned: the node is in the AX tree.
+ hidden: Hidden,
+ };
+
+ // `node` then its AX ancestors up to its frame's document. Null when
+ // `node` isn't in the AX tree: some node on the way isn't listed in its
+ // parent's childIds.
+ fn axChain(self: *const Writer, node: *DOMNode) !?[]const ChainEntry {
+ const allocator = self.temp_arena.allocator();
+ var chain: std.ArrayList(ChainEntry) = .empty;
+ var current = node;
+ while (current.flatParent(self.frame)) |parent| : (current = parent) {
+ // An unslotted child of a host, or unused slot fallback: the
+ // flat tree skips it, though flatParent still finds the parent.
+ if (current.isOutsideFlatTree() or AXNode.fromNode(parent).ignoreChildren()) {
+ return null;
+ }
+ const hidden = axChildHidden(current, ignoreText(parent), self.frame) orelse return null;
+ try chain.append(allocator, .{ .node = current, .hidden = hidden });
+ }
+ if (current._type != .document) {
+ return null;
+ }
+ try chain.append(allocator, .{ .node = current, .hidden = .visible });
+ return chain.items;
+ }
+
+ fn writeChain(self: *const Writer, chain: []const ChainEntry, ignore_cache: *IgnoreCache, w: anytype) !void {
+ for (chain) |entry| {
+ try self.writeSingle(entry.node, entry.hidden, ignore_cache, w);
+ }
+ }
+
+ // Chrome's AXNode for a DOM node that has no AX node.
+ fn writeNotRendered(self: *const Writer, id: u32, w: anytype) !void {
+ try w.beginObject();
+ try w.objectField("nodeId");
+ try writeIdString(id, w);
+ try w.objectField("backendDOMNodeId");
+ try w.write(id);
+ try w.objectField("role");
+ try self.writeAXValue(.{ .role = "none" }, w);
+ try w.objectField("ignored");
+ try w.write(true);
+ try self.writeIgnoredReason("notRendered", w);
+ try w.endObject();
+ }
+
+ // The ignoredReasons field of an ignored node, with the one reason.
+ fn writeIgnoredReason(self: *const Writer, reason: []const u8, w: anytype) !void {
+ try w.objectField("ignoredReasons");
+ try w.beginArray();
+ try w.beginObject();
+ try w.objectField("name");
+ try w.write(reason);
+ try w.objectField("value");
+ try self.writeAXValue(.{ .boolean = true }, w);
+ try w.endObject();
+ try w.endArray();
+ }
+
+ // Write `dom`, a node in the AX tree, without its descendants. `hidden`
+ // is never .pruned for such a node.
+ fn writeSingle(self: *const Writer, dom: *DOMNode, hidden: Hidden, ignore_cache: *IgnoreCache, w: anytype) !void {
+ const node = try self.registry.register(dom);
+ _ = try self.writeNode(node.id, .fromNode(dom), hidden, ignore_cache, w);
+ }
+
+ // Write each node listed in `parent`'s childIds, except `skip`, in the
+ // same order. `parent` must be in the AX tree.
+ fn writeChildren(self: *const Writer, parent: *DOMNode, skip: ?*DOMNode, ignore_cache: *IgnoreCache, w: anytype) !void {
+ if (AXNode.fromNode(parent).ignoreChildren()) {
+ return;
+ }
+ var it: AXChildren = .init(parent, self.frame);
+ while (it.next()) |child| switch (child) {
+ .marker => try self.writeListMarker(parent, w),
+ .node => |c| if (c.dom != skip) {
+ try self.writeSingle(c.dom, c.hidden, ignore_cache, w);
+ },
+ };
+ }
+
/// Resolve the displayed role for `axn`, accounting for label-promotion
/// when a <label> targets a hidden checkbox/radio. Shared between the
/// tree (writeNode) and query (emitMatch) paths so the two can't drift.
@@ -123,51 +280,44 @@ pub const Writer = struct {
fn writeTree(self: *const Writer, ignore_cache: *IgnoreCache, w: anytype) !void {
var walker: Walker = .init(self.root.dom, self.frame);
var descend = try self.writeNode(self.root.id, .fromNode(self.root.dom), walker.hidden(self.root.dom), ignore_cache, w);
+ if (descend) try self.writeListMarker(self.root.dom, w);
while (walker.next(descend)) |dom_node| {
descend = false;
if (self.depth > 0 and self.depth < walker.depth) continue;
- switch (dom_node._type) {
- .cdata => {
- if (dom_node.is(DOMNode.CData.Text) == null) {
- continue;
- }
- if (ignoreText(dom_node.flatParent(self.frame).?)) {
- continue;
- }
- },
- .element => {},
- else => continue,
+ // Only text needs its parent's ignoreText.
+ const skip_text = dom_node._type == .cdata and ignoreText(dom_node.flatParent(self.frame).?);
+ if (!isAXChildType(dom_node, skip_text)) {
+ continue;
}
-
const hidden = walker.hidden(dom_node);
if (hidden == .pruned) {
continue;
}
const node = try self.registry.register(dom_node);
descend = try self.writeNode(node.id, .fromNode(dom_node), hidden, ignore_cache, w);
+ // The marker is a child: drop it with the others at the depth
+ // cut-off.
+ if (descend and (self.depth <= 0 or walker.depth < self.depth)) {
+ try self.writeListMarker(dom_node, w);
+ }
}
}
+ // The tree walk follows an <li> with its synthetic ListMarker node. A
+ // no-op for any other node: see hasListMarker.
fn writeListMarker(self: *const Writer, li_node: *DOMNode, w: anytype) !void {
- // Find the parent list element
- const parent = li_node._parent orelse return;
- const parent_el = parent.is(DOMNode.Element) orelse return;
- const list_type = parent_el.getTag();
-
- // Only create markers for actual list elements
- switch (list_type) {
- .ul, .ol, .menu => {},
- else => return,
+ if (!hasListMarker(li_node)) {
+ return;
}
+ const parent = li_node._parent.?;
+ const list_type = parent.as(DOMNode.Element).getTag();
// Write the ListMarker node
try w.beginObject();
- // Use the next available ID for the marker
try w.objectField("nodeId");
- const marker_id = self.registry.node_id;
- self.registry.node_id += 1;
+ const marker_id = try self.registry.listMarkerId(li_node);
try writeIdString(marker_id, w);
try w.objectField("backendDOMNodeId");
@@ -605,16 +755,7 @@ pub const Writer = struct {
try w.write(ignore);
if (ignore) {
- // Ignore reasons
- try w.objectField("ignoredReasons");
- try w.beginArray();
- try w.beginObject();
- try w.objectField("name");
- try w.write("uninteresting");
- try w.objectField("value");
- try self.writeAXValue(.{ .boolean = true }, w);
- try w.endObject();
- try w.endArray();
+ try self.writeIgnoredReason("uninteresting", w);
} else {
// Name
try w.objectField("name");
@@ -661,40 +802,23 @@ pub const Writer = struct {
// Children
const write_children = axn.ignoreChildren() == false and hidden != .pruned;
- const skip_text = ignoreText(axn.dom);
try w.objectField("childIds");
try w.beginArray();
if (write_children) {
- var registry = self.registry;
- var it: TreeWalker.FlatChildren = .init(n, .{ .frame = self.frame });
+ var it: AXChildren = .init(n, self.frame);
while (it.next()) |child| {
- // ignore non-elements or text.
- if (child.is(DOMNode.Element.Html) == null and (child.is(DOMNode.CData.Text) == null or skip_text)) {
- continue;
- }
-
- // Matches writeTree's pruning
- if (hiddenState(child, self.frame) == .pruned) {
- continue;
- }
-
- const child_node = try registry.register(child);
- try writeIdString(child_node.id, w);
+ const child_id = switch (child) {
+ .marker => try self.registry.listMarkerId(n),
+ .node => |c| (try self.registry.register(c.dom)).id,
+ };
+ try writeIdString(child_id, w);
}
}
try w.endArray();
try w.endObject();
- if (write_children) {
- if (n.is(DOMNode.Element)) |el| {
- if (el.getTag() == .li) {
- try self.writeListMarker(n, w);
- }
- }
- }
-
return write_children;
}
@@ -1394,6 +1518,74 @@ fn scratchAllocator(temp_arena: ?*lp.Arena, frame: *Frame) std.mem.Allocator {
/// `visibility: visible`. `pruned` ignores the whole subtree.
const Hidden = enum { visible, invisible, pruned };
+/// Whether the tree has a ListMarker node for `node`: an <li> of a list.
+pub fn hasListMarker(node: *DOMNode) bool {
+ const el = node.is(DOMNode.Element) orelse return false;
+ if (el.getTag() != .li) {
+ return false;
+ }
+ const parent = (node._parent orelse return false).is(DOMNode.Element) orelse return false;
+ return switch (parent.getTag()) {
+ .ul, .ol, .menu => true,
+ else => false,
+ };
+}
+
+/// Whether a flat child of this type can be an AX node: an element, or text
+/// unless `skip_text` (ignoreText of the parent). Pruning aside, these are
+/// the nodes writeTree walks and childIds lists.
+fn isAXChildType(child: *DOMNode, skip_text: bool) bool {
+ return child.is(DOMNode.Element) != null or (child.is(DOMNode.CData.Text) != null and !skip_text);
+}
+
+/// The hidden state of `child` when it appears in its parent's childIds,
+/// null when it doesn't. `skip_text` is ignoreText of the parent.
+fn axChildHidden(child: *DOMNode, skip_text: bool, frame: *Frame) ?Hidden {
+ if (!isAXChildType(child, skip_text)) {
+ return null;
+ }
+ const hidden = hiddenState(child, frame);
+ return if (hidden == .pruned) null else hidden;
+}
+
+/// The entries of a node's childIds, in order: an <li>'s ListMarker, then
+/// each flat child in the AX tree. The caller checks the node shows its
+/// children at all (ignoreChildren, pruning).
+const AXChildren = struct {
+ parent: *DOMNode,
+ frame: *Frame,
+ skip_text: bool,
+ marker_pending: bool,
+ it: TreeWalker.FlatChildren,
+
+ const Child = union(enum) {
+ marker,
+ node: struct { dom: *DOMNode, hidden: Hidden },
+ };
+
+ fn init(parent: *DOMNode, frame: *Frame) AXChildren {
+ return .{
+ .parent = parent,
+ .frame = frame,
+ .skip_text = ignoreText(parent),
+ .marker_pending = hasListMarker(parent),
+ .it = .init(parent, .{ .frame = frame }),
+ };
+ }
+
+ fn next(self: *AXChildren) ?Child {
+ if (self.marker_pending) {
+ self.marker_pending = false;
+ return .marker;
+ }
+ while (self.it.next()) |child| {
+ const hidden = axChildHidden(child, self.skip_text, self.frame) orelse continue;
+ return .{ .node = .{ .dom = child, .hidden = hidden } };
+ }
+ return null;
+ }
+};
+
/// Text takes its flat-tree parent's state.
fn hiddenState(node: *DOMNode, frame: *Frame) Hidden {
const elt = node.is(DOMNode.Element) orelse node.flatTreeParentElement(frame) orelse return .visible;
@@ -1464,7 +1656,9 @@ fn ignoreChildren(self: AXNode) bool {
const elt = node.as(DOMNode.Element);
return switch (elt.getTag()) {
- .head, .script, .style => true,
+ // An <iframe>'s content is another frame's tree; its DOM children
+ // are only fallback text.
+ .head, .script, .style, .iframe => true,
else => false,
};
}
diff --git a/src/server/cdp/CDP.zig b/src/server/cdp/CDP.zig
index 1ae7eacc9..fdb10d3d3 100644
--- a/src/server/cdp/CDP.zig
+++ b/src/server/cdp/CDP.zig
@@ -32,7 +32,6 @@ const Mime = @import("../../browser/Mime.zig");
const Frame = @import("../../browser/Frame.zig");
const Browser = @import("../../browser/Browser.zig");
const Session = @import("../../browser/Session.zig");
-const Label = @import("../../browser/webapi/element/html/Label.zig");
const WS = @import("../WS.zig");
const Link = @import("../Link.zig");
@@ -765,17 +764,7 @@ pub const BrowserContext = struct {
// cross-frame queries produces names/visibility from the wrong document.
const main_frame = self.mainFrame() orelse return error.FrameNotLoaded;
const frame = root.dom.ownerFrame(main_frame) orelse return error.InvalidNodeId;
- const label_index = try frame.call_arena.create(Label.LabelByForIndex);
- label_index.* = .{};
- return .{
- .frame = frame,
- .root = root,
- .registry = &self.node_registry,
- .label_index = label_index,
- .temp_arena = temp_arena,
- .filter = opts.filter,
- .depth = opts.depth,
- };
+ return .init(frame, root, &self.node_registry, temp_arena, opts);
}
// The root frame of this context's live Page.
diff --git a/src/server/cdp/domains/accessibility.zig b/src/server/cdp/domains/accessibility.zig
index e9ebcfefa..629e525c1 100644
--- a/src/server/cdp/domains/accessibility.zig
+++ b/src/server/cdp/domains/accessibility.zig
@@ -17,14 +17,14 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
-const lp = @import("lightpanda");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
+const AXNode = @import("../AXNode.zig");
const dom = @import("dom.zig");
-
-const log = lp.log;
+const Frame = @import("../../../browser/Frame.zig");
+const NodeRegistry = @import("../../../NodeRegistry.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
@@ -33,6 +33,9 @@ pub fn processMessage(cmd: *CDP.Command) !void {
getFullAXTree,
getPartialAXTree,
queryAXTree,
+ getRootAXNode,
+ getAXNodeAndAncestors,
+ getChildAXNodes,
}, cmd.input.action) orelse return error.UnknownMethod;
switch (action) {
@@ -41,8 +44,12 @@ pub fn processMessage(cmd: *CDP.Command) !void {
.getFullAXTree => return getFullAXTree(cmd),
.getPartialAXTree => return getPartialAXTree(cmd),
.queryAXTree => return queryAXTree(cmd),
+ .getRootAXNode => return getRootAXNode(cmd),
+ .getAXNodeAndAncestors => return getAXNodeAndAncestors(cmd),
+ .getChildAXNodes => return getChildAXNodes(cmd),
}
}
+
fn enable(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
@@ -59,26 +66,110 @@ fn getFullAXTree(cmd: *CDP.Command) !void {
const params: Params = (try cmd.params(Params)) orelse .{};
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
- const session = bc.session;
+ const frame = try findFrame(cmd, bc, params.frameId) orelse return;
- const frame = blk: {
- const frame_id = params.frameId orelse {
- break :blk bc.mainFrame() orelse return error.FrameNotLoaded;
- };
- break :blk session.findFrameByFrameId(try id.parseFrameId(frame_id)) orelse {
- return cmd.sendError(-32000, "Frame with the given id does not belong to the target.", .{});
- };
+ const node = try bc.node_registry.register(frame.window._document.asNode());
+ return sendAXNodes(cmd, bc, frame, node, .{
+ .depth = params.depth orelse 0,
+ });
+}
+
+fn getRootAXNode(cmd: *CDP.Command) !void {
+ const Params = struct {
+ frameId: ?[]const u8 = null,
+ };
+ const params: Params = (try cmd.params(Params)) orelse .{};
+
+ const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
+ const frame = try findFrame(cmd, bc, params.frameId) orelse return;
+
+ const node = try bc.node_registry.register(frame.window._document.asNode());
+ return sendAXNodes(cmd, bc, frame, node, .{
+ .scope = .bare_node,
+ });
+}
+
+fn getAXNodeAndAncestors(cmd: *CDP.Command) !void {
+ const Params = struct {
+ nodeId: ?u32 = null,
+ backendNodeId: ?u32 = null,
+ objectId: ?[]const u8 = null,
+ };
+ const params: Params = (try cmd.params(Params)) orelse .{};
+
+ const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
+ const node = try dom.getNode(cmd.arena, bc, cmd.input.session_id, params.nodeId, params.backendNodeId, params.objectId);
+
+ return sendAXNodes(cmd, bc, bc.mainFrame() orelse return error.FrameNotLoaded, node, .{
+ .scope = .ancestors,
+ });
+}
+
+fn getChildAXNodes(cmd: *CDP.Command) !void {
+ const Params = struct {
+ id: []const u8,
+ frameId: ?[]const u8 = null,
};
+ const params = (try cmd.params(Params)) orelse return error.InvalidParams;
- const doc = frame.window._document.asNode();
- const node = try bc.node_registry.register(doc);
+ const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
+ const frame = try findFrame(cmd, bc, params.frameId) orelse return;
+ // The AXNodeId is our node registry id, unique across frames: the frame
+ // only has to own the node.
+ const ax_id = std.fmt.parseInt(u32, params.id, 10) catch return error.InvalidParams;
+ const node = bc.node_registry.lookup_by_id.get(ax_id) orelse {
+ return getListMarkerChildren(cmd, bc, frame, params.frameId != null, ax_id);
+ };
+ // A frameless node fails in axnodeWriter, like in the other methods.
+ if (node.dom.ownerFrame(frame)) |owner| {
+ if (params.frameId != null and owner != frame) {
+ return error.NodeNotFound;
+ }
+ }
+
+ return sendAXNodes(cmd, bc, frame, node, .{
+ .scope = .children,
+ });
+}
+
+// A ListMarker has no DOM node, and no children. It's checked like its <li>,
+// and is gone once the <li> no longer has one.
+fn getListMarkerChildren(cmd: *CDP.Command, bc: *CDP.BrowserContext, frame: *Frame, check_frame: bool, ax_id: u32) !void {
+ const li = bc.node_registry.listMarkerOwner(ax_id) orelse return error.NodeNotFound;
+ if (!AXNode.hasListMarker(li)) {
+ return error.NodeNotFound;
+ }
+ const owner = li.ownerFrame(frame) orelse return error.InvalidNodeId;
+ if (check_frame and owner != frame) {
+ return error.NodeNotFound;
+ }
+ return cmd.sendResult(.{ .nodes = [0]u32{} }, .{});
+}
+
+// Reply with the AX nodes `opts` selects around `node`: a `nodes` list, or a
+// single `node` for the .bare_node scope.
+fn sendAXNodes(cmd: *CDP.Command, bc: *CDP.BrowserContext, frame: *Frame, node: *const NodeRegistry.Node, opts: AXNode.Writer.Opts) !void {
const temp_arena = try frame.getArena(.medium, "AXNode");
defer temp_arena.release();
- return cmd.sendResult(.{ .nodes = try bc.axnodeWriter(temp_arena, node, .{
- .depth = params.depth orelse 0,
- }) }, .{});
+ const writer = try bc.axnodeWriter(temp_arena, node, opts);
+ if (opts.scope == .bare_node) {
+ return cmd.sendResult(.{ .node = writer }, .{});
+ }
+ return cmd.sendResult(.{ .nodes = writer }, .{});
+}
+
+// The frame `frame_id` names, or the main frame when null. Returns null after
+// replying with an error when the frame isn't part of this target.
+fn findFrame(cmd: *CDP.Command, bc: *CDP.BrowserContext, frame_id: ?[]const u8) !?*Frame {
+ const frame_id_ = frame_id orelse {
+ return bc.mainFrame() orelse return error.FrameNotLoaded;
+ };
+ return bc.session.findFrameByFrameId(try id.parseFrameId(frame_id_)) orelse {
+ try cmd.sendError(-32000, "Frame with the given id does not belong to the target.", .{});
+ return null;
+ };
}
fn queryAXTree(cmd: *CDP.Command) !void {
@@ -94,16 +185,12 @@ fn queryAXTree(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const node = try dom.getNode(cmd.arena, bc, cmd.input.session_id, params.nodeId, params.backendNodeId, params.objectId);
- const frame = bc.mainFrame() orelse return error.FrameNotLoaded;
- const temp_arena = try frame.getArena(.medium, "AXNode");
- defer temp_arena.release();
-
- return cmd.sendResult(.{ .nodes = try bc.axnodeWriter(temp_arena, node, .{
+ return sendAXNodes(cmd, bc, bc.mainFrame() orelse return error.FrameNotLoaded, node, .{
.filter = .{
.accessible_name = params.accessibleName,
.role = params.role,
},
- }) }, .{});
+ });
}
fn getPartialAXTree(cmd: *CDP.Command) !void {
@@ -111,32 +198,17 @@ fn getPartialAXTree(cmd: *CDP.Command) !void {
nodeId: ?u32 = null,
backendNodeId: ?u32 = null,
objectId: ?[]const u8 = null,
- // Accepted for Chrome protocol compatibility. The ancestor chain is
- // not yet emitted; this returns the subtree rooted at the resolved
- // node, matching the scope of queryAXTree.
fetchRelatives: ?bool = null,
};
const params: Params = (try cmd.params(Params)) orelse .{};
- if (params.fetchRelatives orelse true) {
- // orelse true, because that's what Chrome defaults too, and if people
- // aren't setting it, then they're expecting true.
- log.debug(.not_implemented, "getPartialAXTree", .{
- .cdp_cmd = "Accessibility.getPartialAXTree",
- .param = "fetchRelatives",
- });
- }
-
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const node = try dom.getNode(cmd.arena, bc, cmd.input.session_id, params.nodeId, params.backendNodeId, params.objectId);
- const frame = bc.mainFrame() orelse return error.FrameNotLoaded;
- const temp_arena = try frame.getArena(.medium, "AXNode");
- defer temp_arena.release();
-
- // No filter: emit the full accessibility subtree rooted at the resolved
- // node, the same shape getFullAXTree produces for the document root.
- return cmd.sendResult(.{ .nodes = try bc.axnodeWriter(temp_arena, node, .{}) }, .{});
+ return sendAXNodes(cmd, bc, bc.mainFrame() orelse return error.FrameNotLoaded, node, .{
+ // Defaults to true, like Chrome.
+ .scope = if (params.fetchRelatives orelse true) .relatives else .node,
+ });
}
const testing = @import("../testing.zig");
@@ -218,8 +290,335 @@ test "cdp.accessibility: getPartialAXTree rejects a frameless node" {
try ctx.expectSentError(-31998, "InvalidNodeId", .{ .id = 2 });
}
-// The result.result.objectId of the Runtime.evaluate response to `msg_id`.
-fn evaluatedObjectId(ctx: *testing.TestContext, msg_id: i64) ![]const u8 {
+test "cdp.accessibility: getRootAXNode returns the document node" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_tree.html" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Accessibility.getRootAXNode" });
+ const node = (try sentResult(&ctx, 1)).get("node").?.object;
+ try testing.expectEqual("RootWebArea", axRole(node));
+ try testing.expectEqual("AX Tree Fixture", node.get("name").?.object.get("value").?.string);
+ try testing.expect(node.get("childIds").?.array.items.len > 0);
+ try testing.expectEqual(null, node.get("parentId"));
+
+ try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getRootAXNode", .params = .{ .frameId = "FID-0000009999" } });
+ try ctx.expectSentError(-32000, "Frame with the given id does not belong to the target.", .{ .id = 2 });
+}
+
+test "cdp.accessibility: getAXNodeAndAncestors returns the node then its ancestors" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_tree.html" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.getElementById('visible-para')",
+ } });
+ const object_id = try evaluatedObjectId(&ctx, 1);
+
+ try ctx.processMessage(.{
+ .id = 2,
+ .method = "Accessibility.getAXNodeAndAncestors",
+ .params = .{ .objectId = object_id },
+ });
+ const nodes = (try sentResult(&ctx, 2)).get("nodes").?.array.items;
+ // p, main, body, html, document
+ try testing.expectEqual(5, nodes.len);
+ try testing.expectEqual("paragraph", axRole(nodes[0].object));
+ try testing.expectEqual("main", axRole(nodes[1].object));
+ try testing.expectEqual("RootWebArea", axRole(nodes[4].object));
+ for (nodes[0 .. nodes.len - 1], nodes[1..]) |child, parent| {
+ try testing.expectEqual(parent.object.get("nodeId").?.string, child.object.get("parentId").?.string);
+ }
+}
+
+test "cdp.accessibility: getChildAXNodes returns the direct children" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_tree.html" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.querySelector('main')",
+ } });
+ const object_id = try evaluatedObjectId(&ctx, 1);
+
+ try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getPartialAXTree", .params = .{
+ .objectId = object_id,
+ .fetchRelatives = false,
+ } });
+ const main = (try sentResult(&ctx, 2)).get("nodes").?.array.items[0].object;
+ const main_id = main.get("nodeId").?.string;
+ const child_ids = main.get("childIds").?.array.items;
+
+ try ctx.processMessage(.{ .id = 3, .method = "Accessibility.getChildAXNodes", .params = .{ .id = main_id } });
+ const nodes = (try sentResult(&ctx, 3)).get("nodes").?.array.items;
+ try testing.expectEqual(child_ids.len, nodes.len);
+ for (nodes, child_ids) |n, cid| {
+ try testing.expectEqual(cid.string, n.object.get("nodeId").?.string);
+ try testing.expectEqual(main_id, n.object.get("parentId").?.string);
+ }
+ // Whitespace text between the elements is exposed too.
+ var saw_heading = false;
+ var saw_text = false;
+ for (nodes) |n| {
+ const role = axRole(n.object);
+ if (std.mem.eql(u8, role, "heading")) saw_heading = true;
+ if (std.mem.eql(u8, role, "StaticText")) saw_text = true;
+ }
+ try testing.expect(saw_heading and saw_text);
+
+ try ctx.processMessage(.{ .id = 4, .method = "Accessibility.getChildAXNodes", .params = .{ .id = "99999" } });
+ try ctx.expectSentError(-31998, "NodeNotFound", .{ .id = 4 });
+}
+
+test "cdp.accessibility: getPartialAXTree fetchRelatives" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_tree.html" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.getElementById('wrap-label')",
+ } });
+ const object_id = try evaluatedObjectId(&ctx, 1);
+
+ // Without relatives, only the node itself.
+ try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getPartialAXTree", .params = .{
+ .objectId = object_id,
+ .fetchRelatives = false,
+ } });
+ const alone = (try sentResult(&ctx, 2)).get("nodes").?.array.items;
+ try testing.expectEqual(1, alone.len);
+ const label_id = alone[0].object.get("nodeId").?.string;
+
+ // fetchRelatives defaults to true: the node, its ancestors, siblings and
+ // children, but nothing deeper.
+ try ctx.processMessage(.{ .id = 3, .method = "Accessibility.getPartialAXTree", .params = .{
+ .objectId = object_id,
+ } });
+ const nodes = (try sentResult(&ctx, 3)).get("nodes").?.array.items;
+ try testing.expectEqual(label_id, nodes[0].object.get("nodeId").?.string);
+
+ var saw_root = false;
+ var heading_id: ?[]const u8 = null;
+ var saw_child = false;
+ for (nodes) |n| {
+ const role = axRole(n.object);
+ if (std.mem.eql(u8, role, "RootWebArea")) saw_root = true;
+ if (std.mem.eql(u8, role, "heading")) heading_id = n.object.get("nodeId").?.string;
+ if (std.mem.eql(u8, role, "textbox")) saw_child = true;
+ }
+ try testing.expect(saw_root and saw_child);
+ // A sibling is listed, but not its children.
+ for (nodes) |n| {
+ const parent_id = (n.object.get("parentId") orelse continue).string;
+ try testing.expect(!std.mem.eql(u8, parent_id, heading_id.?));
+ }
+}
+
+test "cdp.accessibility: getChildAXNodes lists an li's ListMarker first" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_relatives.html" });
+
+ const li_id = try axNodeIdOf(&ctx, 1, "document.getElementById('item')");
+
+ try ctx.processMessage(.{ .id = 3, .method = "Accessibility.getChildAXNodes", .params = .{ .id = li_id } });
+ const nodes = (try sentResult(&ctx, 3)).get("nodes").?.array.items;
+ try testing.expectEqual(2, nodes.len);
+ const marker = nodes[0].object;
+ try testing.expectEqual("ListMarker", axRole(marker));
+ try testing.expectEqual("1. ", marker.get("name").?.object.get("value").?.string);
+ try testing.expectEqual(li_id, marker.get("parentId").?.string);
+ try testing.expectEqual("StaticText", axRole(nodes[1].object));
+ const marker_id = marker.get("nodeId").?.string;
+
+ // The li lists the marker, and its id doesn't change.
+ try ctx.processMessage(.{ .id = 5, .method = "Accessibility.getAXNodeAndAncestors", .params = .{ .nodeId = try std.fmt.parseInt(u32, li_id, 10) } });
+ const li = (try sentResult(&ctx, 5)).get("nodes").?.array.items[0].object;
+ try testing.expectEqual(marker_id, li.get("childIds").?.array.items[0].string);
+
+ // A marker has no children.
+ try ctx.processMessage(.{ .id = 6, .method = "Accessibility.getChildAXNodes", .params = .{ .id = marker_id } });
+ try testing.expectEqual(0, (try sentResult(&ctx, 6)).get("nodes").?.array.items.len);
+
+ // It belongs to its <li>'s frame, not the iframe's.
+ try ctx.processMessage(.{ .id = 7, .method = "Accessibility.getChildAXNodes", .params = .{
+ .id = marker_id,
+ .frameId = "FID-0000000002",
+ } });
+ try ctx.expectSentError(-31998, "NodeNotFound", .{ .id = 7 });
+
+ // Out of its list, the li has no marker.
+ try ctx.processMessage(.{ .id = 8, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.body.append(document.getElementById('item'))",
+ } });
+ _ = try sentResult(&ctx, 8);
+ try ctx.processMessage(.{ .id = 9, .method = "Accessibility.getChildAXNodes", .params = .{ .id = marker_id } });
+ try ctx.expectSentError(-31998, "NodeNotFound", .{ .id = 9 });
+
+ // The frame must own the node.
+ try ctx.processMessage(.{ .id = 4, .method = "Accessibility.getChildAXNodes", .params = .{
+ .id = li_id,
+ .frameId = "FID-0000009999",
+ } });
+ try ctx.expectSentError(-32000, "Frame with the given id does not belong to the target.", .{ .id = 4 });
+}
+
+test "cdp.accessibility: getFullAXTree depth cuts off an li's ListMarker" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_relatives.html" });
+
+ // document, html, body, ol, li (depth 4), then its marker and text.
+ try ctx.processMessage(.{ .id = 1, .method = "Accessibility.getFullAXTree", .params = .{ .depth = 4 } });
+ var saw_li = false;
+ for ((try sentResult(&ctx, 1)).get("nodes").?.array.items) |n| {
+ const role = axRole(n.object);
+ try testing.expect(!std.mem.eql(u8, role, "ListMarker"));
+ if (std.mem.eql(u8, role, "listitem")) saw_li = true;
+ }
+ try testing.expect(saw_li);
+
+ // A negative depth is no limit, for the marker too.
+ inline for (.{ 5, -1 }, 2..) |depth, msg_id| {
+ try ctx.processMessage(.{ .id = msg_id, .method = "Accessibility.getFullAXTree", .params = .{ .depth = depth } });
+ var saw_marker = false;
+ for ((try sentResult(&ctx, msg_id)).get("nodes").?.array.items) |n| {
+ if (std.mem.eql(u8, axRole(n.object), "ListMarker")) saw_marker = true;
+ }
+ try testing.expect(saw_marker);
+ }
+}
+
+test "cdp.accessibility: a node outside the AX tree is a lone ignored node" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_relatives.html" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.getElementById('in-gone')",
+ } });
+ const gone = try evaluatedObjectId(&ctx, 1);
+ try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getAXNodeAndAncestors", .params = .{ .objectId = gone } });
+ try expectNotRendered((try sentResult(&ctx, 2)).get("nodes").?.array.items);
+
+ // <title>: <head> exposes no children.
+ try ctx.processMessage(.{ .id = 3, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.querySelector('title')",
+ } });
+ const title = try evaluatedObjectId(&ctx, 3);
+ try ctx.processMessage(.{ .id = 4, .method = "Accessibility.getPartialAXTree", .params = .{ .objectId = title } });
+ const nodes = (try sentResult(&ctx, 4)).get("nodes").?.array.items;
+ try expectNotRendered(nodes);
+
+ try ctx.processMessage(.{ .id = 5, .method = "Accessibility.getChildAXNodes", .params = .{
+ .id = nodes[0].object.get("nodeId").?.string,
+ } });
+ try testing.expectEqual(0, (try sentResult(&ctx, 5)).get("nodes").?.array.items.len);
+}
+
+test "cdp.accessibility: an svg is listed in its parent's childIds" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_relatives.html" });
+
+ const svg_id = try axNodeIdOf(&ctx, 1, "document.getElementById('pic')");
+ try ctx.processMessage(.{ .id = 3, .method = "Accessibility.getAXNodeAndAncestors", .params = .{ .nodeId = try std.fmt.parseInt(u32, svg_id, 10) } });
+ const nodes = (try sentResult(&ctx, 3)).get("nodes").?.array.items;
+ try testing.expectEqual(svg_id, nodes[0].object.get("nodeId").?.string);
+ const body = nodes[1].object;
+ try testing.expectEqual(body.get("nodeId").?.string, nodes[0].object.get("parentId").?.string);
+
+ var listed = false;
+ for (body.get("childIds").?.array.items) |cid| {
+ if (std.mem.eql(u8, cid.string, svg_id)) listed = true;
+ }
+ try testing.expect(listed);
+}
+
+test "cdp.accessibility: an iframe's tree is its own" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_relatives.html" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.getElementById('frame').contentDocument.getElementById('inner')",
+ } });
+ const object_id = try evaluatedObjectId(&ctx, 1);
+
+ // The ancestors stop at the iframe's document, the root of its frame's
+ // tree, as getFullAXTree for that frame has it.
+ try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getAXNodeAndAncestors", .params = .{ .objectId = object_id } });
+ const nodes = (try sentResult(&ctx, 2)).get("nodes").?.array.items;
+ // button, body, html, document
+ try testing.expectEqual(4, nodes.len);
+ try testing.expectEqual("button", axRole(nodes[0].object));
+ try testing.expectEqual("RootWebArea", axRole(nodes[3].object));
+ for (nodes[0 .. nodes.len - 1], nodes[1..]) |child, parent| {
+ try testing.expectEqual(parent.object.get("nodeId").?.string, child.object.get("parentId").?.string);
+ }
+ try testing.expectEqual(null, nodes[3].object.get("parentId"));
+
+ // The main frame doesn't own the iframe's nodes.
+ try ctx.processMessage(.{ .id = 4, .method = "Accessibility.getChildAXNodes", .params = .{
+ .id = nodes[1].object.get("nodeId").?.string,
+ .frameId = "FID-0000000001",
+ } });
+ try ctx.expectSentError(-31998, "NodeNotFound", .{ .id = 4 });
+
+ // And the <iframe> has no AX children in its parent's tree: every
+ // childId getFullAXTree lists is in its response.
+ try ctx.processMessage(.{ .id = 5, .method = "Accessibility.getFullAXTree" });
+ const tree = (try sentResult(&ctx, 5)).get("nodes").?.array.items;
+ var ids: std.StringHashMapUnmanaged(void) = .empty;
+ defer ids.deinit(testing.allocator);
+ for (tree) |n| {
+ try ids.put(testing.allocator, n.object.get("nodeId").?.string, {});
+ }
+ for (tree) |n| {
+ for (n.object.get("childIds").?.array.items) |cid| {
+ try testing.expect(ids.contains(cid.string));
+ }
+ }
+}
+
+fn expectNotRendered(nodes: []const std.json.Value) !void {
+ try testing.expectEqual(1, nodes.len);
+ const node = nodes[0].object;
+ try testing.expectEqual("none", axRole(node));
+ try testing.expectEqual(true, node.get("ignored").?.bool);
+ try testing.expectEqual("notRendered", node.get("ignoredReasons").?.array.items[0].object.get("name").?.string);
+ try testing.expectEqual(null, node.get("parentId"));
+ try testing.expectEqual(null, node.get("childIds"));
+}
+
+// The AXNodeId of the node `expression` evaluates to. Uses message ids
+// `msg_id` and `msg_id + 1`.
+fn axNodeIdOf(ctx: *testing.TestContext, msg_id: i64, expression: []const u8) ![]const u8 {
+ try ctx.processMessage(.{ .id = msg_id, .method = "Runtime.evaluate", .params = .{ .expression = expression } });
+ const object_id = try evaluatedObjectId(ctx, msg_id);
+ try ctx.processMessage(.{ .id = msg_id + 1, .method = "Accessibility.getPartialAXTree", .params = .{
+ .objectId = object_id,
+ .fetchRelatives = false,
+ } });
+ return (try sentResult(ctx, msg_id + 1)).get("nodes").?.array.items[0].object.get("nodeId").?.string;
+}
+
+fn axRole(node: std.json.ObjectMap) []const u8 {
+ return node.get("role").?.object.get("value").?.string;
+}
+
+// The result of the response to `msg_id`.
+fn sentResult(ctx: *testing.TestContext, msg_id: i64) !std.json.ObjectMap {
var i: usize = 0;
while (try ctx.getSentMessage(i)) |msg| : (i += 1) {
const obj = switch (msg) {
@@ -231,9 +630,14 @@ fn evaluatedObjectId(ctx: *testing.TestContext, msg_id: i64) ![]const u8 {
continue;
}
const result = obj.get("result") orelse return error.NoResult;
- const remote = result.object.get("result") orelse return error.NoRemoteObject;
- const object_id = remote.object.get("objectId") orelse return error.NoObjectId;
- return object_id.string;
+ return result.object;
}
return error.MessageNotFound;
}
+
+// The result.result.objectId of the Runtime.evaluate response to `msg_id`.
+fn evaluatedObjectId(ctx: *testing.TestContext, msg_id: i64) ![]const u8 {
+ const remote = (try sentResult(ctx, msg_id)).get("result") orelse return error.NoRemoteObject;
+ const object_id = remote.object.get("objectId") orelse return error.NoObjectId;
+ return object_id.string;
+}