代码之家  ›  专栏  ›  技术社区  ›  Ricardo Sanchez-Saez

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

  •  3
  • Ricardo Sanchez-Saez  · 技术社区  · 8 年前

    我们使用这段代码来模拟对 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 不管怎样。添加 sleep(10) 之前 XCUIElement *targetCell = [lensesCollectionView.cells elementBoundByIndex:lensIndex] 没有帮助。

    我见过其他地方提到的黑客解决方案,例如

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

    但这看起来不太干净。实现这一点最干净的方法是什么?


    使现代化 :如果我不检查 我发现这个错误:

    错误:错误-25204对元素pid:43616,elementOrHash执行AXAction 2003。元素ID:4882574576.240

    2 回复  |  直到 8 年前
        1
  •  4
  •   Ricardo Sanchez-Saez    8 年前

    事实证明 isAccessibilityElement NO 关于我们的习惯 集合视图单元格 在…上 iOS 11 (奇怪的是 YES 在…上 iOS 10 ). 显式设置为 修复了该问题。

        2
  •  0
  •   Martijn    8 年前

    里卡多的答案应该是公认的。 为了清楚起见,我们也遇到了同样的问题,并在UICollectionViewCell的类文件中解决了它。 在里面 initWithCoder: isAccessibilityElement :

    - (id)initWithCoder:(NSCoder *)coder {
        self = [super initWithCoder:coder];
    
        if (self != nil) {
            self.isAccessibilityElement = YES;
        }
    
        return self;
    }
    

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

    感谢您提供了这个很棒的解决方案。