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 /// ```no_run
22 /// use viewpoint_core::Page;
23 ///
24 /// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
25 /// // Press a key
26 /// page.keyboard().press("Enter").await?;
27 ///
28 /// // Type text
29 /// page.keyboard().type_text("Hello, World!").await?;
30 ///
31 /// // Use key combinations
32 /// page.keyboard().press("Control+a").await?;
33 ///
34 /// // Hold and release modifiers
35 /// page.keyboard().down("Shift").await?;
36 /// page.keyboard().press("a").await?; // Types 'A'
37 /// page.keyboard().up("Shift").await?;
38 /// # Ok(())
39 /// # }
40 /// ```
41 pub fn keyboard(&self) -> &Keyboard {
42 &self.keyboard
43 }
44
45 /// Get a reference to the mouse controller.
46 ///
47 /// The mouse controller provides methods for moving the mouse,
48 /// clicking, and scrolling.
49 ///
50 /// # Example
51 ///
52 /// ```no_run
53 /// use viewpoint_core::Page;
54 /// use viewpoint_core::MouseButton;
55 ///
56 /// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
57 /// // Move mouse
58 /// page.mouse().move_(100.0, 200.0).send().await?;
59 ///
60 /// // Click at coordinates
61 /// page.mouse().click(100.0, 200.0).send().await?;
62 ///
63 /// // Right-click
64 /// page.mouse().click(100.0, 200.0).button(MouseButton::Right).send().await?;
65 ///
66 /// // Scroll
67 /// page.mouse().wheel(0.0, 100.0).await?;
68 /// # Ok(())
69 /// # }
70 /// ```
71 pub fn mouse(&self) -> &Mouse {
72 &self.mouse
73 }
74
75 /// Get a reference to the touchscreen controller.
76 ///
77 /// The touchscreen controller provides methods for touch input simulation.
78 /// Requires `hasTouch: true` in browser context options.
79 ///
80 /// # Example
81 ///
82 /// ```no_run
83 /// use viewpoint_core::Page;
84 ///
85 /// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
86 /// page.touchscreen().tap(100.0, 200.0).await?;
87 /// # Ok(())
88 /// # }
89 /// ```
90 pub fn touchscreen(&self) -> &Touchscreen {
91 &self.touchscreen
92 }
93
94 /// Get a clock controller for this page.
95 ///
96 /// The clock controller allows you to mock time-related JavaScript functions
97 /// including Date, setTimeout, setInterval, requestAnimationFrame, and
98 /// `performance.now()`.
99 ///
100 /// # Example
101 ///
102 /// ```no_run
103 /// use viewpoint_core::Page;
104 /// use std::time::Duration;
105 ///
106 /// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
107 /// // Install clock mocking
108 /// let mut clock = page.clock();
109 /// clock.install().await?;
110 ///
111 /// // Freeze time at a specific moment
112 /// clock.set_fixed_time("2024-01-01T00:00:00Z").await?;
113 ///
114 /// // Advance time and fire scheduled timers
115 /// clock.run_for(Duration::from_secs(5)).await?;
116 ///
117 /// // Cleanup
118 /// clock.uninstall().await?;
119 /// # Ok(())
120 /// # }
121 /// ```
122 pub fn clock(&self) -> Clock<'_> {
123 Clock::new(&self.connection, &self.session_id)
124 }
125
126 /// Drag from source to target element.
127 ///
128 /// Returns a builder for configuring drag options.
129 ///
130 /// # Example
131 ///
132 /// ```no_run
133 /// use viewpoint_core::Page;
134 ///
135 /// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
136 /// // Simple drag and drop
137 /// page.drag_and_drop("#source", "#target").send().await?;
138 ///
139 /// // With position options
140 /// page.drag_and_drop("#source", "#target")
141 /// .source_position(10.0, 10.0)
142 /// .target_position(5.0, 5.0)
143 /// .send()
144 /// .await?;
145 /// # Ok(())
146 /// # }
147 /// ```
148 pub fn drag_and_drop(
149 &self,
150 source: impl Into<String>,
151 target: impl Into<String>,
152 ) -> DragAndDropBuilder<'_> {
153 DragAndDropBuilder::new(self, source.into(), target.into())
154 }
155}