objective-c - 如何使用 XCUIElementQuery 测试 UIView 是否存在?

标签 objective-c xcode-ui-testing

很难从 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/

相关文章:

ios - Xcode UI 测试手册截图

ios - XC UITesting 因查找 UIElements 而闪烁

iOS Objective-C 获取 YouTube 视频的直接链接以供下载

java - 尝试从构建多部分的 Objective-C 文本在 Java 中构建多部分实体

ios - 入口点 (_main) 未定义。对于体系结构 x86_64 - 仅限 XCode UITesting

ios - 在 XCUITest 的可访问性指示器中找不到堆栈 View

ios - -[UIApplication _runWithMainScene :transitionContext:completion:], 中的断言失败

objective-c - 使用单个对象/键替代 NSDictionary

iphone - 在编译时识别 iDevices,最佳实践

ios - 有没有办法找到 XCUIElement 是否有焦点?