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'"]]);
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With