class: Accessibility

优质
小牛编辑
137浏览
2023-12-01

The Accessibility class provides methods for inspecting Chromium's accessibility tree. The accessibility tree is used by assistive technology such as screen readers.

Accessibility is a very platform-specific thing. On different platforms, there are different screen readers that might have wildly different output.

Blink - Chrome's rendering engine - has a concept of "accessibility tree", which is than translated into different platform-specific APIs. Accessibility namespace gives users access to the Blink Accessibility Tree.

Most of the accessibility tree gets filtered out when converting from Blink AX Tree to Platform-specific AX-Tree or by screen readers themselves. By default, Puppeteer tries to approximate this filtering, exposing only the "interesting" nodes of the tree.

accessibility.snapshot([options])

  • options <Object>
    • interestingOnly <boolean> Prune uninteresting nodes from the tree. Defaults to true.
  • returns: <Promise<Object>> Returns an AXNode object with the following properties:
    • role <string> The role.
    • name <string> A human readable name for the node.
    • value <string|number> The current value of the node.
    • description <string> An additional human readable description of the node.
    • keyshortcuts <string> Keyboard shortcuts associated with this node.
    • roledescription <string> A human readable alternative to the role.
    • valuetext <string> A description of the current value.
    • disabled <boolean> Whether the node is disabled.
    • expanded <boolean> Whether the node is expanded or collapsed.
    • focused <boolean> Whether the node is focused.
    • modal <boolean> Whether the node is modal.
    • multiline <boolean> Whether the node text input supports multiline.
    • multiselectable <boolean> Whether more than one child can be selected.
    • readonly <boolean> Whether the node is read only.
    • required <boolean> Whether the node is required.
    • selected <boolean> Whether the node is selected in its parent node.
    • checked <boolean|"mixed"> Whether the checkbox is checked, or "mixed".
    • pressed <boolean|"mixed"> Whether the toggle button is checked, or "mixed".
    • level <number> The level of a heading.
    • valuemin <number> The minimum value in a node.
    • valuemax <number> The maximum value in a node.
    • autocomplete <string> What kind of autocomplete is supported by a control.
    • haspopup <string> What kind of popup is currently being shown for a node.
    • invalid <string> Whether and in what way this node's value is invalid.
    • orientation <string> Whether the node is oriented horizontally or vertically.
    • children <Array<Object>> Child AXNodes of this node, if any.

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:

const snapshot = await page.accessibility.snapshot();
console.log(snapshot);

An example of logging the focused node's name:

const snapshot = await page.accessibility.snapshot();
const node = findFocusedNode(snapshot);
console.log(node && node.name);

function findFocusedNode(node) {
  if (node.focused)
    return node;
  for (const child of node.children || []) {
    const foundNode = findFocusedNode(child);
    return foundNode;
  }
  return null;
}