snapshot method

Future<AXNode> snapshot({
  1. bool? interestingOnly,
  2. ElementHandle? root,
})

Captures the current state of the accessibility tree. The returned object represents the root accessible node of the page.

NOTE The Chromium accessibility tree contains nodes that go unused on most platforms and by most screen readers. Puppeteer will discard them as well for an easier to process tree, unless interestingOnly is set to false.

An example of dumping the entire accessibility tree:

var snapshot = await page.accessibility.snapshot();
print(snapshot);

An example of logging the focused node's name:

AXNode? findFocusedNode(AXNode node) {
  if (node.focused) return node;
  for (var child in node.children) {
    var foundNode = findFocusedNode(child);
    return foundNode;
  }
  return null;
}

var snapshot = await page.accessibility.snapshot();
var node = findFocusedNode(snapshot);
print(node?.name);

Parameters:

  • interestingOnly Prune uninteresting nodes from the tree. Defaults to true.
  • root The root DOM element for the snapshot. Defaults to the whole page.

Implementation

Future<AXNode> snapshot({bool? interestingOnly, ElementHandle? root}) async {
  interestingOnly ??= true;
  var nodes = await _devTools.accessibility.getFullAXTree();
  BackendNodeId? backendNodeId;
  if (root != null) {
    var node = await _devTools.dom
        .describeNode(objectId: root.remoteObject.objectId);
    backendNodeId = node.backendNodeId;
  }
  var defaultRoot = _AXNode.createTree(nodes);
  _AXNode? needle = defaultRoot;
  if (backendNodeId != null) {
    needle = defaultRoot
        .find((node) => node._payload.backendDOMNodeId == backendNodeId);
    if (needle == null) return AXNode.empty;
  }
  if (!interestingOnly) return _serializeTree(needle)[0];

  var interestingNodes = <_AXNode>{};
  _collectInterestingNodes(interestingNodes, defaultRoot,
      insideControl: false);
  if (!interestingNodes.contains(needle)) return AXNode.empty;
  return _serializeTree(needle, whitelistedNodes: interestingNodes)[0];
}