Skip to main content

yew_e2e/test_driver/
attributes.rs

1//! Reading many matches in one pass inside the page.
2
3use anyhow::{Context, Result};
4
5use crate::TestDriver;
6
7impl TestDriver {
8    /// **Every matching rendered text in one browser observation.**
9    ///
10    /// A live viewer can replace its rows while WebDriver walks a list of
11    /// element handles. Read the list inside the document when the
12    /// observation itself is the assertion, so it is one snapshot rather
13    /// than a handle per row.
14    pub async fn rendered_texts(&self, selector: &str) -> Result<Vec<String>> {
15        let read = self
16            .inner
17            .execute(
18                "return Array.from(document.querySelectorAll(arguments[0])) \
19                 .map(n => n.innerText)",
20                vec![serde_json::json!(selector)],
21            )
22            .await?;
23        let rows = read.json().as_array().context("rendered text array")?;
24
25        rows.iter()
26            .map(|row| row.as_str().context("rendered text").map(str::to_owned))
27            .collect()
28    }
29
30    /// **Every match's attribute, read in one pass.** Reading them one handle
31    /// at a time is how a test goes stale against a window that redraws on the
32    /// animation frame: the elements it is holding are replaced between two reads
33    /// and the second one throws.
34    pub async fn find_all_attrs(&self, selector: &str, attr: &str) -> Result<Vec<String>> {
35        let read = self
36            .inner
37            .execute(
38                "return Array.from(document.querySelectorAll(arguments[0])) \
39                 .map(n => n.getAttribute(arguments[1]) ?? '')",
40                vec![serde_json::json!(selector), serde_json::json!(attr)],
41            )
42            .await?;
43
44        Ok(read
45            .json()
46            .as_array()
47            .map(|rows| {
48                rows.iter()
49                    .map(|row| row.as_str().unwrap_or_default().to_owned())
50                    .collect()
51            })
52            .unwrap_or_default())
53    }
54
55    /// **Where every match sits down the page, with the attribute that names
56    /// it, in one pass.** A band is a top and a height, which is all two things
57    /// drawn in different columns have to agree about.
58    ///
59    /// Measuring a hundred elements one handle at a time is a round trip each
60    /// for the attribute and another for the box, which is a test that runs out
61    /// of time rather than one that fails. A node the browser is not drawing has
62    /// no box and is left out, so a count is also an answer to what is shown.
63    pub async fn bands(&self, selector: &str, attr: &str) -> Result<Vec<(String, f64, f64)>> {
64        let script = "return Array.from(document.querySelectorAll(arguments[0])) \
65             .filter(n => n.getClientRects().length > 0) \
66             .map(n => [n.getAttribute(arguments[1]) ?? '', \
67             n.getBoundingClientRect().top, n.getBoundingClientRect().height])";
68
69        let read = self
70            .inner
71            .execute(
72                script,
73                vec![serde_json::json!(selector), serde_json::json!(attr)],
74            )
75            .await?;
76        let read = read.json();
77
78        let rows = read.as_array().context("a list of bands")?;
79        let mut bands = Vec::with_capacity(rows.len());
80
81        for row in rows {
82            let row = row.as_array().context("a band")?;
83
84            bands.push((
85                row.first()
86                    .and_then(|name| name.as_str())
87                    .unwrap_or_default()
88                    .to_owned(),
89                row.get(1).and_then(|top| top.as_f64()).context("a top")?,
90                row.get(2)
91                    .and_then(|height| height.as_f64())
92                    .context("a height")?,
93            ));
94        }
95
96        Ok(bands)
97    }
98}