很难从 Apple 找到适用于 UI 测试框架的文档。我见过很多可以找到具有特定名称的按钮和标签的示例,但我如何才能找到通用的 UIView?
例如,假设我设置了 accessibilityLabel == "Some View"的 View ,我如何在测试执行期间找到具有该名称的 View ?
我尝试(未成功)以下代码:
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'"]]);
最佳答案
我在上面的代码中看到的问题是,您专门尝试在应用程序的子项而不是应用程序的后代中查找 View 。 subview 严格来说是 View 的 subview ,搜索不会查看子 subview 等。除此之外,它看起来还不错。
尝试:
XCUIElement *pvc = [[app descendantsMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View'"]];
此外,UIView
的默认设置是根本不参与可访问性,因此除了设置标签之外(顺便说一下,您关于设置标签的问题中的代码片段正在做比较而不是分配)你还应该确保你启用了辅助功能:
view.isAccessibilityElement = YES
view.accessibilityLabel = @"AccessibilityLabel"
关于objective-c - 如何使用 XCUIElementQuery 测试 UIView 是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34274341/