Skip to main content

playwright_rs/protocol/
accessibility.rs

1// Accessibility — accessibility tree snapshots
2//
3// See: https://playwright.dev/docs/api/class-accessibility
4
5use crate::error::Result;
6use crate::protocol::page::Page;
7use serde_json::Value;
8
9/// Options for `Accessibility::snapshot`.
10///
11/// See: <https://playwright.dev/docs/api/class-accessibility#accessibility-snapshot>
12#[derive(Debug, Default, Clone)]
13pub struct AccessibilitySnapshotOptions {
14    /// Whether to prune uninteresting nodes from the tree.
15    ///
16    /// Defaults to `true`.
17    pub interesting_only: Option<bool>,
18
19    /// The root element for the snapshot.
20    ///
21    /// When not set, the snapshot is taken from the entire page.
22    pub root: Option<crate::protocol::ElementHandle>,
23}
24
25/// Provides accessibility-tree inspection methods on a page.
26///
27/// Access via [`Page::accessibility`].
28///
29/// See: <https://playwright.dev/docs/api/class-accessibility>
30#[derive(Clone)]
31pub struct Accessibility {
32    page: Page,
33}
34
35impl Accessibility {
36    pub(crate) fn new(page: Page) -> Self {
37        Self { page }
38    }
39
40    /// Captures the current state of the page's accessibility tree.
41    ///
42    /// Returns the accessibility tree as a JSON `Value` (tree of nodes with
43    /// `role`, `name`, `value`, `children`, etc.), or `null` when there is no
44    /// accessibility tree.
45    ///
46    /// # Errors
47    ///
48    /// Returns error if the RPC call fails or the browser has been closed.
49    ///
50    /// See: <https://playwright.dev/docs/api/class-accessibility#accessibility-snapshot>
51    pub async fn snapshot(&self, options: Option<AccessibilitySnapshotOptions>) -> Result<Value> {
52        self.page.accessibility_snapshot(options).await
53    }
54}