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
//! [`TestElement`], one element a lookup found.
use anyhow::Result;
use anyhow::ensure;
use thirtyfour::common::types::ElementRect;
use thirtyfour::prelude::*;
use tokio::time::{self, Duration};
use crate::finding::common;
use crate::{Holding, IntoBy, Waiting};
/// The first step of a drag along one axis: up to 8px towards `offset`, never
/// past it, so that the rest of the drag moves `offset` less this.
pub(crate) fn drag_nudge(offset: i64) -> i64 {
offset.clamp(-8, 8)
}
/// A wrapped WebElement.
pub struct TestElement {
pub(crate) inner: WebElement,
pub(crate) wait_timeout: Duration,
pub(crate) waiting: Waiting,
pub(crate) holding: Holding,
}
impl TestElement {
/// The WebDriver element underneath, for what this wrapper has no method
/// for.
pub fn element(&self) -> &WebElement {
&self.inner
}
/// Click the element.
pub async fn click(&self) -> Result<()> {
tracing::trace!("Clicking element");
self.inner.click().await?;
Ok(())
}
/// **Press the element with control held**, which is how a selection is
/// commonly extended by one item.
pub async fn extend_click(&self) -> Result<()> {
tracing::trace!("Extend-clicking element");
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.key_down(thirtyfour::Key::Control)
.click_element(&self.inner)
.key_up(thirtyfour::Key::Control)
.perform()
.await?;
Ok(())
}
/// **Press the element with shift held**, which picks the run from the
/// last item picked to this one in a list that has an order.
pub async fn shift_click(&self) -> Result<()> {
tracing::trace!("Shift-clicking element");
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.key_down(thirtyfour::Key::Shift)
.click_element(&self.inner)
.key_up(thirtyfour::Key::Shift)
.perform()
.await?;
Ok(())
}
/// Rest the pointer on the element without pressing anything.
pub async fn hover(&self) -> Result<()> {
tracing::trace!("Hovering element");
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.move_to_element_center(&self.inner)
.perform()
.await?;
Ok(())
}
/// Press the element twice, as one gesture.
pub async fn double_click(&self) -> Result<()> {
tracing::trace!("Double-clicking element");
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.double_click_element(&self.inner)
.perform()
.await?;
Ok(())
}
/// **The same, a given offset from the element's centre**, for one element
/// covering a whole surface, where on it a gesture lands being what the
/// gesture is about.
pub async fn double_click_by(&self, dx: i64, dy: i64) -> Result<()> {
tracing::trace!(dx, dy, "Double-clicking element off centre");
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.move_to_element_center(&self.inner)
.move_by_offset(dx, dy)
.double_click()
.perform()
.await?;
Ok(())
}
/// **Press the element once, a given offset from its centre.** A slider is
/// one element covering its whole travel, so where on it a press lands is
/// the whole of what the press says.
pub async fn click_by(&self, dx: i64, dy: i64) -> Result<()> {
tracing::trace!(dx, dy, "Clicking element off centre");
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.move_to_element_center(&self.inner)
.move_by_offset(dx, dy)
.click()
.perform()
.await?;
Ok(())
}
/// Right-click the element.
pub async fn context_click(&self) -> Result<()> {
tracing::trace!("Context-clicking element");
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.context_click_element(&self.inner)
.perform()
.await?;
Ok(())
}
/// Press on this element and drag `dx`, `dy` from where it is, **leaving
/// the button down**.
///
/// The pointer first steps up to 8px towards the offset, so the page sees
/// a drag begin, and then moves the rest of the way: it comes to rest
/// exactly `dx`, `dy` from where it was pressed.
pub async fn drag_by(&self, dx: i64, dy: i64) -> Result<()> {
tracing::trace!(dx, dy, "Dragging element");
let (nx, ny) = (drag_nudge(dx), drag_nudge(dy));
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.move_to_element_center(&self.inner)
.click_and_hold()
.move_by_offset(nx, ny)
.move_by_offset(dx - nx, dy - ny)
.perform()
.await?;
self.holding.set(true);
Ok(())
}
/// The same begun at a point of the element's own choosing rather than at
/// its centre, `at_x` and `at_y` being pixels from that centre.
///
/// A sweep that has to begin in one row of a grid cannot begin wherever the
/// element happens to be halved.
pub async fn drag_from_by(&self, at_x: i64, at_y: i64, dx: i64, dy: i64) -> Result<()> {
tracing::trace!(at_x, at_y, dx, dy, "Dragging from a point in an element");
let (nx, ny) = (drag_nudge(dx), drag_nudge(dy));
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.move_to_element_center(&self.inner)
.move_by_offset(at_x, at_y)
.click_and_hold()
.move_by_offset(nx, ny)
.move_by_offset(dx - nx, dy - ny)
.perform()
.await?;
self.holding.set(true);
Ok(())
}
/// The same with `alt` held.
pub async fn alt_drag_by(&self, dx: i64, dy: i64) -> Result<()> {
tracing::trace!(dx, dy, "Dragging element with alt held");
let (nx, ny) = (drag_nudge(dx), drag_nudge(dy));
self.inner.scroll_into_view().await?;
self.inner
.handle()
.action_chain()
.key_down(thirtyfour::Key::Alt)
.move_to_element_center(&self.inner)
.click_and_hold()
.move_by_offset(nx, ny)
.move_by_offset(dx - nx, dy - ny)
.perform()
.await?;
self.holding.set(true);
Ok(())
}
/// Get the text contents for this element.
pub async fn text(&self) -> Result<String> {
tracing::trace!("Getting element text");
Ok(self.inner.text().await?)
}
/// One of the element's DOM properties.
pub async fn prop(&self, name: &str) -> Result<String> {
tracing::trace!(name, "Getting element property");
Ok(self.inner.prop(name).await?.unwrap_or_default())
}
/// One of the element's DOM *attributes*, or the empty string when it has
/// none.
pub async fn attr(&self, name: &str) -> Result<String> {
tracing::trace!(name, "Getting element attribute");
Ok(self.inner.attr(name).await?.unwrap_or_default())
}
/// Bring the element into view.
pub async fn scroll_into_view(&self) -> Result<()> {
tracing::trace!("Scrolling element into view");
self.inner.scroll_into_view().await?;
Ok(())
}
/// Put keyboard focus here without pressing anything.
pub async fn focus(&self) -> Result<()> {
tracing::trace!("Focusing element");
self.inner.focus().await?;
Ok(())
}
/// Whether the element is displayed, as WebDriver judges it: present in
/// the page is not enough, it must also not be hidden by CSS
/// (`display: none`, `visibility: hidden`) or have no size.
///
/// Answers once, at the time of asking; it does not wait.
pub async fn visible(&self) -> Result<bool> {
Ok(self.inner.is_displayed().await?)
}
/// Where this element is on screen, and how big it is.
pub async fn rect(&self) -> Result<ElementRect> {
tracing::trace!("Measuring element");
Ok(self.inner.rect().await?)
}
/// One of the element's *computed* CSS properties, as `getComputedStyle`
/// serializes it.
///
/// The value is read in the page rather than through WebDriver's own
/// command, which chromedriver answers with every colour rewritten as
/// `rgba(...)`: a colour reads `rgb(255, 0, 0)` in every engine alike.
pub async fn css(&self, name: &str) -> Result<String> {
tracing::trace!(name, "Getting element style");
let script = "return getComputedStyle(arguments[0]).getPropertyValue(arguments[1]);";
let value = self
.inner
.handle()
.execute(script, vec![self.inner.to_json()?, serde_json::json!(name)])
.await?;
Ok(value.json().as_str().unwrap_or_default().to_string())
}
/// The element's `value` property.
pub async fn value(&self) -> Result<String> {
tracing::trace!("Getting element value");
Ok(self.inner.prop("value").await?.unwrap_or_default())
}
/// Clear the element contents.
pub async fn clear(&self) -> Result<()> {
tracing::trace!("Clearing element text");
self.inner.clear().await?;
Ok(())
}
/// Send the specified input targeting the element.
pub async fn send_keys(&self, keys: &str) -> Result<()> {
tracing::trace!(?keys, "Sending keys to element");
self.inner.send_keys(keys).await?;
Ok(())
}
/// Whether the element is currently pressed, by `aria-pressed`.
pub async fn pressed(&self) -> Result<bool> {
Ok(self.prop("ariaPressed").await? == "true")
}
/// Whether the control can be worked at all.
pub async fn enabled(&self) -> Result<bool> {
Ok(self.inner.is_enabled().await?)
}
/// Whether the control says it is out of play, by `aria-disabled`. This is
/// the question to ask of a custom control: `enabled` reads the form
/// control's `disabled`, which an element that is not a form control
/// cannot carry, so it answers `true` for one however dead it is.
pub async fn disabled(&self) -> Result<bool> {
Ok(self.prop("ariaDisabled").await? == "true")
}
common!();
}