1use anyhow::Result;
4use anyhow::ensure;
5use thirtyfour::common::types::ElementRect;
6use thirtyfour::prelude::*;
7use tokio::time::{self, Duration};
8
9use crate::finding::common;
10use crate::{Holding, IntoBy, Waiting};
11
12pub(crate) fn drag_nudge(offset: i64) -> i64 {
15 offset.clamp(-8, 8)
16}
17
18pub struct TestElement {
20 pub(crate) inner: WebElement,
21 pub(crate) wait_timeout: Duration,
22 pub(crate) waiting: Waiting,
23 pub(crate) holding: Holding,
24}
25
26impl TestElement {
27 pub fn element(&self) -> &WebElement {
30 &self.inner
31 }
32
33 pub async fn click(&self) -> Result<()> {
35 tracing::trace!("Clicking element");
36 self.inner.click().await?;
37 Ok(())
38 }
39
40 pub async fn extend_click(&self) -> Result<()> {
43 tracing::trace!("Extend-clicking element");
44
45 self.inner.scroll_into_view().await?;
46
47 self.inner
48 .handle()
49 .action_chain()
50 .key_down(thirtyfour::Key::Control)
51 .click_element(&self.inner)
52 .key_up(thirtyfour::Key::Control)
53 .perform()
54 .await?;
55
56 Ok(())
57 }
58
59 pub async fn shift_click(&self) -> Result<()> {
62 tracing::trace!("Shift-clicking element");
63
64 self.inner.scroll_into_view().await?;
65
66 self.inner
67 .handle()
68 .action_chain()
69 .key_down(thirtyfour::Key::Shift)
70 .click_element(&self.inner)
71 .key_up(thirtyfour::Key::Shift)
72 .perform()
73 .await?;
74
75 Ok(())
76 }
77
78 pub async fn hover(&self) -> Result<()> {
80 tracing::trace!("Hovering element");
81
82 self.inner.scroll_into_view().await?;
83
84 self.inner
85 .handle()
86 .action_chain()
87 .move_to_element_center(&self.inner)
88 .perform()
89 .await?;
90
91 Ok(())
92 }
93
94 pub async fn double_click(&self) -> Result<()> {
96 tracing::trace!("Double-clicking element");
97
98 self.inner.scroll_into_view().await?;
99
100 self.inner
101 .handle()
102 .action_chain()
103 .double_click_element(&self.inner)
104 .perform()
105 .await?;
106
107 Ok(())
108 }
109
110 pub async fn double_click_by(&self, dx: i64, dy: i64) -> Result<()> {
114 tracing::trace!(dx, dy, "Double-clicking element off centre");
115
116 self.inner.scroll_into_view().await?;
117
118 self.inner
119 .handle()
120 .action_chain()
121 .move_to_element_center(&self.inner)
122 .move_by_offset(dx, dy)
123 .double_click()
124 .perform()
125 .await?;
126
127 Ok(())
128 }
129
130 pub async fn click_by(&self, dx: i64, dy: i64) -> Result<()> {
134 tracing::trace!(dx, dy, "Clicking element off centre");
135
136 self.inner.scroll_into_view().await?;
137
138 self.inner
139 .handle()
140 .action_chain()
141 .move_to_element_center(&self.inner)
142 .move_by_offset(dx, dy)
143 .click()
144 .perform()
145 .await?;
146
147 Ok(())
148 }
149
150 pub async fn context_click(&self) -> Result<()> {
152 tracing::trace!("Context-clicking element");
153
154 self.inner.scroll_into_view().await?;
155
156 self.inner
157 .handle()
158 .action_chain()
159 .context_click_element(&self.inner)
160 .perform()
161 .await?;
162
163 Ok(())
164 }
165
166 pub async fn drag_by(&self, dx: i64, dy: i64) -> Result<()> {
173 tracing::trace!(dx, dy, "Dragging element");
174
175 let (nx, ny) = (drag_nudge(dx), drag_nudge(dy));
176
177 self.inner.scroll_into_view().await?;
178
179 self.inner
180 .handle()
181 .action_chain()
182 .move_to_element_center(&self.inner)
183 .click_and_hold()
184 .move_by_offset(nx, ny)
185 .move_by_offset(dx - nx, dy - ny)
186 .perform()
187 .await?;
188 self.holding.set(true);
189
190 Ok(())
191 }
192
193 pub async fn drag_from_by(&self, at_x: i64, at_y: i64, dx: i64, dy: i64) -> Result<()> {
199 tracing::trace!(at_x, at_y, dx, dy, "Dragging from a point in an element");
200
201 let (nx, ny) = (drag_nudge(dx), drag_nudge(dy));
202
203 self.inner.scroll_into_view().await?;
204
205 self.inner
206 .handle()
207 .action_chain()
208 .move_to_element_center(&self.inner)
209 .move_by_offset(at_x, at_y)
210 .click_and_hold()
211 .move_by_offset(nx, ny)
212 .move_by_offset(dx - nx, dy - ny)
213 .perform()
214 .await?;
215 self.holding.set(true);
216
217 Ok(())
218 }
219
220 pub async fn alt_drag_by(&self, dx: i64, dy: i64) -> Result<()> {
222 tracing::trace!(dx, dy, "Dragging element with alt held");
223
224 let (nx, ny) = (drag_nudge(dx), drag_nudge(dy));
225
226 self.inner.scroll_into_view().await?;
227
228 self.inner
229 .handle()
230 .action_chain()
231 .key_down(thirtyfour::Key::Alt)
232 .move_to_element_center(&self.inner)
233 .click_and_hold()
234 .move_by_offset(nx, ny)
235 .move_by_offset(dx - nx, dy - ny)
236 .perform()
237 .await?;
238 self.holding.set(true);
239
240 Ok(())
241 }
242
243 pub async fn text(&self) -> Result<String> {
245 tracing::trace!("Getting element text");
246 Ok(self.inner.text().await?)
247 }
248
249 pub async fn prop(&self, name: &str) -> Result<String> {
251 tracing::trace!(name, "Getting element property");
252 Ok(self.inner.prop(name).await?.unwrap_or_default())
253 }
254
255 pub async fn attr(&self, name: &str) -> Result<String> {
258 tracing::trace!(name, "Getting element attribute");
259 Ok(self.inner.attr(name).await?.unwrap_or_default())
260 }
261
262 pub async fn scroll_into_view(&self) -> Result<()> {
264 tracing::trace!("Scrolling element into view");
265 self.inner.scroll_into_view().await?;
266 Ok(())
267 }
268
269 pub async fn focus(&self) -> Result<()> {
271 tracing::trace!("Focusing element");
272 self.inner.focus().await?;
273 Ok(())
274 }
275
276 pub async fn visible(&self) -> Result<bool> {
282 Ok(self.inner.is_displayed().await?)
283 }
284
285 pub async fn rect(&self) -> Result<ElementRect> {
287 tracing::trace!("Measuring element");
288 Ok(self.inner.rect().await?)
289 }
290
291 pub async fn css(&self, name: &str) -> Result<String> {
298 tracing::trace!(name, "Getting element style");
299
300 let script = "return getComputedStyle(arguments[0]).getPropertyValue(arguments[1]);";
301
302 let value = self
303 .inner
304 .handle()
305 .execute(script, vec![self.inner.to_json()?, serde_json::json!(name)])
306 .await?;
307
308 Ok(value.json().as_str().unwrap_or_default().to_string())
309 }
310
311 pub async fn value(&self) -> Result<String> {
313 tracing::trace!("Getting element value");
314 Ok(self.inner.prop("value").await?.unwrap_or_default())
315 }
316
317 pub async fn clear(&self) -> Result<()> {
319 tracing::trace!("Clearing element text");
320 self.inner.clear().await?;
321 Ok(())
322 }
323
324 pub async fn send_keys(&self, keys: &str) -> Result<()> {
326 tracing::trace!(?keys, "Sending keys to element");
327 self.inner.send_keys(keys).await?;
328 Ok(())
329 }
330
331 pub async fn pressed(&self) -> Result<bool> {
333 Ok(self.prop("ariaPressed").await? == "true")
334 }
335
336 pub async fn enabled(&self) -> Result<bool> {
338 Ok(self.inner.is_enabled().await?)
339 }
340
341 pub async fn disabled(&self) -> Result<bool> {
346 Ok(self.prop("ariaDisabled").await? == "true")
347 }
348
349 common!();
350}