Skip to main content

allwright/
client_tab_accessibility.rs

1use super::command::command_retry_options;
2use super::tab::ensure_tab_open;
3use super::types::{AccessibilitySnapshotFormat, AccessibilitySnapshotOptions, Error, Result, Tab};
4use crate::proto::context_session_command::Command;
5use crate::proto::context_session_event::Event;
6use crate::proto::{AccessibilitySnapshotCommand, ContextSessionCommand};
7
8impl Tab {
9    pub async fn accessibility_snapshot(&self) -> Result<String> {
10        self.accessibility_snapshot_with_options(AccessibilitySnapshotOptions::default())
11            .await
12    }
13
14    pub async fn accessibility_snapshot_with_options(
15        &self,
16        options: AccessibilitySnapshotOptions,
17    ) -> Result<String> {
18        let mut state = self.inner.state.lock().await;
19        let handle = self.ensure_handle(&mut state).await?;
20        ensure_tab_open(handle, &self.inner.session_id)?;
21        handle
22            .command_tx
23            .send(ContextSessionCommand {
24                surface_session_id: self.inner.surface_session_id.clone(),
25                context_session_id: self.inner.session_id.clone(),
26                command: Some(Command::AccessibilitySnapshot(
27                    AccessibilitySnapshotCommand {
28                        format: match options.format {
29                            AccessibilitySnapshotFormat::Json => "json",
30                            AccessibilitySnapshotFormat::Yaml => "yaml",
31                        }
32                        .into(),
33                        retry_options: command_retry_options(options.timeout_ms),
34                    },
35                )),
36            })
37            .await
38            .map_err(|_| Error::new("failed to send AccessibilitySnapshotCommand"))?;
39        loop {
40            let event = handle.events.message().await?.ok_or_else(|| {
41                Error::new("page session closed while capturing accessibility snapshot")
42            })?;
43            match event.event {
44                Some(Event::AccessibilitySnapshotCaptured(result)) => return Ok(result.snapshot),
45                Some(Event::Error(error)) => return Err(Error::new(error.message)),
46                Some(Event::Closed(_)) => {
47                    handle.closed = true;
48                    return Err(Error::new(
49                        "page session closed while capturing accessibility snapshot",
50                    ));
51                }
52                _ => {}
53            }
54        }
55    }
56}