Xcode 如何从 UITests 下的 UIImagePickerController 的某个索引处选择图像?

How to pick an image at some index from UIImagePickerController under UITests in Xcode?

目前我访问照片库并通过以下方式挑选照片:

extension XCUIApplication {

    func pickPhotoFromImagePickerAtIndex(index: UInt) {

        tables.buttons["Moments"].tap()
        collectionViews["PhotosGridView"].tapCellAtIndex(index)
    }
}

使用示例:

photosCollectionView.tapCellAtIndex(0)
app.pickPhotoFromImagePickerAtIndex(5)

此方法有时会崩溃。这取决于画廊中的照片。

有没有更优雅有效的方式来做到这一点?

我假设,您需要点击的索引是从集合视图的底部开始计算的。如果是这样,那么您可以重新设计您的方法:

func pickPhotoFromImagePickerAtInvertedIndex(index: UInt) {

    tables.buttons["Camera Roll"].tap()
    let collectionView = collectionViews["PhotosGridView"]
    collectionView.cells.elementBoundByIndex(collectionView.cells.count - 1 - index).tap()
}