playwright/api/
accessibility.rs

1pub use crate::imp::page::{AccessibilitySnapshotResponse as SnapshotResponse, Mixed, Val};
2use crate::{
3    api::ElementHandle,
4    imp::{
5        core::*,
6        page::{AccessibilitySnapshotArgs as SnapshotArgs, Page as PageImpl},
7        prelude::*
8    }
9};
10
11/// The Accessibility class provides methods for inspecting Chromium's accessibility tree. The accessibility tree is used by
12/// assistive technology such as [screen readers](https://en.wikipedia.org/wiki/Screen_reader) or
13/// [switches](https://en.wikipedia.org/wiki/Switch_access).
14///
15/// Accessibility is a very platform-specific thing. On different platforms, there are different screen readers that might
16/// have wildly different output.
17///
18/// Rendering engines of Chromium, Firefox and WebKit have a concept of "accessibility tree", which is then translated into
19/// different platform-specific APIs. Accessibility namespace gives access to this Accessibility Tree.
20///
21/// Most of the accessibility tree gets filtered out when converting from internal browser AX Tree to Platform-specific
22/// AX-Tree or by assistive technologies themselves. By default, Playwright tries to approximate this filtering, exposing
23/// only the "interesting" nodes of the tree.
24#[derive(Debug, Clone)]
25pub struct Accessibility {
26    inner: Weak<PageImpl>
27}
28
29impl Accessibility {
30    pub(crate) fn new(inner: Weak<PageImpl>) -> Self { Self { inner } }
31
32    /// Captures the current state of the accessibility tree. The returned object represents the root accessible node of the
33    /// page.
34    ///
35    /// > NOTE: The Chromium accessibility tree contains nodes that go unused on most platforms and by most screen readers.
36    /// Playwright will discard them as well for an easier to process tree, unless `interestingOnly` is set to `false`.
37    ///
38    /// An example of dumping the entire accessibility tree:
39    ///
40    /// ```js
41    /// const snapshot = await page.accessibility.snapshot();
42    /// console.log(snapshot);
43    /// ```
44    ///
45    /// An example of logging the focused node's name:
46    ///
47    /// ```js
48    /// const snapshot = await page.accessibility.snapshot();
49    /// const node = findFocusedNode(snapshot);
50    /// console.log(node && node.name);
51    ///
52    /// function findFocusedNode(node) {
53    ///  if (node.focused)
54    ///    return node;
55    ///  for (const child of node.children || []) {
56    ///    const foundNode = findFocusedNode(child);
57    ///    return foundNode;
58    ///  }
59    ///  return null;
60    /// }
61    /// var accessibilitySnapshot = await Page.Accessibility.SnapshotAsync();
62    /// var focusedNode = findFocusedNode(accessibilitySnapshot);
63    /// if(focusedNode != null)
64    ///  Console.WriteLine(focusedNode.Name);
65    /// ```
66    pub fn snapshot_builder(&self) -> SnapshotBuilder { SnapshotBuilder::new(self.inner.clone()) }
67}
68
69pub struct SnapshotBuilder {
70    inner: Weak<PageImpl>,
71    args: SnapshotArgs
72}
73
74impl SnapshotBuilder {
75    fn new(inner: Weak<PageImpl>) -> Self {
76        let args = SnapshotArgs::default();
77        Self { inner, args }
78    }
79
80    pub async fn snapshot(self) -> ArcResult<Option<SnapshotResponse>> {
81        let Self { inner, args } = self;
82        upgrade(&inner)?.accessibility_snapshot(args).await
83    }
84
85    /// The root DOM element for the snapshot. Defaults to the whole page.
86    pub fn try_root(mut self, x: ElementHandle) -> Result<Self, Error> {
87        let guid = x.guid()?;
88        self.args.root = Some(OnlyGuid { guid });
89        Ok(self)
90    }
91
92    setter!(
93        /// Prune uninteresting nodes from the tree. Defaults to `true`.
94        interesting_only: Option<bool>
95    );
96
97    pub fn clear_root(mut self) -> Self {
98        self.args.root = None;
99        self
100    }
101}