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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use std::sync::Arc;
use std::time::Duration;
use crate::element::{Element, ElementData};
use crate::error::{Error, Result};
use crate::event::ElementState;
use crate::provider::Provider;
use crate::selector::{chain_combinator, SelectorGroup};
/// A lazy element descriptor that re-resolves against a fresh accessibility
/// tree on every operation.
///
/// Inspired by Playwright's `Locator` pattern: a Locator never holds a live
/// reference to a UI element. Instead, it stores a selector and resolves it
/// on demand, making it immune to staleness.
///
/// # Example
/// ```ignore
/// # use std::time::Duration;
/// # use xa11y::*;
/// # fn example() -> Result<()> {
/// let app = App::by_name("MyApp", Duration::from_secs(5))?;
/// let save_btn = app.locator(r#"button[name="Save"]"#);
/// save_btn.press()?;
/// # Ok(())
/// # }
/// ```
/// Default auto-wait timeout for Locator action methods (5 seconds).
const DEFAULT_ACTION_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Clone)]
pub struct Locator {
provider: Arc<dyn Provider>,
/// Root element for scoped searches. `None` = system root (all apps).
root: Option<ElementData>,
selector: String,
/// Which match to select (0-based). `None` means first match.
nth: Option<usize>,
/// Timeout for auto-wait before action methods.
timeout: Duration,
}
impl Locator {
/// Create a new Locator.
///
/// Pass `root: None` to search the entire accessibility tree, or
/// `Some(element)` to scope the search to that element's subtree.
pub fn new(provider: Arc<dyn Provider>, root: Option<ElementData>, selector: &str) -> Self {
Self {
provider,
root,
selector: selector.to_string(),
nth: None,
timeout: DEFAULT_ACTION_TIMEOUT,
}
}
/// Return a new Locator with a custom auto-wait timeout for action methods.
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
/// Return a new Locator that selects the nth match (1-based).
///
/// # Panics
/// Panics if `n` is 0. Use `.first()` or `.nth(1)` for the first match.
pub fn nth(mut self, n: usize) -> Self {
assert!(n > 0, "Locator::nth() is 1-based, got 0");
self.nth = Some(n - 1); // store 0-based internally
self
}
/// Return a new Locator that selects the first match.
pub fn first(self) -> Self {
self.nth(1)
}
/// Return a new Locator scoped to a direct child matching `child_selector`.
///
/// Appends ` > {child_selector}` to the current selector. If either side
/// is a comma-separated selector group, the combinator distributes over
/// every clause: e.g. `"a, b".child("c") => "a > c, b > c"`.
pub fn child(mut self, child_selector: &str) -> Self {
self.selector = chain_combinator(&self.selector, " > ", child_selector);
self.nth = None;
self
}
/// Return a new Locator scoped to a descendant matching `desc_selector`.
///
/// Appends ` {desc_selector}` to the current selector. If either side is
/// a comma-separated selector group, the combinator distributes over
/// every clause: e.g. `"a, b".descendant("c") => "a c, b c"`.
pub fn descendant(mut self, desc_selector: &str) -> Self {
self.selector = chain_combinator(&self.selector, " ", desc_selector);
self.nth = None;
self
}
/// Get the selector string.
pub fn selector(&self) -> &str {
&self.selector
}
/// Get the underlying provider.
#[doc(hidden)]
pub fn provider(&self) -> &Arc<dyn Provider> {
&self.provider
}
/// Get the root element data, if scoped.
#[doc(hidden)]
pub fn root(&self) -> Option<&ElementData> {
self.root.as_ref()
}
/// Get the nth index, if set.
#[doc(hidden)]
pub fn nth_index(&self) -> Option<usize> {
self.nth
}
// ── Internal resolution ─────────────────────────────────────────
/// Resolve the selector to a single ElementData.
fn resolve_data(&self) -> Result<ElementData> {
let group = SelectorGroup::parse(&self.selector)?;
// For multi-clause groups we can't safely truncate at the provider
// call to `nth+1` — a low-priority clause's match might come
// *before* a high-priority clause's in document order, so we need
// the full union to apply `nth` correctly.
let provider_limit = if group.is_single() {
Some(self.nth.unwrap_or(0) + 1)
} else {
None
};
let matches =
self.provider
.find_elements_group(self.root.as_ref(), &group, provider_limit, None)?;
let idx = self.nth.unwrap_or(0);
matches
.into_iter()
.nth(idx)
.ok_or_else(|| Error::SelectorNotMatched {
selector: self.selector.clone(),
})
}
// ── Queries (each re-queries the provider) ─────────────────────
/// Check if a matching element exists.
pub fn exists(&self) -> Result<bool> {
match self.resolve_data() {
Ok(_) => Ok(true),
Err(Error::SelectorNotMatched { .. }) => Ok(false),
Err(e) => Err(e),
}
}
/// Count matching elements.
pub fn count(&self) -> Result<usize> {
let group = SelectorGroup::parse(&self.selector)?;
let matches = self
.provider
.find_elements_group(self.root.as_ref(), &group, None, None)?;
Ok(matches.len())
}
/// Get a single [`Element`] handle.
pub fn element(&self) -> Result<Element> {
let data = self.resolve_data()?;
Ok(Element::new(data, Arc::clone(&self.provider)))
}
/// Get all matching elements.
pub fn elements(&self) -> Result<Vec<Element>> {
let group = SelectorGroup::parse(&self.selector)?;
let matches = self
.provider
.find_elements_group(self.root.as_ref(), &group, None, None)?;
Ok(matches
.into_iter()
.map(|d| Element::new(d, Arc::clone(&self.provider)))
.collect())
}
/// Capture the subtree rooted at the matched element as a recursive
/// snapshot. Resolves the selector once (no auto-wait — inspection ops
/// should fail fast on selector miss). See [`Element::tree`] for
/// `max_depth` semantics.
pub fn tree(&self, max_depth: Option<usize>) -> Result<crate::element::TreeNode> {
self.element()?.tree(max_depth)
}
/// Render the subtree rooted at the matched element as an indented
/// string. Resolves the selector once (no auto-wait). See
/// [`Element::dump`] for the output format.
pub fn dump(&self, max_depth: Option<usize>) -> Result<String> {
self.element()?.dump(max_depth)
}
// ── Auto-wait ──────────────────────────────────────────────────
/// Poll until the element is attached, visible, and enabled, returning a
/// live [`Element`] handle. Used by the action methods below to provide
/// resilience against transient unactionable states.
fn auto_wait(&self) -> Result<Element> {
let start = std::time::Instant::now();
let poll_interval = Duration::from_millis(100);
loop {
let elapsed = start.elapsed();
if elapsed >= self.timeout {
return Err(Error::Timeout { elapsed });
}
match self.resolve_data() {
Ok(data) if data.states.visible && data.states.enabled => {
return Ok(Element::new(data, Arc::clone(&self.provider)));
}
Ok(_) | Err(Error::SelectorNotMatched { .. }) => {
// Not yet actionable — poll again
}
Err(e) => return Err(e),
}
std::thread::sleep(poll_interval);
}
}
// ── Actions ────────────────────────────────────────────────────
//
// Locator actions auto-wait for the element to be visible and enabled
// (re-resolving the selector on each poll), then delegate to the
// [`Element`] action of the same name. For snapshot-bound actions that
// do not re-resolve, capture an [`Element`] via [`Locator::element`]
// and call its action methods directly.
/// Click / invoke the matched element.
pub fn press(&self) -> Result<()> {
self.auto_wait()?.press()
}
/// Set keyboard focus on the matched element.
pub fn focus(&self) -> Result<()> {
self.auto_wait()?.focus()
}
/// Remove keyboard focus from the matched element.
pub fn blur(&self) -> Result<()> {
self.auto_wait()?.blur()
}
/// Toggle the matched element (checkbox, switch).
pub fn toggle(&self) -> Result<()> {
self.auto_wait()?.toggle()
}
/// Select the matched element (list item, etc.).
pub fn select(&self) -> Result<()> {
self.auto_wait()?.select()
}
/// Expand the matched element.
pub fn expand(&self) -> Result<()> {
self.auto_wait()?.expand()
}
/// Collapse the matched element.
pub fn collapse(&self) -> Result<()> {
self.auto_wait()?.collapse()
}
/// Show the context menu for the matched element.
pub fn show_menu(&self) -> Result<()> {
self.auto_wait()?.show_menu()
}
/// Increment the matched element (slider, spinner).
pub fn increment(&self) -> Result<()> {
self.auto_wait()?.increment()
}
/// Decrement the matched element (slider, spinner).
pub fn decrement(&self) -> Result<()> {
self.auto_wait()?.decrement()
}
/// Scroll the matched element into view.
pub fn scroll_into_view(&self) -> Result<()> {
self.auto_wait()?.scroll_into_view()
}
/// Set the text value of the matched element.
pub fn set_value(&self, value: &str) -> Result<()> {
self.auto_wait()?.set_value(value)
}
/// Set the numeric value of the matched element (slider, spinner).
pub fn set_numeric_value(&self, value: f64) -> Result<()> {
// Validate up-front so callers fail fast on NaN/inf without burning
// the auto-wait timeout.
if !value.is_finite() {
return Err(Error::InvalidActionData {
message: format!("set_numeric_value requires a finite value, got {}", value),
});
}
self.auto_wait()?.set_numeric_value(value)
}
/// Type text at the current cursor position on the matched element.
pub fn type_text(&self, text: &str) -> Result<()> {
self.auto_wait()?.type_text(text)
}
/// Select a text range within the matched element.
pub fn select_text(&self, start: u32, end: u32) -> Result<()> {
if start > end {
return Err(Error::InvalidActionData {
message: format!("select_text start ({}) must be <= end ({})", start, end),
});
}
self.auto_wait()?.select_text(start, end)
}
/// Perform an action by name (with auto-wait).
///
/// This is the escape hatch for platform-specific actions not covered
/// by the named methods above. Also works for well-known action names.
pub fn perform_action(&self, action: &str) -> Result<()> {
self.auto_wait()?.perform_action(action)
}
// ── Wait operations ─────────────────────────────────────────────
/// Wait until the element is visible, polling the provider.
pub fn wait_visible(&self, timeout: Duration) -> Result<Element> {
self.wait_for_state(ElementState::Visible, timeout)
.map(|opt| opt.expect("visible wait must return an element"))
}
/// Wait until the element exists.
pub fn wait_attached(&self, timeout: Duration) -> Result<Element> {
self.wait_for_state(ElementState::Attached, timeout)
.map(|opt| opt.expect("attached wait must return an element"))
}
/// Wait until the element is removed.
pub fn wait_detached(&self, timeout: Duration) -> Result<()> {
self.wait_for_state(ElementState::Detached, timeout)
.map(|_| ())
}
/// Wait until the element is enabled.
pub fn wait_enabled(&self, timeout: Duration) -> Result<Element> {
self.wait_for_state(ElementState::Enabled, timeout)
.map(|opt| opt.expect("enabled wait must return an element"))
}
/// Wait until the element is disabled (exists but not enabled).
pub fn wait_disabled(&self, timeout: Duration) -> Result<Element> {
self.wait_for_state(ElementState::Disabled, timeout)
.map(|opt| opt.expect("disabled wait must return an element"))
}
/// Wait until the element is hidden or removed.
pub fn wait_hidden(&self, timeout: Duration) -> Result<()> {
self.wait_for_state(ElementState::Hidden, timeout)
.map(|_| ())
}
/// Wait until the element has keyboard focus.
pub fn wait_focused(&self, timeout: Duration) -> Result<Element> {
self.wait_for_state(ElementState::Focused, timeout)
.map(|opt| opt.expect("focused wait must return an element"))
}
/// Wait until the element does not have keyboard focus.
pub fn wait_unfocused(&self, timeout: Duration) -> Result<Element> {
self.wait_for_state(ElementState::Unfocused, timeout)
.map(|opt| opt.expect("unfocused wait must return an element"))
}
/// Wait for an [`ElementState`] condition to be met.
pub fn wait_for_state(
&self,
state: ElementState,
timeout: Duration,
) -> Result<Option<Element>> {
self.poll_until(|element| state.is_met(element), timeout)
}
/// Wait until an arbitrary predicate is satisfied, polling at ~100 ms intervals.
pub fn wait_until(
&self,
predicate: impl Fn(Option<&ElementData>) -> bool,
timeout: Duration,
) -> Result<Option<Element>> {
self.poll_until(&predicate, timeout)
}
/// Core polling loop shared by `wait_for_state` and `wait_until`.
fn poll_until(
&self,
predicate: impl Fn(Option<&ElementData>) -> bool,
timeout: Duration,
) -> Result<Option<Element>> {
let start = std::time::Instant::now();
let poll_interval = Duration::from_millis(100);
loop {
let elapsed = start.elapsed();
if elapsed >= timeout {
return Err(Error::Timeout { elapsed });
}
let matched = match self.resolve_data() {
Ok(data) => Some(data),
Err(Error::SelectorNotMatched { .. }) => None,
Err(e) => return Err(e),
};
if predicate(matched.as_ref()) {
return Ok(matched.map(|data| Element::new(data, Arc::clone(&self.provider))));
}
std::thread::sleep(poll_interval);
}
}
}
#[cfg(test)]
mod tests {
//! End-to-end tests for [`Locator`] against the in-memory mock provider.
//!
//! Covers:
//! - selector-group (comma alternation): union, dedup, document order
//! across clauses; `count()`, `elements()`, `nth()`, `element()`;
//! chained `.descendant()` / `.child()` distributing per clause.
//! - tree/dump inspection helpers (subtree capture, depth limits,
//! fail-fast on miss).
//!
//! The mock tree topology is documented on [`crate::mock`].
use super::*;
use crate::mock::build_provider;
fn root_locator(selector: &str) -> Locator {
let provider = build_provider();
let provider_dyn: Arc<dyn Provider> = provider;
Locator::new(provider_dyn, None, selector)
}
fn names(elements: &[Element]) -> Vec<String> {
elements
.iter()
.map(|e| e.data().name.clone().unwrap_or_default())
.collect()
}
#[test]
fn group_count_returns_union_size_across_clauses() {
// Two non-overlapping clauses; count is the sum.
let loc = root_locator("check_box, slider");
assert_eq!(loc.count().unwrap(), 2);
}
#[test]
fn group_count_dedups_overlapping_clauses() {
// `button` matches "Back" and "Forward"; `[name="Back"]` overlaps
// with "Back". The union must dedupe to 2 unique elements.
let loc = root_locator(r#"button, [name="Back"]"#);
assert_eq!(loc.count().unwrap(), 2);
}
#[test]
fn group_elements_returned_in_document_order() {
// Mock tree DFS order through Navigation/Content:
// Back, Forward (toolbar) → Search (text_field) → ... → Item 2.
// The result of `button, text_field` must interleave by document
// position, not group by clause.
let loc = root_locator("button, text_field");
let names = names(&loc.elements().unwrap());
assert_eq!(names, vec!["Back", "Forward", "Search"]);
}
#[test]
fn group_element_returns_first_match_in_document_order() {
// `text_field, button` would naively return Search first (the first
// text_field clause matches first by clause). Document order makes
// "Back" win.
let loc = root_locator("text_field, button");
let el = loc.element().expect("element must resolve");
assert_eq!(el.data().name.as_deref(), Some("Back"));
}
#[test]
fn group_nth_picks_across_full_union() {
// `.nth(2)` on the document-ordered union [Back, Forward, Search]
// must be "Forward", not the 2nd element of any single clause.
let loc = root_locator("button, text_field").nth(2);
let el = loc.element().unwrap();
assert_eq!(el.data().name.as_deref(), Some("Forward"));
}
#[test]
fn group_single_clause_behaves_identically() {
// Sanity: no-comma selectors are unaffected by SelectorGroup parsing.
let single = root_locator("button");
assert_eq!(single.count().unwrap(), 2);
assert_eq!(names(&single.elements().unwrap()), vec!["Back", "Forward"],);
}
#[test]
fn descendant_distributes_over_clauses() {
// `.descendant("button")` on a group of (toolbar, group) parents
// must apply to *both* parents. Direct child buttons exist only
// under "toolbar"; if distribution failed, we'd miss them. Inverse
// case is harder to construct in this fixture, but we verify the
// generated string round-trips and matches buttons under each
// clause's subtree.
let loc = root_locator("toolbar, group").descendant("button");
// After chaining, the stored selector must distribute per clause:
assert_eq!(loc.selector(), "toolbar button, group button");
// And resolve to the two buttons (both under toolbar; the group
// subtree has no buttons in this fixture).
let names = names(&loc.elements().unwrap());
assert_eq!(names, vec!["Back", "Forward"]);
}
#[test]
fn child_distributes_over_clauses() {
// `.child("button")` on a (toolbar, group) parent group should
// distribute the `>` combinator over each clause.
let loc = root_locator("toolbar, group").child("button");
assert_eq!(loc.selector(), "toolbar > button, group > button");
let names = names(&loc.elements().unwrap());
assert_eq!(names, vec!["Back", "Forward"]);
}
#[test]
fn descendant_after_group_then_another_group_cross_products() {
// Repeated chained navigation keeps distributing — and a group
// suffix multiplies clauses (cross product). Verify the stored
// selector form is what we expect; semantic resolution is covered
// by the other tests.
let loc = root_locator("toolbar, group").descendant("button, text_field");
assert_eq!(
loc.selector(),
"toolbar button, toolbar text_field, group button, group text_field",
);
}
#[test]
fn group_exists_true_when_any_clause_matches() {
// First clause matches nothing, second matches; existence is union.
let loc = root_locator(r#"button[name="Nope"], slider"#);
assert!(loc.exists().unwrap());
}
#[test]
fn group_exists_false_when_no_clause_matches() {
let loc = root_locator(r#"button[name="Nope"], text_field[name="AlsoNope"]"#);
assert!(!loc.exists().unwrap());
}
// ── tree() / dump() ─────────────────────────────────────────────
#[test]
fn locator_tree_returns_subtree_rooted_at_match() {
let node = root_locator("application")
.tree(None)
.expect("tree must succeed");
assert_eq!(node.role, "application");
assert_eq!(node.name.as_deref(), Some("TestApp"));
assert!(!node.children.is_empty());
}
#[test]
fn locator_tree_respects_max_depth() {
let node = root_locator("application")
.tree(Some(0))
.expect("tree must succeed");
assert!(node.children.is_empty(), "max_depth=0 should drop children");
}
#[test]
fn locator_dump_renders_selector_subtree() {
let s = root_locator("application")
.dump(None)
.expect("dump must succeed");
assert!(
s.contains(r#"application "TestApp""#),
"dump should render the matched root: {s}"
);
}
#[test]
fn locator_dump_max_depth_zero_is_one_line() {
let s = root_locator("application")
.dump(Some(0))
.expect("dump must succeed");
let non_empty: Vec<&str> = s.lines().filter(|l| !l.trim().is_empty()).collect();
assert_eq!(non_empty.len(), 1);
}
#[test]
fn locator_tree_no_match_returns_selector_not_matched() {
let err = root_locator(r#"button[name="DoesNotExist"]"#)
.tree(None)
.expect_err("tree must fail on miss");
assert!(
matches!(err, Error::SelectorNotMatched { .. }),
"expected SelectorNotMatched, got {err:?}"
);
}
#[test]
fn locator_dump_does_not_auto_wait() {
// Locator dump/tree are inspection ops — they must fail fast, not poll.
let locator = root_locator(r#"button[name="DoesNotExist"]"#);
let start = std::time::Instant::now();
let _ = locator.dump(None);
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(500),
"dump should fail fast, took {elapsed:?}"
);
}
}