Skip to main content

yew_e2e/
element.rs

1//! [`TestElement`], one element a lookup found.
2
3use 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
12/// The first step of a drag along one axis: up to 8px towards `offset`, never
13/// past it, so that the rest of the drag moves `offset` less this.
14pub(crate) fn drag_nudge(offset: i64) -> i64 {
15    offset.clamp(-8, 8)
16}
17
18/// A wrapped WebElement.
19pub 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    /// The WebDriver element underneath, for what this wrapper has no method
28    /// for.
29    pub fn element(&self) -> &WebElement {
30        &self.inner
31    }
32
33    /// Click the element.
34    pub async fn click(&self) -> Result<()> {
35        tracing::trace!("Clicking element");
36        self.inner.click().await?;
37        Ok(())
38    }
39
40    /// **Press the element with control held**, which is how a selection is
41    /// commonly extended by one item.
42    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    /// **Press the element with shift held**, which picks the run from the
60    /// last item picked to this one in a list that has an order.
61    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    /// Rest the pointer on the element without pressing anything.
79    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    /// Press the element twice, as one gesture.
95    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    /// **The same, a given offset from the element's centre**, for one element
111    /// covering a whole surface, where on it a gesture lands being what the
112    /// gesture is about.
113    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    /// **Press the element once, a given offset from its centre.** A slider is
131    /// one element covering its whole travel, so where on it a press lands is
132    /// the whole of what the press says.
133    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    /// Right-click the element.
151    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    /// Press on this element and drag `dx`, `dy` from where it is, **leaving
167    /// the button down**.
168    ///
169    /// The pointer first steps up to 8px towards the offset, so the page sees
170    /// a drag begin, and then moves the rest of the way: it comes to rest
171    /// exactly `dx`, `dy` from where it was pressed.
172    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    /// The same begun at a point of the element's own choosing rather than at
194    /// its centre, `at_x` and `at_y` being pixels from that centre.
195    ///
196    /// A sweep that has to begin in one row of a grid cannot begin wherever the
197    /// element happens to be halved.
198    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    /// The same with `alt` held.
221    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    /// Get the text contents for this element.
244    pub async fn text(&self) -> Result<String> {
245        tracing::trace!("Getting element text");
246        Ok(self.inner.text().await?)
247    }
248
249    /// One of the element's DOM properties.
250    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    /// One of the element's DOM *attributes*, or the empty string when it has
256    /// none.
257    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    /// Bring the element into view.
263    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    /// Put keyboard focus here without pressing anything.
270    pub async fn focus(&self) -> Result<()> {
271        tracing::trace!("Focusing element");
272        self.inner.focus().await?;
273        Ok(())
274    }
275
276    /// Whether the element is displayed, as WebDriver judges it: present in
277    /// the page is not enough, it must also not be hidden by CSS
278    /// (`display: none`, `visibility: hidden`) or have no size.
279    ///
280    /// Answers once, at the time of asking; it does not wait.
281    pub async fn visible(&self) -> Result<bool> {
282        Ok(self.inner.is_displayed().await?)
283    }
284
285    /// Where this element is on screen, and how big it is.
286    pub async fn rect(&self) -> Result<ElementRect> {
287        tracing::trace!("Measuring element");
288        Ok(self.inner.rect().await?)
289    }
290
291    /// One of the element's *computed* CSS properties, as `getComputedStyle`
292    /// serializes it.
293    ///
294    /// The value is read in the page rather than through WebDriver's own
295    /// command, which chromedriver answers with every colour rewritten as
296    /// `rgba(...)`: a colour reads `rgb(255, 0, 0)` in every engine alike.
297    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    /// The element's `value` property.
312    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    /// Clear the element contents.
318    pub async fn clear(&self) -> Result<()> {
319        tracing::trace!("Clearing element text");
320        self.inner.clear().await?;
321        Ok(())
322    }
323
324    /// Send the specified input targeting the element.
325    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    /// Whether the element is currently pressed, by `aria-pressed`.
332    pub async fn pressed(&self) -> Result<bool> {
333        Ok(self.prop("ariaPressed").await? == "true")
334    }
335
336    /// Whether the control can be worked at all.
337    pub async fn enabled(&self) -> Result<bool> {
338        Ok(self.inner.is_enabled().await?)
339    }
340
341    /// Whether the control says it is out of play, by `aria-disabled`. This is
342    /// the question to ask of a custom control: `enabled` reads the form
343    /// control's `disabled`, which an element that is not a form control
344    /// cannot carry, so it answers `true` for one however dead it is.
345    pub async fn disabled(&self) -> Result<bool> {
346        Ok(self.prop("ariaDisabled").await? == "true")
347    }
348
349    common!();
350}