yew_e2e/test_driver/
attributes.rs1use anyhow::{Context, Result};
4
5use crate::TestDriver;
6
7impl TestDriver {
8 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 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 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}