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
//! Locator actions for element interaction.
use tracing::{debug, instrument};
use viewpoint_cdp::protocol::input::DispatchKeyEventParams;
use super::Locator;
use super::builders::{
CheckBuilder, ClickBuilder, DblclickBuilder, FillBuilder, HoverBuilder, PressBuilder,
TapBuilder, TypeBuilder,
};
use crate::error::LocatorError;
impl<'a> Locator<'a> {
/// Click the element.
///
/// Returns a builder that can be configured with additional options, or awaited
/// directly for a simple click.
///
/// # Examples
///
/// ```no_run
/// use viewpoint_core::Page;
/// use viewpoint_cdp::protocol::input::{MouseButton, modifiers};
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple click - await directly
/// page.locator("button").click().await?;
///
/// // Click with options
/// page.locator("button").click()
/// .position(10.0, 5.0)
/// .button(MouseButton::Right)
/// .modifiers(modifiers::SHIFT)
/// .send().await?;
///
/// // Force click without waiting for actionability
/// page.locator("button").click().force(true).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Options
///
/// - [`ClickBuilder::position`] - Click at offset from element's top-left corner
/// - [`ClickBuilder::button`] - Use a different mouse button (right, middle)
/// - [`ClickBuilder::modifiers`] - Hold modifier keys (Shift, Ctrl, Alt)
/// - [`ClickBuilder::force`] - Skip actionability checks
pub fn click(&self) -> ClickBuilder<'_, 'a> {
ClickBuilder::new(self)
}
/// Double-click the element.
///
/// Returns a builder that can be configured with additional options, or awaited
/// directly for a simple double-click.
///
/// # Examples
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple double-click - await directly
/// page.locator("button").dblclick().await?;
///
/// // Double-click with options
/// page.locator("button").dblclick()
/// .position(10.0, 5.0)
/// .no_wait_after(true)
/// .send().await?;
/// # Ok(())
/// # }
/// ```
pub fn dblclick(&self) -> DblclickBuilder<'_, 'a> {
DblclickBuilder::new(self)
}
/// Fill the element with text (clears existing content first).
///
/// This is for input and textarea elements. Returns a builder that can be
/// configured with additional options, or awaited directly.
///
/// # Examples
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple fill - await directly
/// page.locator("input").fill("hello").await?;
///
/// // Fill without waiting for navigation
/// page.locator("input").fill("hello").no_wait_after(true).await?;
/// # Ok(())
/// # }
/// ```
pub fn fill(&self, text: &str) -> FillBuilder<'_, 'a> {
FillBuilder::new(self, text)
}
/// Type text character by character.
///
/// Unlike `fill`, this types each character with keydown/keyup events.
/// Returns a builder that can be configured with additional options, or awaited
/// directly for simple typing.
///
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple type - await directly
/// page.locator("input").type_text("hello").await?;
///
/// // Type with delay between characters
/// page.locator("input").type_text("hello")
/// .delay(Duration::from_millis(100))
/// .send().await?;
/// # Ok(())
/// # }
/// ```
///
/// # Options
///
/// - [`TypeBuilder::delay`] - Add delay between character keystrokes
pub fn type_text(&self, text: &str) -> TypeBuilder<'_, 'a> {
TypeBuilder::new(self, text)
}
/// Press a key or key combination.
///
/// Examples: "Enter", "Backspace", "Control+a", "Shift+Tab"
///
/// Returns a builder that can be configured with additional options, or awaited
/// directly for a simple key press.
///
/// # Examples
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple press - await directly
/// page.locator("input").press("Enter").await?;
///
/// // Press without waiting for navigation (e.g., form submission)
/// page.locator("input").press("Enter").no_wait_after(true).await?;
/// # Ok(())
/// # }
/// ```
pub fn press(&self, key: &str) -> PressBuilder<'_, 'a> {
PressBuilder::new(self, key)
}
/// Hover over the element.
///
/// Returns a builder that can be configured with additional options, or awaited
/// directly for a simple hover.
///
/// # Examples
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple hover - await directly
/// page.locator("button").hover().await?;
///
/// // Hover with position offset
/// page.locator("button").hover()
/// .position(10.0, 5.0)
/// .send().await?;
///
/// // Force hover without waiting for actionability
/// page.locator("button").hover().force(true).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Options
///
/// - [`HoverBuilder::position`] - Hover at offset from element's top-left corner
/// - [`HoverBuilder::modifiers`] - Hold modifier keys during hover
/// - [`HoverBuilder::force`] - Skip actionability checks
pub fn hover(&self) -> HoverBuilder<'_, 'a> {
HoverBuilder::new(self)
}
/// Focus the element.
///
/// # Errors
///
/// Returns an error if the element cannot be found or focused.
#[instrument(level = "debug", skip(self), fields(selector = ?self.selector))]
pub async fn focus(&self) -> Result<(), LocatorError> {
self.wait_for_actionable().await?;
debug!("Focusing element");
self.focus_element().await?;
Ok(())
}
/// Clear the element's content.
///
/// # Errors
///
/// Returns an error if the element cannot be cleared.
#[instrument(level = "debug", skip(self), fields(selector = ?self.selector))]
pub async fn clear(&self) -> Result<(), LocatorError> {
self.wait_for_actionable().await?;
debug!("Clearing element");
// Focus and select all, then delete
self.focus_element().await?;
let mut select_all = DispatchKeyEventParams::key_down("a");
select_all.modifiers = Some(viewpoint_cdp::protocol::input::modifiers::CTRL);
self.dispatch_key_event(select_all).await?;
self.dispatch_key_event(DispatchKeyEventParams::key_down("Backspace"))
.await?;
Ok(())
}
/// Check a checkbox or radio button.
///
/// Returns a builder that can be configured with additional options, or awaited
/// directly for a simple check operation.
///
/// # Examples
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple check - await directly
/// page.locator("input[type=checkbox]").check().await?;
///
/// // Check without waiting for navigation
/// page.locator("input[type=checkbox]").check().no_wait_after(true).await?;
/// # Ok(())
/// # }
/// ```
pub fn check(&self) -> CheckBuilder<'_, 'a> {
CheckBuilder::new_check(self)
}
/// Uncheck a checkbox.
///
/// Returns a builder that can be configured with additional options, or awaited
/// directly for a simple uncheck operation.
///
/// # Examples
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple uncheck - await directly
/// page.locator("input[type=checkbox]").uncheck().await?;
///
/// // Uncheck without waiting for navigation
/// page.locator("input[type=checkbox]").uncheck().no_wait_after(true).await?;
/// # Ok(())
/// # }
/// ```
pub fn uncheck(&self) -> CheckBuilder<'_, 'a> {
CheckBuilder::new_uncheck(self)
}
/// Tap on the element (touch event).
///
/// Requires touch to be enabled via `page.touchscreen().enable()`.
///
/// Returns a builder to configure tap options.
///
/// # Example
///
/// ```no_run
/// use viewpoint_core::Page;
/// use viewpoint_cdp::protocol::input::modifiers;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Simple tap
/// page.locator("button").tap().send().await?;
///
/// // Tap with position offset
/// page.locator("button").tap().position(10.0, 5.0).send().await?;
///
/// // Tap with modifiers
/// page.locator("button").tap().modifiers(modifiers::SHIFT).send().await?;
///
/// // Force tap without waiting for actionability
/// page.locator("button").tap().force(true).send().await?;
/// # Ok(())
/// # }
/// ```
pub fn tap(&self) -> TapBuilder<'_, 'a> {
TapBuilder::new(self)
}
/// Drag this element to another locator.
///
/// # Arguments
///
/// * `target` - The target locator to drag to.
///
/// # Example
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// let source = page.locator("#draggable");
/// let target = page.locator("#droppable");
/// source.drag_to(&target).await?;
/// # Ok(())
/// # }
/// ```
#[instrument(level = "debug", skip(self, target), fields(selector = ?self.selector))]
pub async fn drag_to(&self, target: &Locator<'_>) -> Result<(), LocatorError> {
self.drag_to_with_options(target, None, None, 1).await
}
/// Drag this element to another locator with options.
///
/// # Arguments
///
/// * `target` - The target locator to drag to.
/// * `source_position` - Optional offset from source element's top-left corner.
/// * `target_position` - Optional offset from target element's top-left corner.
/// * `steps` - Number of intermediate steps for smooth dragging.
#[instrument(level = "debug", skip(self, target))]
pub async fn drag_to_with_options(
&self,
target: &Locator<'_>,
source_position: Option<(f64, f64)>,
target_position: Option<(f64, f64)>,
steps: u32,
) -> Result<(), LocatorError> {
// Get source element info
let source_info = self.wait_for_actionable().await?;
let (source_x, source_y) = if let Some((ox, oy)) = source_position {
(
source_info.x.expect("x") + ox,
source_info.y.expect("y") + oy,
)
} else {
(
source_info.x.expect("x") + source_info.width.expect("width") / 2.0,
source_info.y.expect("y") + source_info.height.expect("height") / 2.0,
)
};
// Get target element info
let target_info = target.wait_for_actionable().await?;
let (target_x, target_y) = if let Some((ox, oy)) = target_position {
(
target_info.x.expect("x") + ox,
target_info.y.expect("y") + oy,
)
} else {
(
target_info.x.expect("x") + target_info.width.expect("width") / 2.0,
target_info.y.expect("y") + target_info.height.expect("height") / 2.0,
)
};
debug!(
"Dragging from ({}, {}) to ({}, {})",
source_x, source_y, target_x, target_y
);
// Perform drag operation
self.page.mouse().move_(source_x, source_y).send().await?;
self.page.mouse().down().send().await?;
self.page
.mouse()
.move_(target_x, target_y)
.steps(steps)
.send()
.await?;
self.page.mouse().up().send().await?;
Ok(())
}
/// Take a screenshot of this element.
///
/// Returns a builder to configure screenshot options.
///
/// # Example
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
/// // Capture element screenshot
/// let bytes = page.locator("button").screenshot().capture().await?;
///
/// // Capture and save to file
/// page.locator("button")
/// .screenshot()
/// .path("button.png")
/// .capture()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn screenshot(&self) -> crate::page::screenshot_element::ElementScreenshotBuilder<'_, '_> {
crate::page::screenshot_element::ElementScreenshotBuilder::new(self)
}
}