playwright_rs/protocol/mouse.rs
1// Mouse - Low-level mouse control
2//
3// See: https://playwright.dev/docs/api/class-mouse
4
5use crate::error::Result;
6use crate::protocol::page::Page;
7
8/// Mouse provides low-level mouse control.
9///
10/// Coordinates are in main-frame CSS pixels relative to the viewport's top-left corner.
11///
12/// See: <https://playwright.dev/docs/api/class-mouse>
13#[derive(Clone)]
14pub struct Mouse {
15 page: Page,
16}
17
18impl Mouse {
19 /// Creates a new Mouse instance for the given page
20 pub(crate) fn new(page: Page) -> Self {
21 Self { page }
22 }
23
24 /// Dispatches a `mousemove` event.
25 ///
26 /// # Note
27 ///
28 /// Under headless Linux with the bundled Chromium, a move dispatched while a
29 /// button is held (between [`down`](Self::down) and [`up`](Self::up)) can
30 /// hang: the browser treats it as a native drag whose move never returns.
31 /// Use [`Locator::drag_to`](crate::protocol::Locator::drag_to) for
32 /// drag-and-drop; discrete `down` / `up` without an intervening move are
33 /// unaffected.
34 ///
35 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-move>
36 pub async fn move_to(
37 &self,
38 x: f64,
39 y: f64,
40 options: impl Into<Option<crate::protocol::MouseOptions>>,
41 ) -> Result<()> {
42 let options = options.into();
43 self.page.mouse_move(x, y, options).await
44 }
45
46 /// Combines `move()`, `down()`, and `up()` actions.
47 ///
48 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-click>
49 pub async fn click(
50 &self,
51 x: f64,
52 y: f64,
53 options: impl Into<Option<crate::protocol::MouseOptions>>,
54 ) -> Result<()> {
55 let options = options.into();
56 self.page.mouse_click(x, y, options).await
57 }
58
59 /// Shortcut performing `move()`, `down()`, `up()`, `down()`, and `up()` sequentially.
60 ///
61 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-dblclick>
62 pub async fn dblclick(
63 &self,
64 x: f64,
65 y: f64,
66 options: impl Into<Option<crate::protocol::MouseOptions>>,
67 ) -> Result<()> {
68 let options = options.into();
69 self.page.mouse_dblclick(x, y, options).await
70 }
71
72 /// Dispatches a `mousedown` event.
73 ///
74 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-down>
75 pub async fn down(
76 &self,
77 options: impl Into<Option<crate::protocol::MouseOptions>>,
78 ) -> Result<()> {
79 let options = options.into();
80 self.page.mouse_down(options).await
81 }
82
83 /// Dispatches a `mouseup` event.
84 ///
85 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-up>
86 pub async fn up(
87 &self,
88 options: impl Into<Option<crate::protocol::MouseOptions>>,
89 ) -> Result<()> {
90 let options = options.into();
91 self.page.mouse_up(options).await
92 }
93
94 /// Dispatches a `wheel` event for manual page scrolling.
95 ///
96 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-wheel>
97 pub async fn wheel(&self, delta_x: f64, delta_y: f64) -> Result<()> {
98 self.page.mouse_wheel(delta_x, delta_y).await
99 }
100}