我正在使用 XCode 进行 UITest。我有多个 CollectionView 单元格。
当我在 collectionView 中执行计数时,它会显示一定的计数。
我可以访问前两个单元格,但以 3 的形式访问第三个单元格(取决于设备大小)。它说我在 3rd Cell 中寻找的特定按钮 exists
。
但是 isHittable
是 false。
有什么方法可以点击 3rd Cell 上的按钮。
我尝试使用在线提供的 forceTapElement()
扩展,但没有帮助。
使用的扩展名:
extension XCUIElement{
func forceTapElement(){
if self.isHittable{
self.tap()
}else{
let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: .zero)
coordinate.tap()
}
}
}
尝试执行 swipeUp() 并访问按钮。它仍然显示 isHittable
为 false
最佳答案
我发现的唯一方法是向上滑动直到 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图书馆并尝试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)
}
关于swift - 如何点击屏幕上不可见的 CollectionView 单元格内的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806462/