playwright_rs/protocol/
drag_to.rs1use crate::protocol::click::Position;
6
7#[derive(Debug, Clone, Default)]
37pub struct DragToOptions {
38 pub force: Option<bool>,
40 pub no_wait_after: Option<bool>,
42 pub timeout: Option<f64>,
44 pub trial: Option<bool>,
46 pub source_position: Option<Position>,
48 pub target_position: Option<Position>,
50}
51
52impl DragToOptions {
53 pub fn builder() -> DragToOptionsBuilder {
55 DragToOptionsBuilder::default()
56 }
57
58 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 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#[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 pub fn force(mut self, force: bool) -> Self {
109 self.force = Some(force);
110 self
111 }
112
113 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 pub fn timeout(mut self, timeout: f64) -> Self {
121 self.timeout = Some(timeout);
122 self
123 }
124
125 pub fn trial(mut self, trial: bool) -> Self {
127 self.trial = Some(trial);
128 self
129 }
130
131 pub fn source_position(mut self, source_position: Position) -> Self {
133 self.source_position = Some(source_position);
134 self
135 }
136
137 pub fn target_position(mut self, target_position: Position) -> Self {
139 self.target_position = Some(target_position);
140 self
141 }
142
143 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 assert!(json["timeout"].is_number());
166 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}