Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test for the existence of a UIView using XCUIElementsQuery?

It is difficult to find proper documentation for the UI Testing Framework from Apple. I've seen many examples where it is possible to find buttons and labels with specific names, but how can I look for generic UIViews?

For example, suppose I set a view with accessibilityLabel == "Some View", how can I find a view with that name during the test execution?

I tried (unsuccessfully) the following code:

XCUIElement *pvc = [[app childrenMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View'"]];

NSPredicate *exists = [NSPredicate predicateWithFormat:@"exists == true"];
[self expectationForPredicate:exists evaluatedWithObject:pvc handler:nil];
[self waitForExpectationsWithTimeout:10 handler:nil];

NSLog(@"%@", [[app childrenMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View Controller'"]]);
like image 591
Felipe Ferri Avatar asked Dec 14 '15 18:12

Felipe Ferri


1 Answers

The problem I see with the code you have above is that you're specifically trying to find a view in the children of the app, instead of the descendants of the app. Children are strictly subviews of a view, and the search won't look at sub-sub-views, etc. Other than that, it looks fine.

Try:

XCUIElement *pvc = [[app descendantsMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View'"]];

Also, the default for UIView is to not participate in accessibility at all, so in addition to setting the label (and incidentally the code snippet in your question about setting a label is doing comparison instead of assignment) you should also make sure you enable accessibility:

view.isAccessibilityElement = YES
view.accessibilityLabel = @"AccessibilityLabel"
like image 124
Charles A. Avatar answered Dec 09 '22 12:12

Charles A.