如何使用XCUIElementsQuery测试是否存在UIView?

时间:2015-12-14 18:45:01

标签: 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'"]]);

2 个答案:

答案 0 :(得分:6)

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

尝试:

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

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

view.isAccessibilityElement = YES
view.accessibilityLabel = @"AccessibilityLabel"

答案 1 :(得分:2)

来自其他答案的查询有效,但我发现您也可以通过以下方式查找视图:

app.otherElements["MenuViewController.menuView"].swipeLeft()

在我的情况MenuViewController.menuView中,UIView设置为accessibilityIdentifier