I also love the efforts by projects such as Appium to enable cross platform testing using any test framework you like (eg: Cucumber Ruby for me).
But why oh why must Apple require us to traverse complex hierarchy of elements (which may change) to find the element we want to test? Why no simple withPredicateRecursive(..) ?? That would make our lives much easier!
I spent a while expecting surely there is a way, but couldn't find it.
The simplest way was suggested in the book Test iOS Apps with UI Automation: Bug Hunting Made Easy which is to make a javascript function to recursively search the hierarchy.
Here is how (taken from the books publicly available sample code):
function recursiveSearch(predicate, startElement) {
target.pushTimeout(0);
// ...
var elements = startElement.elements();
var found = elements.firstWithPredicate(predicate);
target.popTimeout();
if (found.isValid()) return found;
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
found = recursiveSearch(predicate, element);
if (found) return found;
}
return null;
}
Use it like this:
var add_button = recursiveSearch("name matches 'Add'",
UIATarget.localTarget().frontMostApp().mainWindow());