viewpoint-core 0.4.3

High-level browser automation API for Viewpoint
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#![cfg(feature = "integration")]

//! ARIA snapshot iframe ref tests.
//!
//! These tests verify ref behavior with iframes and aria_snapshot_with_frames().

mod common;

use viewpoint_core::{AriaSnapshot, Browser};

/// Test that element refs from iframes are resolvable after aria_snapshot_with_frames().
///
/// This verifies that the fix for iframe ref resolution works - child frame refs
/// are now stored in Page's ref_map and can be resolved via page.locator_from_ref().
#[tokio::test]
async fn test_iframe_refs_resolvable_via_aria_snapshot_with_frames() {
    common::init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let page = context.new_page().await.expect("Failed to create page");

    page.set_content(
        r#"
        <html><body>
            <h1>Main Page</h1>
            <iframe name="widget" srcdoc="<html><body><button id='iframe-btn'>Iframe Button</button></body></html>"></iframe>
        </body></html>
    "#,
    )
    .set()
    .await
    .expect("Failed to set content");

    // Wait for iframe to load
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    // Capture snapshot with frames - this should now store iframe refs in Page's ref_map
    let snapshot = page
        .aria_snapshot_with_frames()
        .await
        .expect("Failed to get multi-frame snapshot");

    let yaml = snapshot.to_yaml();
    println!("Multi-frame snapshot:\n{yaml}");

    // Find the button ref from the iframe
    let button_ref =
        common::find_button_ref(&snapshot).expect("Should find button ref from iframe");
    println!("Found iframe button ref: {button_ref}");

    // Verify the ref contains a frame index (f1 for first child frame)
    assert!(
        button_ref.contains("f1"),
        "Iframe button ref should have frame index f1, got: {button_ref}"
    );

    // The iframe button ref should now be resolvable via page.element_from_ref()
    let handle = page
        .element_from_ref(&button_ref)
        .await
        .expect("Iframe refs should now be resolvable via aria_snapshot_with_frames()");

    assert!(
        handle.is_attached().await.unwrap(),
        "Resolved iframe element should be attached"
    );

    // Clean up
    browser.close().await.expect("Failed to close browser");
}

/// Test clicking an element inside an iframe after aria_snapshot_with_frames().
///
/// This test verifies that we can click an iframe button using its ref.
/// The button updates its own text content to prove the click occurred.
#[tokio::test]
async fn test_click_iframe_element_via_ref() {
    common::init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let page = context.new_page().await.expect("Failed to create page");

    // The button updates its own text content when clicked
    page.set_content(
        r#"
        <html><body>
            <h1>Main Page</h1>
            <iframe name="widget" srcdoc="<html><body><button onclick='this.innerText = &quot;Clicked!&quot;'>Click Me</button></body></html>"></iframe>
        </body></html>
    "#,
    )
    .set()
    .await
    .expect("Failed to set content");

    // Wait for iframe to load
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    // Capture snapshot with frames
    let snapshot = page
        .aria_snapshot_with_frames()
        .await
        .expect("Failed to get multi-frame snapshot");

    // Find the button ref
    let button_ref = common::find_button_ref(&snapshot).expect("Should find button ref");
    println!("Clicking button with ref: {button_ref}");

    // Click the button via locator_from_ref
    page.locator_from_ref(&button_ref)
        .click()
        .await
        .expect("Should be able to click iframe button via ref");

    // Wait for click to process
    tokio::time::sleep(std::time::Duration::from_millis(100)).await;

    // Verify the click worked by getting the button's text content (it should now say "Clicked!")
    let button_text = page
        .locator_from_ref(&button_ref)
        .text_content()
        .await
        .expect("Should get button text");

    assert_eq!(
        button_text,
        Some("Clicked!".to_string()),
        "Button click should have updated button text"
    );

    // Clean up
    browser.close().await.expect("Failed to close browser");
}

/// Test typing into an input inside an iframe after aria_snapshot_with_frames().
#[tokio::test]
async fn test_type_into_iframe_input_via_ref() {
    common::init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let page = context.new_page().await.expect("Failed to create page");

    page.set_content(
        r#"
        <html><body>
            <h1>Main Page</h1>
            <iframe name="form-frame" srcdoc="<html><body><label for='name'>Name:</label><input type='text' id='name' placeholder='Enter name'></body></html>"></iframe>
        </body></html>
    "#,
    )
    .set()
    .await
    .expect("Failed to set content");

    // Wait for iframe to load
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    // Capture snapshot with frames
    let snapshot = page
        .aria_snapshot_with_frames()
        .await
        .expect("Failed to get multi-frame snapshot");

    let yaml = snapshot.to_yaml();
    println!("Snapshot:\n{yaml}");

    // Find the textbox ref
    let input_ref = common::find_textbox_ref(&snapshot).expect("Should find textbox ref");
    println!("Filling input with ref: {input_ref}");

    // Fill the input via locator_from_ref
    page.locator_from_ref(&input_ref)
        .fill("Test User")
        .await
        .expect("Should be able to fill iframe input via ref");

    // Verify the input was filled by getting its value
    let value = page
        .locator_from_ref(&input_ref)
        .input_value()
        .await
        .expect("Should get input value");

    assert_eq!(value, "Test User", "Input should contain typed text");

    // Clean up
    browser.close().await.expect("Failed to close browser");
}

