如何使用XCUIElementsQuery测试UIView的存在?

Fel*_*rri 5 objective-c xcode-ui-testing

很难从Apple找到适用于UI测试框架的文档.我已经看到很多可以找到具有特定名称的按钮和标签的例子,但我怎样才能找到通用的UIViews?

例如,假设我使用accessibilityLabel =="Some 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'"]]);
Run Code Online (Sandbox Code Playgroud)

Cha*_* A. 7

我在上面的代码中看到的问题是,您专门尝试在应用程序的子代中查找视图,而不是应用程序的后代.子视图是视图的严格子视图,搜索不会查看子视图等.除此之外,它看起来很好.

尝试:

XCUIElement *pvc = [[app descendantsMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View'"]];
Run Code Online (Sandbox Code Playgroud)

此外,默认设置UIView是根本不参与辅助功能,因此除了设置标签(顺便提一下,关于设置标签的问题中的代码片段正在进行比较而不是分配),您还应确保启用辅助功能:

view.isAccessibilityElement = YES
view.accessibilityLabel = @"AccessibilityLabel"
Run Code Online (Sandbox Code Playgroud)