use xa11y::*;
pub fn app_tree() -> Tree {
app_tree_with(&QueryOptions::default())
}
pub fn app_tree_with(opts: &QueryOptions) -> Tree {
for attempt in 0..3 {
match xa11y::app(&AppTarget::ByName("xa11y".to_string()), opts) {
Ok(tree) => return tree,
Err(_) if attempt < 2 => {
std::thread::sleep(std::time::Duration::from_millis(200));
}
Err(e) => panic!("Could not find test app after retries: {}", e),
}
}
unreachable!()
}
pub fn one<'a>(tree: &'a Tree, selector: &str) -> &'a Node {
let results = tree.query(selector).unwrap_or_else(|e| {
panic!(
"Selector '{}' failed: {}. Tree:\n{}",
selector,
e,
tree.dump()
)
});
assert!(
results.len() == 1,
"Selector '{}' matched {} nodes (expected 1). Tree:\n{}",
selector,
results.len(),
tree.dump()
);
results[0]
}
pub fn named<'a>(tree: &'a Tree, substring: &str) -> &'a Node {
let selector = format!("[name*=\"{}\"]", substring);
let results = tree.query(&selector).unwrap_or_else(|e| {
panic!(
"Selector '{}' failed: {}. Tree:\n{}",
selector,
e,
tree.dump()
)
});
assert!(
!results.is_empty(),
"No node with name containing '{}'. Tree:\n{}",
substring,
tree.dump()
);
results[0]
}
pub fn act(tree: &Tree, node: &Node, action: Action) -> Tree {
act_with(tree, node, action, None)
}
pub fn act_with(tree: &Tree, node: &Node, action: Action, data: Option<ActionData>) -> Tree {
xa11y::perform_action(tree, node, action, data)
.unwrap_or_else(|e| panic!("Action {:?} failed: {}", action, e));
std::thread::sleep(std::time::Duration::from_millis(100));
app_tree()
}