viewpoint_core/page/input_devices/mod.rs
1//! Input device access methods for Page.
2//!
3//! This module contains methods for accessing input device controllers
4//! (keyboard, mouse, touchscreen, clock) and performing drag-and-drop operations.
5
6use crate::page::clock::Clock;
7use crate::page::keyboard::Keyboard;
8use crate::page::mouse::Mouse;
9use crate::page::mouse_drag::DragAndDropBuilder;
10use crate::page::touchscreen::Touchscreen;
11use crate::page::Page;
12
13impl Page {
14 /// Get a reference to the keyboard controller.
15 ///
16 /// The keyboard controller provides methods for pressing keys, typing text,
17 /// and managing modifier key state.
18 ///
19 /// # Example
20 ///
21 /// ```ignore
22 /// // Press a key
23 /// page.keyboard().press("Enter").await?;
24 ///
25 /// // Type text
26 /// page.keyboard().type_text("Hello, World!").await?;
27 ///
28 /// // Use key combinations
29 /// page.keyboard().press("Control+a").await?;
30 ///
31 /// // Hold and release modifiers
32 /// page.keyboard().down("Shift").await?;
33 /// page.keyboard().press("a").await?; // Types 'A'
34 /// page.keyboard().up("Shift").await?;
35 /// ```
36 pub fn keyboard(&self) -> &Keyboard {
37 &self.keyboard
38 }
39
40 /// Get a reference to the mouse controller.
41 ///
42 /// The mouse controller provides methods for moving the mouse,
43 /// clicking, and scrolling.
44 ///
45 /// # Example
46 ///
47 /// ```ignore
48 /// // Move mouse
49 /// page.mouse().move_(100.0, 200.0).send().await?;
50 ///
51 /// // Click at coordinates
52 /// page.mouse().click(100.0, 200.0).send().await?;
53 ///
54 /// // Right-click
55 /// page.mouse().click(100.0, 200.0).button(MouseButton::Right).send().await?;
56 ///
57 /// // Scroll
58 /// page.mouse().wheel(0.0, 100.0).await?;
59 /// ```
60 pub fn mouse(&self) -> &Mouse {
61 &self.mouse
62 }
63
64 /// Get a reference to the touchscreen controller.
65 ///
66 /// The touchscreen controller provides methods for touch input simulation.
67 /// Requires `hasTouch: true` in browser context options.
68 ///
69 /// # Example
70 ///
71 /// ```ignore
72 /// page.touchscreen().tap(100.0, 200.0).await?;
73 /// ```
74 pub fn touchscreen(&self) -> &Touchscreen {
75 &self.touchscreen
76 }
77
78 /// Get a clock controller for this page.
79 ///
80 /// The clock controller allows you to mock time-related JavaScript functions
81 /// including Date, setTimeout, setInterval, requestAnimationFrame, and
82 /// `performance.now()`.
83 ///
84 /// # Example
85 ///
86 /// ```ignore
87 /// use std::time::Duration;
88 ///
89 /// // Install clock mocking
90 /// let mut clock = page.clock();
91 /// clock.install().await?;
92 ///
93 /// // Freeze time at a specific moment
94 /// clock.set_fixed_time("2024-01-01T00:00:00Z").await?;
95 ///
96 /// // Advance time and fire scheduled timers
97 /// clock.run_for(Duration::from_secs(5)).await?;
98 ///
99 /// // Cleanup
100 /// clock.uninstall().await?;
101 /// ```
102 pub fn clock(&self) -> Clock<'_> {
103 Clock::new(&self.connection, &self.session_id)
104 }
105
106 /// Drag from source to target element.
107 ///
108 /// Returns a builder for configuring drag options.
109 ///
110 /// # Example
111 ///
112 /// ```ignore
113 /// // Simple drag and drop
114 /// page.drag_and_drop("#source", "#target").await?;
115 ///
116 /// // With position options
117 /// page.drag_and_drop("#source", "#target")
118 /// .source_position(10.0, 10.0)
119 /// .target_position(5.0, 5.0)
120 /// .await?;
121 /// ```
122 pub fn drag_and_drop(
123 &self,
124 source: impl Into<String>,
125 target: impl Into<String>,
126 ) -> DragAndDropBuilder<'_> {
127 DragAndDropBuilder::new(self, source.into(), target.into())
128 }
129}