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
//! Frame locator for interacting with iframe content.
//!
//! `FrameLocator` provides a way to locate and interact with elements inside
//! iframes without needing to directly access Frame objects.

// Allow dead code for frame locator methods (spec: frames)

use std::time::Duration;

use super::locator::{AriaRole, LocatorOptions, Selector};
use crate::Page;
use viewpoint_js::js;
use viewpoint_js_core::escape_js_string_single;

/// Default timeout for frame locator operations.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// A locator for finding and interacting with iframe content.
///
/// `FrameLocator` represents a view into an iframe on the page. It provides
/// methods to locate elements within the iframe using the same patterns
/// as page-level locators.
///
/// # Example
///
/// ```
/// # #[cfg(feature = "integration")]
/// # tokio_test::block_on(async {
/// # use viewpoint_core::Browser;
/// use viewpoint_core::AriaRole;
/// # let browser = Browser::launch().headless(true).launch().await.unwrap();
/// # let context = browser.new_context().await.unwrap();
/// # let page = context.new_page().await.unwrap();
/// # page.goto("about:blank").goto().await.unwrap();
///
/// // Locate elements inside an iframe
/// page.frame_locator("#my-iframe")
///     .locator("button")
///     .click()
///     .await.ok();
///
/// // Use semantic locators inside frames
/// page.frame_locator("#payment-frame")
///     .get_by_role(AriaRole::Button)
///     .with_name("Submit")
///     .build()
///     .click()
///     .await.ok();
///
/// // Nested frames
/// page.frame_locator("#outer")
///     .frame_locator("#inner")
///     .locator("input")
///     .fill("text")
///     .await.ok();
/// # });
/// ```
#[derive(Debug, Clone)]
pub struct FrameLocator<'a> {
    /// Reference to the page.
    page: &'a Page,
    /// Selector for the iframe element.
    frame_selector: String,
    /// Parent frame locators (for nested frames).
    parent_selectors: Vec<String>,
    /// Timeout for operations.
    timeout: Duration,
}

impl<'a> FrameLocator<'a> {
    /// Create a new frame locator.
    pub(crate) fn new(page: &'a Page, selector: impl Into<String>) -> Self {
        Self {
            page,
            frame_selector: selector.into(),
            parent_selectors: Vec::new(),
            timeout: DEFAULT_TIMEOUT,
        }
    }

    /// Create a nested frame locator with parent context.
    fn with_parent(
        page: &'a Page,
        frame_selector: String,
        mut parent_selectors: Vec<String>,
        parent_selector: String,
    ) -> Self {
        parent_selectors.push(parent_selector);
        Self {
            page,
            frame_selector,
            parent_selectors,
            timeout: DEFAULT_TIMEOUT,
        }
    }

    /// Set a custom timeout for this frame locator.
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Get the page this frame locator belongs to.
    pub fn page(&self) -> &'a Page {
        self.page
    }

    /// Create a locator for elements within this frame.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use viewpoint_core::Page;
    ///
    /// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
    /// let button = page.frame_locator("#iframe").locator("button.submit");
    /// button.click().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn locator(&self, selector: impl Into<String>) -> FrameElementLocator<'a> {
        FrameElementLocator::new(self.clone(), Selector::Css(selector.into()))
    }

    /// Create a locator for elements containing the specified text within this frame.
    pub fn get_by_text(&self, text: impl Into<String>) -> FrameElementLocator<'a> {
        FrameElementLocator::new(
            self.clone(),
            Selector::Text {
                text: text.into(),
                exact: false,
            },
        )
    }

    /// Create a locator for elements with exact text content within this frame.
    pub fn get_by_text_exact(&self, text: impl Into<String>) -> FrameElementLocator<'a> {
        FrameElementLocator::new(
            self.clone(),
            Selector::Text {
                text: text.into(),
                exact: true,
            },
        )
    }

    /// Create a locator for elements with the specified ARIA role within this frame.
    pub fn get_by_role(&self, role: AriaRole) -> FrameRoleLocatorBuilder<'a> {
        FrameRoleLocatorBuilder::new(self.clone(), role)
    }

    /// Create a locator for elements with the specified test ID within this frame.
    pub fn get_by_test_id(&self, test_id: impl Into<String>) -> FrameElementLocator<'a> {
        FrameElementLocator::new(self.clone(), Selector::TestId(test_id.into()))
    }

    /// Create a locator for form controls by their associated label text within this frame.
    pub fn get_by_label(&self, label: impl Into<String>) -> FrameElementLocator<'a> {
        FrameElementLocator::new(self.clone(), Selector::Label(label.into()))
    }

    /// Create a locator for inputs by their placeholder text within this frame.
    pub fn get_by_placeholder(&self, placeholder: impl Into<String>) -> FrameElementLocator<'a> {
        FrameElementLocator::new(self.clone(), Selector::Placeholder(placeholder.into()))
    }

    /// Create a frame locator for a nested iframe within this frame.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use viewpoint_core::Page;
    ///
    /// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
    /// // Access element in nested frame
    /// page.frame_locator("#outer-frame")
    ///     .frame_locator("#inner-frame")
    ///     .locator("button")
    ///     .click()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn frame_locator(&self, selector: impl Into<String>) -> FrameLocator<'a> {
        FrameLocator::with_parent(
            self.page,
            selector.into(),
            self.parent_selectors.clone(),
            self.frame_selector.clone(),
        )
    }

    /// Get the frame selector.
    pub fn selector(&self) -> &str {
        &self.frame_selector
    }

    /// Get the parent selectors (for nested frames).
    pub fn parent_selectors(&self) -> &[String] {
        &self.parent_selectors
    }

    /// Build the JavaScript expression to access the frame's content document.
    ///
    /// Note: This function builds JavaScript dynamically at runtime because it processes
    /// a variable number of parent frame selectors. It uses `viewpoint_js_core` escaping
    /// utilities to ensure proper string escaping.
    pub(crate) fn to_js_frame_access(&self) -> String {
        let mut js = String::new();

        // Start from the top-level document
        js.push_str("(function() {\n");
        js.push_str("  let doc = document;\n");

        // Navigate through parent frames
        for parent_selector in &self.parent_selectors {
            let escaped_selector = escape_js_string_single(parent_selector);
            js.push_str("  const parent = doc.querySelector(");
            js.push_str(&escaped_selector);
            js.push_str(");\n");
            js.push_str("  if (!parent || !parent.contentDocument) return null;\n");
            js.push_str("  doc = parent.contentDocument;\n");
        }

        // Access the final frame
        let escaped_frame_selector = escape_js_string_single(&self.frame_selector);
        js.push_str("  const frame = doc.querySelector(");
        js.push_str(&escaped_frame_selector);
        js.push_str(");\n");
        js.push_str("  if (!frame || !frame.contentDocument) return null;\n");
        js.push_str("  return frame.contentDocument;\n");
        js.push_str("})()");
        js
    }
}

