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 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-move>
27 pub async fn move_to(
28 &self,
29 x: i32,
30 y: i32,
31 options: Option<crate::protocol::MouseOptions>,
32 ) -> Result<()> {
33 self.page.mouse_move(x, y, options).await
34 }
35
36 /// Combines `move()`, `down()`, and `up()` actions.
37 ///
38 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-click>
39 pub async fn click(
40 &self,
41 x: i32,
42 y: i32,
43 options: Option<crate::protocol::MouseOptions>,
44 ) -> Result<()> {
45 self.page.mouse_click(x, y, options).await
46 }
47
48 /// Shortcut performing `move()`, `down()`, `up()`, `down()`, and `up()` sequentially.
49 ///
50 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-dblclick>
51 pub async fn dblclick(
52 &self,
53 x: i32,
54 y: i32,
55 options: Option<crate::protocol::MouseOptions>,
56 ) -> Result<()> {
57 self.page.mouse_dblclick(x, y, options).await
58 }
59
60 /// Dispatches a `mousedown` event.
61 ///
62 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-down>
63 pub async fn down(&self, options: Option<crate::protocol::MouseOptions>) -> Result<()> {
64 self.page.mouse_down(options).await
65 }
66
67 /// Dispatches a `mouseup` event.
68 ///
69 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-up>
70 pub async fn up(&self, options: Option<crate::protocol::MouseOptions>) -> Result<()> {
71 self.page.mouse_up(options).await
72 }
73
74 /// Dispatches a `wheel` event for manual page scrolling.
75 ///
76 /// See: <https://playwright.dev/docs/api/class-mouse#mouse-wheel>
77 pub async fn wheel(&self, delta_x: i32, delta_y: i32) -> Result<()> {
78 self.page.mouse_wheel(delta_x, delta_y).await
79 }
80}