Skip to main content

playwright_rs/protocol/
drag_to.rs

1// DragToOptions and related types
2//
3// Provides configuration for drag_to actions, matching Playwright's API.
4
5use crate::protocol::click::Position;
6
7/// Options for [`Locator::drag_to()`].
8///
9/// Configuration for dragging a source element onto a target element.
10///
11/// Use the builder pattern to construct options:
12///
13/// # Example
14///
15/// ```ignore
16/// use playwright_rs::{DragToOptions, Position};
17///
18/// // Drag with custom source and target positions
19/// let options = DragToOptions::builder()
20///     .source_position(Position { x: 10.0, y: 10.0 })
21///     .target_position(Position { x: 60.0, y: 60.0 })
22///     .build();
23///
24/// // Force drag (bypass actionability checks)
25/// let options = DragToOptions::builder()
26///     .force(true)
27///     .build();
28///
29/// // Trial run (actionability checks only, don't actually drag)
30/// let options = DragToOptions::builder()
31///     .trial(true)
32///     .build();
33/// ```
34///
35/// See: <https://playwright.dev/docs/api/class-locator#locator-drag-to>
36#[derive(Debug, Clone, Default)]
37pub struct DragToOptions {
38    /// Whether to bypass actionability checks
39    pub force: Option<bool>,
40    /// Don't wait for navigation after the action
41    pub no_wait_after: Option<bool>,
42    /// Maximum time in milliseconds
43    pub timeout: Option<f64>,
44    /// Perform actionability checks without dragging
45    pub trial: Option<bool>,
46    /// Where to click on the source element (relative to top-left corner)
47    pub source_position: Option<Position>,
48    /// Where to drop on the target element (relative to top-left corner)
49    pub target_position: Option<Position>,
50}
51
52impl DragToOptions {
53    /// Create a new builder for DragToOptions
54    pub fn builder() -> DragToOptionsBuilder {
55        DragToOptionsBuilder::default()
56    }
57
58    /// Convert options to JSON value for protocol
59    pub(crate) fn to_json(&self) -> serde_json::Value {
60        let mut json = serde_json::json!({});
61
62        if let Some(force) = self.force {
63            json["force"] = serde_json::json!(force);
64        }
65
66        if let Some(no_wait_after) = self.no_wait_after {
67            json["noWaitAfter"] = serde_json::json!(no_wait_after);
68        }
69
70        // Timeout is required in Playwright 1.56.1+
71        if let Some(timeout) = self.timeout {
72            json["timeout"] = serde_json::json!(timeout);
73        } else {
74            json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
75        }
76
77        if let Some(trial) = self.trial {
78            json["trial"] = serde_json::json!(trial);
79        }
80
81        if let Some(source_position) = &self.source_position {
82            json["sourcePosition"] = serde_json::to_value(source_position).unwrap();
83        }
84
85        if let Some(target_position) = &self.target_position {
86            json["targetPosition"] = serde_json::to_value(target_position).unwrap();
87        }
88
89        json
90    }
91}
92
93/// Builder for DragToOptions
94///
95/// Provides a fluent API for constructing drag_to options.
96#[derive(Debug, Clone, Default)]
97pub struct DragToOptionsBuilder {
98    force: Option<bool>,
99    no_wait_after: Option<bool>,
100    timeout: Option<f64>,
101    trial: Option<bool>,
102    source_position: Option<Position>,
103    target_position: Option<Position>,
104}
105
106impl DragToOptionsBuilder {
107    /// Bypass actionability checks
108    pub fn force(mut self, force: bool) -> Self {
109        self.force = Some(force);
110        self
111    }
112
113    /// Don't wait for navigation after the action
114    pub fn no_wait_after(mut self, no_wait_after: bool) -> Self {
115        self.no_wait_after = Some(no_wait_after);
116        self
117    }
118
119    /// Set timeout in milliseconds
120    pub fn timeout(mut self, timeout: f64) -> Self {
121        self.timeout = Some(timeout);
122        self
123    }
124
125    /// Perform actionability checks without dragging
126    pub fn trial(mut self, trial: bool) -> Self {
127        self.trial = Some(trial);
128        self
129    }
130
131    /// Set where to click on the source element (relative to top-left corner)
132    pub fn source_position(mut self, source_position: Position) -> Self {
133        self.source_position = Some(source_position);
134        self
135    }
136
137    /// Set where to drop on the target element (relative to top-left corner)
138    pub fn target_position(mut self, target_position: Position) -> Self {
139        self.target_position = Some(target_position);
140        self
141    }
142
143    /// Build the DragToOptions
144    pub fn build(self) -> DragToOptions {
145        DragToOptions {
146            force: self.force,
147            no_wait_after: self.no_wait_after,
148            timeout: self.timeout,
149            trial: self.trial,
150            source_position: self.source_position,
151            target_position: self.target_position,
152        }
153    }
154}
155
156#[cfg(test)]
157mod tests {
158    use super::*;
159
160    #[test]
161    fn test_drag_to_options_default() {
162        let options = DragToOptions::builder().build();
163        let json = options.to_json();
164        // timeout has a default value
165        assert!(json["timeout"].is_number());
166        // other optional fields are absent
167        assert!(json.get("force").is_none());
168        assert!(json.get("trial").is_none());
169        assert!(json.get("sourcePosition").is_none());
170        assert!(json.get("targetPosition").is_none());
171    }
172
173    #[test]
174    fn test_drag_to_options_force() {
175        let options = DragToOptions::builder().force(true).build();
176        let json = options.to_json();
177        assert_eq!(json["force"], true);
178    }
179
180    #[test]
181    fn test_drag_to_options_timeout() {
182        let options = DragToOptions::builder().timeout(5000.0).build();
183        let json = options.to_json();
184        assert_eq!(json["timeout"], 5000.0);
185    }
186
187    #[test]
188    fn test_drag_to_options_trial() {
189        let options = DragToOptions::builder().trial(true).build();
190        let json = options.to_json();
191        assert_eq!(json["trial"], true);
192    }
193
194    #[test]
195    fn test_drag_to_options_positions() {
196        let options = DragToOptions::builder()
197            .source_position(Position { x: 5.0, y: 10.0 })
198            .target_position(Position { x: 50.0, y: 60.0 })
199            .build();
200        let json = options.to_json();
201        assert_eq!(json["sourcePosition"]["x"], 5.0);
202        assert_eq!(json["sourcePosition"]["y"], 10.0);
203        assert_eq!(json["targetPosition"]["x"], 50.0);
204        assert_eq!(json["targetPosition"]["y"], 60.0);
205    }
206
207    #[test]
208    fn test_drag_to_options_no_wait_after() {
209        let options = DragToOptions::builder().no_wait_after(true).build();
210        let json = options.to_json();
211        assert_eq!(json["noWaitAfter"], true);
212    }
213}