/// A locator for elements within a frame.
///
/// This combines a `FrameLocator` with an element `Selector` to locate
/// elements inside an iframe.
#[derive(Debug, Clone)]
pub struct FrameElementLocator<'a> {
    /// The frame locator.
    frame_locator: FrameLocator<'a>,
    /// The element selector within the frame.
    selector: Selector,
    /// Locator options.
    options: LocatorOptions,
}

impl<'a> FrameElementLocator<'a> {
    /// Create a new frame element locator.
    fn new(frame_locator: FrameLocator<'a>, selector: Selector) -> Self {
        Self {
            frame_locator,
            selector,
            options: LocatorOptions::default(),
        }
    }

    /// Set a custom timeout for this locator.
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.options.timeout = timeout;
        self
    }

    /// Create a child locator that further filters elements.
    #[must_use]
    pub fn locator(&self, selector: impl Into<String>) -> FrameElementLocator<'a> {
        FrameElementLocator {
            frame_locator: self.frame_locator.clone(),
            selector: Selector::Chained(
                Box::new(self.selector.clone()),
                Box::new(Selector::Css(selector.into())),
            ),
            options: self.options.clone(),
        }
    }

    /// Select the first matching element.
    #[must_use]
    pub fn first(&self) -> FrameElementLocator<'a> {
        FrameElementLocator {
            frame_locator: self.frame_locator.clone(),
            selector: Selector::Nth {
                base: Box::new(self.selector.clone()),
                index: 0,
            },
            options: self.options.clone(),
        }
    }

    /// Select the last matching element.
    #[must_use]
    pub fn last(&self) -> FrameElementLocator<'a> {
        FrameElementLocator {
            frame_locator: self.frame_locator.clone(),
            selector: Selector::Nth {
                base: Box::new(self.selector.clone()),
                index: -1,
            },
            options: self.options.clone(),
        }
    }

    /// Select the nth matching element (0-indexed).
    #[must_use]
    pub fn nth(&self, index: i32) -> FrameElementLocator<'a> {
        FrameElementLocator {
            frame_locator: self.frame_locator.clone(),
            selector: Selector::Nth {
                base: Box::new(self.selector.clone()),
                index,
            },
            options: self.options.clone(),
        }
    }

    /// Get the frame locator.
    pub fn frame_locator(&self) -> &FrameLocator<'a> {
        &self.frame_locator
    }

    /// Get the selector.
    pub fn selector(&self) -> &Selector {
        &self.selector
    }

    /// Get the locator options.
    pub(crate) fn options(&self) -> &LocatorOptions {
        &self.options
    }

    /// Build the JavaScript expression to query elements within the frame.
    fn to_js_expression(&self) -> String {
        let frame_access = self.frame_locator.to_js_frame_access();
        let element_selector = self.selector.to_js_expression();

        js! {
            (function() {
                const frameDoc = @{frame_access};
                if (!frameDoc) return { found: false, count: 0, error: "Frame not found or not accessible" };

                // Override document for the selector expression
                const originalDocument = document;
                try {
                    // Create a modified expression that uses frameDoc instead of document
                    const elements = (function() {
                        const document = frameDoc;
                        return Array.from(@{element_selector});
                    })();
                    return elements;
                } catch (e) {
                    return [];
                }
            })()
        }
    }
}

/// Builder for role-based frame locators.
#[derive(Debug)]
pub struct FrameRoleLocatorBuilder<'a> {
    frame_locator: FrameLocator<'a>,
    role: AriaRole,
    name: Option<String>,
}

impl<'a> FrameRoleLocatorBuilder<'a> {
    fn new(frame_locator: FrameLocator<'a>, role: AriaRole) -> Self {
        Self {
            frame_locator,
            role,
            name: None,
        }
    }

    /// Filter by accessible name.
    #[must_use]
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Build the locator.
    pub fn build(self) -> FrameElementLocator<'a> {
        FrameElementLocator::new(
            self.frame_locator,
            Selector::Role {
                role: self.role,
                name: self.name,
            },
        )
    }
}

impl<'a> From<FrameRoleLocatorBuilder<'a>> for FrameElementLocator<'a> {
    fn from(builder: FrameRoleLocatorBuilder<'a>) -> Self {
        builder.build()
    }
}