如何点击屏幕上不可见的 CollectionView 单元格内的按钮

How to tap on a button inside a CollectionView cell which is not visible on screen

我正在使用 XCode 进行 UITest。我有多个 CollectionView 单元格。 当我在 collectionView 中执行计数时,它会显示一定的计数。 我可以访问前两个单元格,但以 3 的形式访问第三个单元格(取决于设备大小)。它说我在 3rd Cell 中寻找的特定按钮为 exists

但是isHittable是错误的。

有什么方法可以点击 3rd Cell 上的按钮。 我试过使用在线提供的 forceTapElement() 的扩展名,但没有用。

使用的扩展名:

extension XCUIElement{
    func forceTapElement(){
        if self.isHittable{
            self.tap()
        }else{
            let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: .zero)
            coordinate.tap()
        }
    }
}

试图执行 swipeUp() 并访问按钮。它仍然显示 isHittablefalse

我发现的唯一方法是向上滑动,直到 isHittable 变为 true

app.collectionViews.cells.staticTexts["TEST"].tap()
Thread.sleep(forTimeInterval: 3)

let collectionView = app.otherElements.collectionViews.element(boundBy: 0)
let testAds = collectionView.cells
let numberOfTestAds = testAds.count
if numberOfTestAds > 0 {
    let tester = collectionView.cells.element(boundBy: 2).buttons["ABC"]
    for _ in 0..<100 {
        guard !tester.isHittable else {
            break;
        }
        collectionView.swipeUp()
    }
}

请注意,swipeUp() 方法只会移动几个像素。如果你想使用更全面的方法,你可以得到 AutoMate library and try swipe(to:untilVisible:times:avoid:from:):

app.collectionViews.cells.staticTexts["TEST"].tap()
Thread.sleep(forTimeInterval: 3)

let collectionView = app.otherElements.collectionViews.element(boundBy: 0)
let testAds = collectionView.cells
let numberOfTestAds = testAds.count
if numberOfTestAds > 0 {
    let tester = collectionView.cells.element(boundBy: 2).buttons["ABC"]
    collectionView.swipe(to: .down, untilVisible: tester)
    // or swipe max 100 times in case 10 times is not enough
    // collectionView.swipe(to: .down, untilVisible: tester, times: 100)
}