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
34impl AccessibilitySnapshotOptions {
35 /// Whether to prune uninteresting nodes from the tree. Defaults to `true`.
36 /// Currently ignored; see the struct docs.
37 pub fn interesting_only(mut self, interesting_only: bool) -> Self {
38 self.interesting_only = Some(interesting_only);
39 self
40 }
41
42 /// The root element for the snapshot. Defaults to the entire page.
43 /// Currently ignored; see the struct docs.
44 pub fn root(mut self, root: crate::protocol::ElementHandle) -> Self {
45 self.root = Some(root);
46 self
47 }
48}
49
50/// Provides accessibility-tree inspection methods on a page.
51///
52/// Access via [`Page::accessibility`].
53///
54/// See: <https://playwright.dev/docs/api/class-accessibility>
55#[derive(Clone)]
56pub struct Accessibility {
57 page: Page,
58}
59
60impl Accessibility {
61 pub(crate) fn new(page: Page) -> Self {
62 Self { page }
63 }
64
65 /// Captures the current state of the page's accessibility tree.
66 ///
67 /// Returns the accessibility tree as a JSON `Value` (tree of nodes with
68 /// `role`, `name`, `value`, `children`, etc.), or `null` when there is no
69 /// accessibility tree.
70 ///
71 /// # Errors
72 ///
73 /// Returns error if the RPC call fails or the browser has been closed.
74 ///
75 /// See: <https://playwright.dev/docs/api/class-accessibility#accessibility-snapshot>
76 pub async fn snapshot(
77 &self,
78 options: impl Into<Option<AccessibilitySnapshotOptions>>,
79 ) -> Result<Value> {
80 let options = options.into();
81 self.page.accessibility_snapshot(options).await
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 #[test]
90 fn snapshot_setter_writes_the_field() {
91 let opts = AccessibilitySnapshotOptions::default().interesting_only(false);
92 assert_eq!(opts.interesting_only, Some(false));
93 assert!(opts.root.is_none());
94 }
95}