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///
13/// Both fields are accepted for parity with the other bindings and are
14/// currently ignored: the driver this crate bundles has no accessibility
15/// snapshot command, so [`Accessibility::snapshot`] is emulated with a
16/// whole-page ARIA snapshot. Use [`Locator::aria_snapshot`] to scope a
17/// snapshot to an element.
18///
19/// [`Locator::aria_snapshot`]: crate::protocol::Locator::aria_snapshot
20#[derive(Debug, Default, Clone)]
21#[non_exhaustive]
22pub struct AccessibilitySnapshotOptions {
23    /// Whether to prune uninteresting nodes from the tree.
24    ///
25    /// Defaults to `true`.
26    pub interesting_only: Option<bool>,
27
28    /// The root element for the snapshot.
29    ///
30    /// When not set, the snapshot is taken from the entire page.
31    pub root: Option<crate::protocol::ElementHandle>,
32}
33
34/// Provides accessibility-tree inspection methods on a page.
35///
36/// Access via [`Page::accessibility`].
37///
38/// See: <https://playwright.dev/docs/api/class-accessibility>
39#[derive(Clone)]
40pub struct Accessibility {
41    page: Page,
42}
43
44impl Accessibility {
45    pub(crate) fn new(page: Page) -> Self {
46        Self { page }
47    }
48
49    /// Captures the current state of the page's accessibility tree.
50    ///
51    /// Returns the accessibility tree as a JSON `Value` (tree of nodes with
52    /// `role`, `name`, `value`, `children`, etc.), or `null` when there is no
53    /// accessibility tree.
54    ///
55    /// # Errors
56    ///
57    /// Returns error if the RPC call fails or the browser has been closed.
58    ///
59    /// See: <https://playwright.dev/docs/api/class-accessibility#accessibility-snapshot>
60    pub async fn snapshot(
61        &self,
62        options: impl Into<Option<AccessibilitySnapshotOptions>>,
63    ) -> Result<Value> {
64        let options = options.into();
65        self.page.accessibility_snapshot(options).await
66    }
67}