/// Test that refs from nested iframes are resolvable.
#[tokio::test]
async fn test_nested_iframe_refs_resolvable() {
    common::init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let page = context.new_page().await.expect("Failed to create page");

    // Create a page with nested iframes using srcdoc
    page.set_content(
        r#"
        <html><body>
            <h1>Level 0 Main</h1>
            <button id="btn0">Main Button</button>
            <iframe name="level1" srcdoc="<html><body><h2>Level 1</h2><button id='btn1'>Level 1 Button</button><iframe name='level2' srcdoc='<html><body><h3>Level 2</h3><button id=&quot;btn2&quot;>Level 2 Button</button></body></html>'></iframe></body></html>"></iframe>
        </body></html>
    "#,
    )
    .set()
    .await
    .expect("Failed to set content");

    // Wait for nested iframes to load
    tokio::time::sleep(std::time::Duration::from_millis(500)).await;

    // Capture snapshot with frames
    let snapshot = page
        .aria_snapshot_with_frames()
        .await
        .expect("Failed to get multi-frame snapshot");

    let yaml = snapshot.to_yaml();
    println!("Nested frames snapshot:\n{yaml}");

    // Collect all button refs
    let mut button_refs: Vec<String> = Vec::new();
    common::collect_refs_by_role(&snapshot, "button", &mut button_refs);

    println!("Found button refs: {:?}", button_refs);

    // Should have at least 2 buttons (main and one from iframes)
    assert!(
        button_refs.len() >= 2,
        "Should have at least 2 button refs, got {}",
        button_refs.len()
    );

    // Verify all button refs are resolvable
    for button_ref in &button_refs {
        let handle = page
            .element_from_ref(button_ref)
            .await
            .expect(&format!("Button ref {} should be resolvable", button_ref));
        assert!(
            handle.is_attached().await.unwrap(),
            "Resolved button should be attached"
        );
    }

    // Clean up
    browser.close().await.expect("Failed to close browser");
}

/// Test that refs from different frames have distinct frame indices.
#[tokio::test]
async fn test_iframe_refs_have_correct_frame_indices() {
    common::init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let page = context.new_page().await.expect("Failed to create page");

    page.set_content(
        r#"
        <html><body>
            <h1>Main Page</h1>
            <button id="main-btn">Main Button</button>
            <iframe name="frame1" srcdoc="<html><body><button id='btn1'>Frame1 Button</button></body></html>"></iframe>
            <iframe name="frame2" srcdoc="<html><body><button id='btn2'>Frame2 Button</button></body></html>"></iframe>
        </body></html>
    "#,
    )
    .set()
    .await
    .expect("Failed to set content");

    // Wait for iframes to load
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    // Capture snapshot with frames
    let snapshot = page
        .aria_snapshot_with_frames()
        .await
        .expect("Failed to get multi-frame snapshot");

    let yaml = snapshot.to_yaml();
    println!("Multi-frame snapshot:\n{yaml}");

    // Collect all button refs with their names
    fn collect_button_info(snapshot: &AriaSnapshot, info: &mut Vec<(String, String)>) {
        if snapshot.role.as_deref() == Some("button") {
            if let Some(ref r) = snapshot.node_ref {
                let name = snapshot.name.clone().unwrap_or_default();
                info.push((name, r.clone()));
            }
        }
        for child in &snapshot.children {
            collect_button_info(child, info);
        }
    }

    let mut button_info: Vec<(String, String)> = Vec::new();
    collect_button_info(&snapshot, &mut button_info);

    println!("Button info: {:?}", button_info);

    // Extract frame indices from refs (format: c{ctx}p{page}f{frame}e{element})
    fn extract_frame_index(ref_str: &str) -> Option<usize> {
        // Parse the ref format: c0p0f1e5
        let f_pos = ref_str.find('f')?;
        let e_pos = ref_str.find('e')?;
        if f_pos < e_pos {
            ref_str[f_pos + 1..e_pos].parse().ok()
        } else {
            None
        }
    }

    // Find main button (should have f0)
    let main_btn = button_info
        .iter()
        .find(|(name, _)| name.contains("Main"))
        .expect("Should find main button");
    let main_frame_idx =
        extract_frame_index(&main_btn.1).expect("Main button should have frame index");
    assert_eq!(
        main_frame_idx, 0,
        "Main frame button should have frame index 0"
    );

    // Find frame1 button (should have f1 or f2)
    let frame1_btn = button_info
        .iter()
        .find(|(name, _)| name.contains("Frame1"))
        .expect("Should find frame1 button");
    let frame1_idx =
        extract_frame_index(&frame1_btn.1).expect("Frame1 button should have frame index");
    assert!(
        frame1_idx > 0,
        "Frame1 button should have frame index > 0, got {}",
        frame1_idx
    );

    // Find frame2 button (should have different index from frame1)
    let frame2_btn = button_info
        .iter()
        .find(|(name, _)| name.contains("Frame2"))
        .expect("Should find frame2 button");
    let frame2_idx =
        extract_frame_index(&frame2_btn.1).expect("Frame2 button should have frame index");
    assert!(
        frame2_idx > 0,
        "Frame2 button should have frame index > 0, got {}",
        frame2_idx
    );

    // Frame indices should be different for different frames
    assert_ne!(
        frame1_idx, frame2_idx,
        "Frame1 and Frame2 should have different frame indices"
    );

    // All refs should be resolvable
    for (name, ref_str) in &button_info {
        let handle = page.element_from_ref(ref_str).await.expect(&format!(
            "Button '{}' ref {} should be resolvable",
            name, ref_str
        ));
        assert!(
            handle.is_attached().await.unwrap(),
            "Button '{}' should be attached",
            name
        );
    }

    // Clean up
    browser.close().await.expect("Failed to close browser");
}