use xa11y::*;
pub fn app_root() -> App {
for attempt in 0..3 {
match App::by_name("xa11y-test-app") {
Ok(app) => return app,
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(app: &App, selector: &str) -> Element {
let results = app
.locator(selector)
.elements()
.unwrap_or_else(|e| panic!("Selector '{}' failed: {}. App: {}", selector, e, app));
assert!(
results.len() == 1,
"Selector '{}' matched {} elements (expected 1). App: {}",
selector,
results.len(),
app
);
results.into_iter().next().unwrap()
}
pub fn named(app: &App, substring: &str) -> Element {
let selector = format!("[name*=\"{}\"]", substring);
let results = app
.locator(&selector)
.elements()
.unwrap_or_else(|e| panic!("Selector '{}' failed: {}. App: {}", selector, e, app));
assert!(
!results.is_empty(),
"No element with name containing '{}'. App: {}",
substring,
app
);
results.into_iter().next().unwrap()
}
pub fn try_act(element: &Element, action: Action) -> Result<()> {
try_act_with(element, action, None)
}
pub fn try_act_with(element: &Element, action: Action, data: Option<ActionData>) -> Result<()> {
element.provider().perform_action(element, action, data)
}
pub fn act(element: &Element, action: Action) -> App {
act_with(element, action, None)
}
pub fn act_with(element: &Element, action: Action, data: Option<ActionData>) -> App {
try_act_with(element, action, data)
.unwrap_or_else(|e| panic!("Action {:?} failed: {}", action, e));
std::thread::sleep(std::time::Duration::from_millis(100));
app_root()
}