Xcode UI 测试集合视图单元格在 iOS 11 上变得不可点击

Xcode UI testing collection view cells became non-hittable on iOS 11

我们使用此代码模拟在 Xcode UI 上 UICollectionView 的第一个单元格上的点击测试:

XCUIElementQuery *query = [application descendantsMatchingType:XCUIElementTypeAny];
XCUIElement *collectionView = [query objectForKeyedSubscript:collectionViewAccessibilityIdentifier];
XCUIElement *targetCell = [lensesCollectionView.cells elementBoundByIndex:cellIndex];
if (targetCell.hittable) {
    [targetCell tap];
}

这在 iOS 10 上工作正常,但在 iOS 11 上停止工作。无论如何,targetCell 永远不会是 hittable。在 XCUIElement *targetCell = [lensesCollectionView.cells elementBoundByIndex:lensIndex] 之前添加 sleep(10) 没有帮助。

我看到其他地方提到的 hacky 解决方案,例如

func forceTapElement() {   
    if self.isHittable {   
        self.tap()   
    } else {   
        var coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0))   
        coordinate.tap()   
    }  
}

但这看起来不太干净。实现此目标的最简洁方法是什么?


更新:如果我在不检查 hittable 的情况下尝试点击它,我会收到此错误:

error: Error -25204 performing AXAction 2003 on element pid: 43616, elementOrHash.elementID: 4882574576.240

事实证明,isAccessibilityElement 在我们的自定义 集合视图单元格 iOS 11(奇怪的是,在 iOS 10 上是 YES)。将其明确设置为 YES 解决了问题。

Ricardo的回答应该是可以接受的。 为清楚起见,我们遇到了同样的问题并在 UICollectionViewCell 的 Class 文件中解决了它。 在 initWithCoder: 中我们刚刚添加了 属性 isAccessibilityElement:

- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];

    if (self != nil) {
        self.isAccessibilityElement = YES;
    }

    return self;
}

我们现在可以 运行 在 Xcode 9.1 中形成相同的测试脚本 Xcode 8 并且可以正确点击单元格。

感谢这个伟大的解决方案。