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
//! Headless test helpers: locate elements by visible text or `title`/`alt`, and click them.
//!
//! These replace hand-rolled depth-first searches over `NodeBlueprint` / `LayoutNode` that match
//! on `Debug` output. Matching walks the blueprint and layout trees in lockstep (so it honours
//! fragment flattening and skips `display: none` subtrees) and returns hierarchical index paths
//! compatible with [`AppRuntime::find_node_by_path`].
//!
//! Matches are returned in document (pre-order) order. Later elements, such as modals and
//! `<Portal>` content, are painted above earlier ones, so use the `*_last` variants to target the
//! top-most match.
use super::*;
/// How a [`NodeQuery`] compares its needle against an element's text or title.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MatchMode {
/// Whitespace-normalized equality (default).
#[default]
Exact,
/// Whitespace-normalized substring containment.
Contains,
}
/// Declarative element query for [`AppRuntime::find_all`], [`AppRuntime::click`] and friends.
///
/// When both `text` and `title` are set, an element must satisfy both. Only the *deepest*
/// matching elements are reported: an ancestor whose text merely aggregates a matching
/// descendant is not itself a match.
#[derive(Debug, Clone, Default)]
pub struct NodeQuery {
text: Option<String>,
title: Option<String>,
mode: MatchMode,
}
impl NodeQuery {
/// Matches elements whose rendered text content equals `text` (see [`NodeQuery::contains`]).
pub fn text(text: impl Into<String>) -> Self {
Self {
text: Some(text.into()),
..Self::default()
}
}
/// Matches elements whose rendered text content contains `substring` (whitespace-normalized).
pub fn text_contains(substring: impl Into<String>) -> Self {
Self::text(substring).contains()
}
/// Matches elements whose `title` attribute (or `alt` on `<button>` / `<img>`) equals `title`.
///
/// `title` and `alt` are not prioritised here, unlike the hover tooltip (which prefers
/// `title`): the element matches if **either** attribute matches the needle. A button with
/// `title="Save"` and `alt="Store"` is found by both `"Save"` and `"Store"`. `alt` is only
/// considered on `<button>` and `<img>`; other elements match on `title` alone.
pub fn title(title: impl Into<String>) -> Self {
Self {
title: Some(title.into()),
..Self::default()
}
}
/// Matches elements whose `title` (or `alt` on `<button>` / `<img>`) contains `substring`.
pub fn title_contains(substring: impl Into<String>) -> Self {
Self::title(substring).contains()
}
/// Additionally requires the element's rendered text content to match.
pub fn with_text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into());
self
}
/// Additionally requires the element's `title` / `alt` to match.
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Switches from whitespace-normalized equality to substring containment.
pub fn contains(mut self) -> Self {
self.mode = MatchMode::Contains;
self
}
fn matches_str(&self, needle: &str, haystack: &str) -> bool {
let needle = normalize_ws(needle);
let haystack = normalize_ws(haystack);
match self.mode {
MatchMode::Exact => haystack == needle,
MatchMode::Contains => haystack.contains(&needle),
}
}
}
fn normalize_ws(s: &str) -> String {
s.split_whitespace().collect::<Vec<_>>().join(" ")
}
fn collect_text(bp: &NodeBlueprint, out: &mut String) {
match bp {
NodeBlueprint::Text(t) => {
if !out.is_empty() && !out.ends_with(' ') {
out.push(' ');
}
out.push_str(t);
}
NodeBlueprint::DynamicText(f) => {
if !out.is_empty() && !out.ends_with(' ') {
out.push(' ');
}
out.push_str(&f());
}
NodeBlueprint::Element { children, .. } | NodeBlueprint::Fragment(children) => {
for c in children {
collect_text(c, out);
}
}
NodeBlueprint::Empty => {}
}
}
fn own_tooltip_attrs(bp: &NodeBlueprint) -> Vec<&str> {
let mut found = Vec::new();
if let NodeBlueprint::Element {
tag, attributes, ..
} = bp
{
let supports_alt = match tag {
strata_dom::Tag::Img | strata_dom::Tag::Button => true,
strata_dom::Tag::Custom(name) => {
name.eq_ignore_ascii_case("img") || name.eq_ignore_ascii_case("button")
}
_ => false,
};
for attr in attributes {
if let strata_dom::Attribute::Property { key, value } = attr {
if key.eq_ignore_ascii_case("title")
|| (supports_alt && key.eq_ignore_ascii_case("alt"))
{
found.push(value.as_str());
}
}
}
}
found
}
impl AppRuntime {
fn query_matches_node(query: &NodeQuery, bp: &NodeBlueprint) -> bool {
if !matches!(bp, NodeBlueprint::Element { .. }) {
return false;
}
if query.text.is_none() && query.title.is_none() {
return false;
}
if let Some(ref needle) = query.text {
let mut content = String::new();
collect_text(bp, &mut content);
if !query.matches_str(needle, &content) {
return false;
}
}
if let Some(ref needle) = query.title {
if !own_tooltip_attrs(bp)
.into_iter()
.any(|v| query.matches_str(needle, v))
{
return false;
}
}
true
}
/// Walks the paired trees; returns whether `bp` or any descendant matched. A node is reported
/// only if none of its descendants matched, so results are the deepest matches in
/// document order.
fn collect_query_matches(
query: &NodeQuery,
bp: &NodeBlueprint,
layout: &LayoutNode,
path: &mut Vec<usize>,
out: &mut Vec<Vec<usize>>,
) -> bool {
if path.len() >= 256 {
return false;
}
let mut any_child = false;
for (idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
if matches!(
child_layout.style.display,
strata_style::types::Display::None
) {
continue;
}
path.push(idx);
if Self::collect_query_matches(query, child_bp, child_layout, path, out) {
any_child = true;
}
path.pop();
}
if !any_child && Self::query_matches_node(query, bp) {
out.push(path.clone());
return true;
}
any_child
}
/// Returns the tree paths of every element matching `query`, in document order.
pub fn find_all(&self, query: &NodeQuery) -> Vec<Vec<usize>> {
let mut out = Vec::new();
let mut path = Vec::new();
Self::collect_query_matches(
query,
&self.root_blueprint,
&self.layout_root,
&mut path,
&mut out,
);
out
}
/// First (bottom-most in paint order) element matching `query`.
pub fn find(&self, query: &NodeQuery) -> Option<Vec<usize>> {
self.find_all(query).into_iter().next()
}
/// Last (top-most in paint order) element matching `query`, e.g. a modal's button when the
/// same label also exists on the page behind it.
pub fn find_last(&self, query: &NodeQuery) -> Option<Vec<usize>> {
self.find_all(query).into_iter().next_back()
}
/// Shorthand for `find(&NodeQuery::text(text))`.
pub fn find_by_text(&self, text: &str) -> Option<Vec<usize>> {
self.find(&NodeQuery::text(text))
}
/// Shorthand for `find(&NodeQuery::text_contains(substring))`.
pub fn find_by_text_contains(&self, substring: &str) -> Option<Vec<usize>> {
self.find(&NodeQuery::text_contains(substring))
}
/// Shorthand for `find_last(&NodeQuery::text(text))`.
pub fn find_last_by_text(&self, text: &str) -> Option<Vec<usize>> {
self.find_last(&NodeQuery::text(text))
}
/// Shorthand for `find_last(&NodeQuery::text_contains(substring))`.
pub fn find_last_by_text_contains(&self, substring: &str) -> Option<Vec<usize>> {
self.find_last(&NodeQuery::text_contains(substring))
}
/// Shorthand for `find(&NodeQuery::title(title))`.
pub fn find_by_title(&self, title: &str) -> Option<Vec<usize>> {
self.find(&NodeQuery::title(title))
}
/// Shorthand for `find(&NodeQuery::title_contains(substring))`.
pub fn find_by_title_contains(&self, substring: &str) -> Option<Vec<usize>> {
self.find(&NodeQuery::title_contains(substring))
}
/// Shorthand for `find_last(&NodeQuery::title(title))`.
pub fn find_last_by_title(&self, title: &str) -> Option<Vec<usize>> {
self.find_last(&NodeQuery::title(title))
}
/// Shorthand for `find_last(&NodeQuery::title_contains(substring))`.
pub fn find_last_by_title_contains(&self, substring: &str) -> Option<Vec<usize>> {
self.find_last(&NodeQuery::title_contains(substring))
}
/// Rendered text content of the element at `path`, whitespace-normalized.
pub fn node_text(&self, path: &[usize]) -> Option<String> {
let (bp, _) = Self::find_node_by_path(&self.root_blueprint, &self.layout_root, path)?;
let mut content = String::new();
collect_text(bp, &mut content);
Some(normalize_ws(&content))
}
/// Border-box center of the element at `path` in window coordinates.
pub fn node_center(&self, path: &[usize]) -> Option<(f32, f32)> {
let (_, layout) = Self::find_node_by_path(&self.root_blueprint, &self.layout_root, path)?;
let b = layout.geometry.border_box;
Some((b.x + b.width * 0.5, b.y + b.height * 0.5))
}
/// Clicks the center of the element at `path` through the normal hit-test path (so an
/// occluding element receives the click, as for a real pointer). Returns `true` if a
/// click listener ran.
pub fn click_node(&self, path: &[usize]) -> bool {
match self.node_center(path) {
Some((x, y)) => self.dispatch_click(x, y),
None => false,
}
}
/// Clicks the first element matching `query`. Returns `false` if none matched or no listener ran.
pub fn click(&self, query: &NodeQuery) -> bool {
self.find(query).is_some_and(|p| self.click_node(&p))
}
/// Clicks the last (top-most) element matching `query`.
pub fn click_last(&self, query: &NodeQuery) -> bool {
self.find_last(query).is_some_and(|p| self.click_node(&p))
}
/// Clicks the first element whose text equals `text`.
pub fn click_text(&self, text: &str) -> bool {
self.click(&NodeQuery::text(text))
}
/// Clicks the first element whose text contains `substring`.
pub fn click_text_contains(&self, substring: &str) -> bool {
self.click(&NodeQuery::text_contains(substring))
}
/// Clicks the last (top-most) element whose text equals `text`.
pub fn click_last_text(&self, text: &str) -> bool {
self.click_last(&NodeQuery::text(text))
}
/// Clicks the last (top-most) element whose text contains `substring`.
pub fn click_last_text_contains(&self, substring: &str) -> bool {
self.click_last(&NodeQuery::text_contains(substring))
}
/// Clicks the first element whose `title` or (on button/img) `alt` equals `title`; either
/// attribute may match. See [`NodeQuery::title`].
pub fn click_title(&self, title: &str) -> bool {
self.click(&NodeQuery::title(title))
}
/// Clicks the first element whose `title` or (on button/img) `alt` contains `substring`.
pub fn click_title_contains(&self, substring: &str) -> bool {
self.click(&NodeQuery::title_contains(substring))
}
/// Clicks the last (top-most) element whose `title` or (on button/img) `alt` equals `title`;
/// either attribute may match. See [`NodeQuery::title`].
pub fn click_last_title(&self, title: &str) -> bool {
self.click_last(&NodeQuery::title(title))
}
/// Clicks the last (top-most) element whose `title` or (on button/img) `alt` contains `substring`.
pub fn click_last_title_contains(&self, substring: &str) -> bool {
self.click_last(&NodeQuery::title_contains(substring))
}
}