1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! Frame ARIA accessibility snapshot operations.
use std::collections::HashMap;
use tracing::{debug, instrument, trace};
use viewpoint_cdp::protocol::dom::{BackendNodeId, DescribeNodeParams, DescribeNodeResult};
use viewpoint_cdp::protocol::runtime::EvaluateParams;
use viewpoint_js::js;
use super::Frame;
use crate::error::PageError;
use crate::page::aria_snapshot::apply_refs_to_snapshot;
use crate::page::locator::aria_js::aria_snapshot_with_refs_js;
impl Frame {
/// Capture an ARIA accessibility snapshot of this frame's document.
///
/// The snapshot represents the accessible structure of the frame's content
/// as it would be exposed to assistive technologies. This is useful for
/// accessibility testing and MCP (Model Context Protocol) integrations.
///
/// # Node References
///
/// The snapshot includes `node_ref` on each element (format: `e{backendNodeId}`).
/// These refs can be used with `Page::element_from_ref()` or `Page::locator_from_ref()`
/// to interact with elements discovered in the snapshot.
///
/// # Frame Boundaries
///
/// Any iframes within this frame are marked as frame boundaries in the snapshot
/// with `is_frame: true`. Their content is NOT traversed (for security reasons).
/// To capture multi-frame accessibility trees, use `Page::aria_snapshot_with_frames()`.
///
/// # Errors
///
/// Returns an error if:
/// - The frame is detached
/// - JavaScript evaluation fails
/// - The snapshot cannot be parsed
#[instrument(level = "debug", skip(self), fields(frame_id = %self.id))]
pub async fn aria_snapshot(&self) -> Result<crate::page::locator::AriaSnapshot, PageError> {
if self.is_detached() {
return Err(PageError::EvaluationFailed("Frame is detached".to_string()));
}
// Capture snapshot with element collection for ref resolution
self.capture_snapshot_with_refs().await
}
/// Internal method to capture a snapshot with refs resolved.
///
/// This uses a two-phase approach:
/// 1. JS traversal collects the snapshot and element references
/// 2. CDP calls resolve each element to its backendNodeId
#[instrument(level = "debug", skip(self), fields(frame_id = %self.id))]
pub(super) async fn capture_snapshot_with_refs(
&self,
) -> Result<crate::page::locator::AriaSnapshot, PageError> {
let snapshot_fn = aria_snapshot_with_refs_js();
// Evaluate the JS function to get snapshot and element array
// We return by value for the snapshot, but need remote objects for elements
let js_code = js! {
(function() {
const getSnapshotWithRefs = @{snapshot_fn};
return getSnapshotWithRefs(document.body);
})()
};
// Get the execution context ID for this frame's main world
let context_id = self.main_world_context_id();
trace!(context_id = ?context_id, "Using execution context for aria_snapshot()");
// First, evaluate to get the result as a RemoteObject (not by value)
// so we can access the elements array
let result: viewpoint_cdp::protocol::runtime::EvaluateResult = self
.connection
.send_command(
"Runtime.evaluate",
Some(EvaluateParams {
expression: js_code,
object_group: Some("viewpoint-snapshot".to_string()),
include_command_line_api: None,
silent: Some(true),
context_id,
return_by_value: Some(false), // Get RemoteObject, not value
await_promise: Some(false),
}),
Some(&self.session_id),
)
.await?;
if let Some(exception) = result.exception_details {
return Err(PageError::EvaluationFailed(exception.text));
}
let result_object_id = result.result.object_id.ok_or_else(|| {
PageError::EvaluationFailed("No object ID from snapshot evaluation".to_string())
})?;
// Get the snapshot property (by value)
let snapshot_value = self.get_property_value(&result_object_id, "snapshot").await?;
// Parse the snapshot
let mut snapshot: crate::page::locator::AriaSnapshot =
serde_json::from_value(snapshot_value).map_err(|e| {
PageError::EvaluationFailed(format!("Failed to parse aria snapshot: {e}"))
})?;
// Get the elements array as a RemoteObject
let elements_result = self.get_property_object(&result_object_id, "elements").await?;
if let Some(elements_object_id) = elements_result {
// Get the length of the elements array
let length_value = self
.get_property_value(&elements_object_id, "length")
.await?;
let element_count = length_value.as_u64().unwrap_or(0) as usize;
debug!(element_count = element_count, "Resolving element refs");
// Build a map of element index -> backendNodeId
let mut ref_map: HashMap<usize, BackendNodeId> = HashMap::new();
for i in 0..element_count {
// Get the element at index i
if let Ok(Some(element_object_id)) =
self.get_array_element(&elements_object_id, i).await
{
// Get the backendNodeId for this element
match self.describe_node(&element_object_id).await {
Ok(backend_node_id) => {
ref_map.insert(i, backend_node_id);
trace!(
index = i,
backend_node_id = backend_node_id,
"Resolved element ref"
);
}
Err(e) => {
debug!(index = i, error = %e, "Failed to get backendNodeId for element");
}
}
}
}
// Apply refs to the snapshot tree
apply_refs_to_snapshot(&mut snapshot, &ref_map);
// Release the elements array to free memory
let _ = self.release_object(&elements_object_id).await;
}
// Release the result object
let _ = self.release_object(&result_object_id).await;
Ok(snapshot)
}
/// Get a property value from a RemoteObject by name.
pub(super) async fn get_property_value(
&self,
object_id: &str,
property: &str,
) -> Result<serde_json::Value, PageError> {
#[derive(Debug, serde::Deserialize)]
struct CallResult {
result: viewpoint_cdp::protocol::runtime::RemoteObject,
}
let result: CallResult = self
.connection
.send_command(
"Runtime.callFunctionOn",
Some(serde_json::json!({
"objectId": object_id,
"functionDeclaration": format!("function() {{ return this.{}; }}", property),
"returnByValue": true
})),
Some(&self.session_id),
)
.await?;
Ok(result.result.value.unwrap_or(serde_json::Value::Null))
}
/// Get a property as a RemoteObject from a RemoteObject by name.
pub(super) async fn get_property_object(
&self,
object_id: &str,
property: &str,
) -> Result<Option<String>, PageError> {
#[derive(Debug, serde::Deserialize)]
struct CallResult {
result: viewpoint_cdp::protocol::runtime::RemoteObject,
}
let result: CallResult = self
.connection
.send_command(
"Runtime.callFunctionOn",
Some(serde_json::json!({
"objectId": object_id,
"functionDeclaration": format!("function() {{ return this.{}; }}", property),
"returnByValue": false
})),
Some(&self.session_id),
)
.await?;
Ok(result.result.object_id)
}
/// Get an element from an array by index.
pub(super) async fn get_array_element(
&self,
array_object_id: &str,
index: usize,
) -> Result<Option<String>, PageError> {
#[derive(Debug, serde::Deserialize)]
struct CallResult {
result: viewpoint_cdp::protocol::runtime::RemoteObject,
}
let result: CallResult = self
.connection
.send_command(
"Runtime.callFunctionOn",
Some(serde_json::json!({
"objectId": array_object_id,
"functionDeclaration": format!("function() {{ return this[{}]; }}", index),
"returnByValue": false
})),
Some(&self.session_id),
)
.await?;
Ok(result.result.object_id)
}
/// Get the backendNodeId for an element by its object ID.
pub(super) async fn describe_node(&self, object_id: &str) -> Result<BackendNodeId, PageError> {
let result: DescribeNodeResult = self
.connection
.send_command(
"DOM.describeNode",
Some(DescribeNodeParams {
node_id: None,
backend_node_id: None,
object_id: Some(object_id.to_string()),
depth: Some(0),
pierce: None,
}),
Some(&self.session_id),
)
.await?;
Ok(result.node.backend_node_id)
}
/// Release a RemoteObject by its object ID.
pub(super) async fn release_object(&self, object_id: &str) -> Result<(), PageError> {
let _: serde_json::Value = self
.connection
.send_command(
"Runtime.releaseObject",
Some(serde_json::json!({
"objectId": object_id
})),
Some(&self.session_id),
)
.await?;
Ok(())
}
}