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
// Click options and related types
//
// Provides configuration for click and dblclick actions, matching Playwright's API.
use crate::protocol::action_options::Scroll;
use serde::Serialize;
/// Mouse button for click actions
///
/// # Example
///
/// ```no_run
/// use playwright_rs::protocol::click::MouseButton;
///
/// let button = MouseButton::Right;
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum MouseButton {
/// Left mouse button (default)
Left,
/// Right mouse button
Right,
/// Middle mouse button
Middle,
}
/// Keyboard modifier keys
///
/// # Example
///
/// ```no_run
/// use playwright_rs::protocol::click::KeyboardModifier;
///
/// let modifiers = vec![KeyboardModifier::Shift, KeyboardModifier::Control];
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub enum KeyboardModifier {
/// Alt key
Alt,
/// Control key
Control,
/// Meta key (Command on macOS, Windows key on Windows)
Meta,
/// Shift key
Shift,
/// Control on Windows/Linux, Meta on macOS
ControlOrMeta,
}
/// Position for click actions
///
/// Coordinates are relative to the top-left corner of the element's padding box.
///
/// # Example
///
/// ```no_run
/// use playwright_rs::protocol::click::Position;
///
/// let position = Position { x: 10.0, y: 20.0 };
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct Position {
/// X coordinate
pub x: f64,
/// Y coordinate
pub y: f64,
}
/// Click options
///
/// Configuration options for click and dblclick actions.
///
/// Use the builder pattern to construct options:
///
/// # Example
///
/// ```no_run
/// use playwright_rs::protocol::click::{ClickOptions, MouseButton, KeyboardModifier, Position};
///
/// // Right-click with modifiers
/// let options = ClickOptions::builder()
/// .button(MouseButton::Right)
/// .modifiers(vec![KeyboardModifier::Shift])
/// .build();
///
/// // Click at specific position
/// let options = ClickOptions::builder()
/// .position(Position { x: 10.0, y: 20.0 })
/// .build();
///
/// // Trial run (actionability checks only)
/// let options = ClickOptions::builder()
/// .trial(true)
/// .build();
/// ```
///
/// See: <https://playwright.dev/docs/api/class-locator#locator-click>
#[derive(Debug, Clone, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ClickOptions {
/// Mouse button to click (left, right, middle)
#[serde(skip_serializing_if = "Option::is_none")]
pub button: Option<MouseButton>,
/// Number of clicks (for multi-click)
#[serde(skip_serializing_if = "Option::is_none")]
pub click_count: Option<u32>,
/// Time to wait between mousedown and mouseup in milliseconds
#[serde(skip_serializing_if = "Option::is_none")]
pub delay: Option<f64>,
/// Whether to bypass actionability checks
#[serde(skip_serializing_if = "Option::is_none")]
pub force: Option<bool>,
/// Modifier keys to press during click
#[serde(skip_serializing_if = "Option::is_none")]
pub modifiers: Option<Vec<KeyboardModifier>>,
/// Don't wait for navigation after click
#[serde(skip_serializing_if = "Option::is_none")]
pub no_wait_after: Option<bool>,
/// Position to click relative to element top-left corner
#[serde(skip_serializing_if = "Option::is_none")]
pub position: Option<Position>,
/// Maximum time in milliseconds. Serializes to the default timeout when
/// unset (Playwright 1.56.1+ requires the field to be present).
#[serde(serialize_with = "crate::protocol::serialize_timeout_or_default")]
pub timeout: Option<f64>,
/// Perform actionability checks without clicking
#[serde(skip_serializing_if = "Option::is_none")]
pub trial: Option<bool>,
/// Whether the action may scroll the element into view first
#[serde(skip_serializing_if = "Option::is_none")]
pub scroll: Option<Scroll>,
}
impl ClickOptions {
/// Create a new builder for ClickOptions
pub fn builder() -> ClickOptionsBuilder {
ClickOptionsBuilder::default()
}
/// Convert options to JSON value for protocol
pub(crate) fn to_json(&self) -> serde_json::Value {
serde_json::to_value(self).expect("ClickOptions serialization cannot fail")
}
}
/// Builder for ClickOptions
///
/// Provides a fluent API for constructing click options.
#[derive(Debug, Clone, Default)]
pub struct ClickOptionsBuilder {
button: Option<MouseButton>,
click_count: Option<u32>,
delay: Option<f64>,
force: Option<bool>,
modifiers: Option<Vec<KeyboardModifier>>,
no_wait_after: Option<bool>,
position: Option<Position>,
timeout: Option<f64>,
trial: Option<bool>,
scroll: Option<Scroll>,
}
impl ClickOptionsBuilder {
/// Set the mouse button to click
pub fn button(mut self, button: MouseButton) -> Self {
self.button = Some(button);
self
}
/// Set the number of clicks
pub fn click_count(mut self, click_count: u32) -> Self {
self.click_count = Some(click_count);
self
}
/// Set delay between mousedown and mouseup in milliseconds
pub fn delay(mut self, delay: f64) -> Self {
self.delay = Some(delay);
self
}
/// Bypass actionability checks
pub fn force(mut self, force: bool) -> Self {
self.force = Some(force);
self
}
/// Set modifier keys to press during click
pub fn modifiers(mut self, modifiers: Vec<KeyboardModifier>) -> Self {
self.modifiers = Some(modifiers);
self
}
/// Don't wait for navigation after click
pub fn no_wait_after(mut self, no_wait_after: bool) -> Self {
self.no_wait_after = Some(no_wait_after);
self
}
/// Set position to click relative to element top-left corner
pub fn position(mut self, position: Position) -> Self {
self.position = Some(position);
self
}
/// Set timeout in milliseconds
pub fn timeout(mut self, timeout: f64) -> Self {
self.timeout = Some(timeout);
self
}
/// Perform actionability checks without clicking
pub fn trial(mut self, trial: bool) -> Self {
self.trial = Some(trial);
self
}
/// Opt out of scrolling the element into view (`Scroll::None`), or keep
/// Playwright's default (`Scroll::Auto`)
pub fn scroll(mut self, scroll: Scroll) -> Self {
self.scroll = Some(scroll);
self
}
/// Build the ClickOptions
pub fn build(self) -> ClickOptions {
ClickOptions {
button: self.button,
click_count: self.click_count,
delay: self.delay,
force: self.force,
modifiers: self.modifiers,
no_wait_after: self.no_wait_after,
position: self.position,
timeout: self.timeout,
trial: self.trial,
scroll: self.scroll,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mouse_button_serialization() {
assert_eq!(
serde_json::to_string(&MouseButton::Left).unwrap(),
"\"left\""
);
assert_eq!(
serde_json::to_string(&MouseButton::Right).unwrap(),
"\"right\""
);
assert_eq!(
serde_json::to_string(&MouseButton::Middle).unwrap(),
"\"middle\""
);
}
#[test]
fn test_keyboard_modifier_serialization() {
assert_eq!(
serde_json::to_string(&KeyboardModifier::Alt).unwrap(),
"\"Alt\""
);
assert_eq!(
serde_json::to_string(&KeyboardModifier::Control).unwrap(),
"\"Control\""
);
assert_eq!(
serde_json::to_string(&KeyboardModifier::Meta).unwrap(),
"\"Meta\""
);
assert_eq!(
serde_json::to_string(&KeyboardModifier::Shift).unwrap(),
"\"Shift\""
);
assert_eq!(
serde_json::to_string(&KeyboardModifier::ControlOrMeta).unwrap(),
"\"ControlOrMeta\""
);
}
#[test]
fn test_builder_button() {
let options = ClickOptions::builder().button(MouseButton::Right).build();
let json = options.to_json();
assert_eq!(json["button"], "right");
}
#[test]
fn test_builder_click_count() {
let options = ClickOptions::builder().click_count(2).build();
let json = options.to_json();
assert_eq!(json["clickCount"], 2);
}
#[test]
fn test_builder_delay() {
let options = ClickOptions::builder().delay(100.0).build();
let json = options.to_json();
assert_eq!(json["delay"], 100.0);
}
#[test]
fn test_builder_force() {
let options = ClickOptions::builder().force(true).build();
let json = options.to_json();
assert_eq!(json["force"], true);
}
#[test]
fn test_builder_modifiers() {
let options = ClickOptions::builder()
.modifiers(vec![KeyboardModifier::Shift, KeyboardModifier::Control])
.build();
let json = options.to_json();
assert_eq!(json["modifiers"], serde_json::json!(["Shift", "Control"]));
}
#[test]
fn test_builder_position() {
let position = Position { x: 10.0, y: 20.0 };
let options = ClickOptions::builder().position(position).build();
let json = options.to_json();
assert_eq!(json["position"]["x"], 10.0);
assert_eq!(json["position"]["y"], 20.0);
}
#[test]
fn test_builder_timeout() {
let options = ClickOptions::builder().timeout(5000.0).build();
let json = options.to_json();
assert_eq!(json["timeout"], 5000.0);
}
#[test]
fn test_builder_trial() {
let options = ClickOptions::builder().trial(true).build();
let json = options.to_json();
assert_eq!(json["trial"], true);
}
#[test]
fn test_builder_multiple_options() {
let options = ClickOptions::builder()
.button(MouseButton::Right)
.modifiers(vec![KeyboardModifier::Shift])
.position(Position { x: 5.0, y: 10.0 })
.force(true)
.timeout(3000.0)
.build();
let json = options.to_json();
assert_eq!(json["button"], "right");
assert_eq!(json["modifiers"], serde_json::json!(["Shift"]));
assert_eq!(json["position"]["x"], 5.0);
assert_eq!(json["position"]["y"], 10.0);
assert_eq!(json["force"], true);
assert_eq!(json["timeout"], 3000.0);
}
}