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
//! Reading many matches in one pass inside the page.
use anyhow::{Context, Result};
use crate::TestDriver;
impl TestDriver {
/// **Every matching rendered text in one browser observation.**
///
/// A live viewer can replace its rows while WebDriver walks a list of
/// element handles. Read the list inside the document when the
/// observation itself is the assertion, so it is one snapshot rather
/// than a handle per row.
pub async fn rendered_texts(&self, selector: &str) -> Result<Vec<String>> {
let read = self
.inner
.execute(
"return Array.from(document.querySelectorAll(arguments[0])) \
.map(n => n.innerText)",
vec![serde_json::json!(selector)],
)
.await?;
let rows = read.json().as_array().context("rendered text array")?;
rows.iter()
.map(|row| row.as_str().context("rendered text").map(str::to_owned))
.collect()
}
/// **Every match's attribute, read in one pass.** Reading them one handle
/// at a time is how a test goes stale against a window that redraws on the
/// animation frame: the elements it is holding are replaced between two reads
/// and the second one throws.
pub async fn find_all_attrs(&self, selector: &str, attr: &str) -> Result<Vec<String>> {
let read = self
.inner
.execute(
"return Array.from(document.querySelectorAll(arguments[0])) \
.map(n => n.getAttribute(arguments[1]) ?? '')",
vec![serde_json::json!(selector), serde_json::json!(attr)],
)
.await?;
Ok(read
.json()
.as_array()
.map(|rows| {
rows.iter()
.map(|row| row.as_str().unwrap_or_default().to_owned())
.collect()
})
.unwrap_or_default())
}
/// **Where every match sits down the page, with the attribute that names
/// it, in one pass.** A band is a top and a height, which is all two things
/// drawn in different columns have to agree about.
///
/// Measuring a hundred elements one handle at a time is a round trip each
/// for the attribute and another for the box, which is a test that runs out
/// of time rather than one that fails. A node the browser is not drawing has
/// no box and is left out, so a count is also an answer to what is shown.
pub async fn bands(&self, selector: &str, attr: &str) -> Result<Vec<(String, f64, f64)>> {
let script = "return Array.from(document.querySelectorAll(arguments[0])) \
.filter(n => n.getClientRects().length > 0) \
.map(n => [n.getAttribute(arguments[1]) ?? '', \
n.getBoundingClientRect().top, n.getBoundingClientRect().height])";
let read = self
.inner
.execute(
script,
vec![serde_json::json!(selector), serde_json::json!(attr)],
)
.await?;
let read = read.json();
let rows = read.as_array().context("a list of bands")?;
let mut bands = Vec::with_capacity(rows.len());
for row in rows {
let row = row.as_array().context("a band")?;
bands.push((
row.first()
.and_then(|name| name.as_str())
.unwrap_or_default()
.to_owned(),
row.get(1).and_then(|top| top.as_f64()).context("a top")?,
row.get(2)
.and_then(|height| height.as_f64())
.context("a height")?,
));
}
Ok(bands)
}
}