UItesting : 点击 UIcollectionview 然后测试它失败

0 xcode objective-c ios uicollectionview xcode-ui-testing

我是 ui-testing 的新手。在记录期间,当我挖掘UI收集查看第一对象被显示在用户界面和对应于写入在测试示例方法的代码是:

XCUIElement *window = [[app childrenMatchingType:XCUIElementTypeWindow] elementBoundByIndex:0];
    XCUIElement *element2 = [[[[window childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element;
    [element2 tap]; 
Run Code Online (Sandbox Code Playgroud)

并且在自动化 test-example 方法时,无法获取 ui-collection view 的第一个对象。请建议一种方法来做到这一点。提前致谢。

joe*_*ern 5

记录 UITests 往往会给你带来非常长和丑陋的查询。我强烈建议您查看如何手动编写 UITest。这真的很容易,而且查询看起来好多了。

例如:要点击集合视图的第一个单元格,您只需执行以下操作(前提是UICollectionView当前屏幕上只有一个单元格):

目标-C

- (void)testExample {
    XCUIApplication *app = [[XCUIApplication alloc] init];
    [app launch];

    [[app.collectionViews.cells elementBoundByIndex:0] tap];
}
Run Code Online (Sandbox Code Playgroud)

迅速

func testExample() {
    let app = XCUIApplication()
    app.launch()

    // tap on the first collection view cell on the current screen
    app.collectionViews.cells.element(boundBy:0).tap()
}
Run Code Online (Sandbox Code Playgroud)