2017-05-05 48 views
0

我正在使用XCode編寫UITests。我有多個CollectionView單元格。 當我在collectionView中執行Count時,它顯示了一定的計數。 我能夠訪問前兩個單元,但是以3爲單位來訪問第三個單元(取決於設備的大小)。它說我正在尋找第三個細胞的特定按鈕,如exists如何點擊屏幕上不可見的CollectionView單元格內的按鈕

但是isHittable是錯誤的。

有沒有什麼辦法可以點擊第三個單元上的按鈕。 我曾嘗試使用網絡上可用的forceTapElement()的擴展名,但沒有幫助。

擴展使用:

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

嘗試執行swipeUp()和訪問按鈕。它仍然顯示isHittablefalse

回答

2

我發現的唯一的辦法就是向上輕掃,直至出現isHittabletrue

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) 
} 
+0

謝謝@ Bartosz..But問題是,即使我執行swipeUp()它仍然顯示'isHittable'爲'FALSE' – user1642224

+0

你能分享這個問題的示例應用嗎?如果沒有,你可以檢查[AutoMate測試](https://github.com/PGSSoft/AutoMate/blob/master/AutoMateExample/AutoMateExampleUITests/Extensions%20tests/XCUIElementExtensionTests.swift#L162)滾動到收集視圖中的第10個單元格,直到它將可見(存在並且可擊中)。 –

+0

下面是示例代碼'app.collectionViews.cells.staticTexts [「TEST」]。tap() Thread.sleep(forTimeInterval:3) let testAds = app.otherElements.collectionViews.element(boundBy:0).cells let numberOfTestAds = testAds.count if numberOfTestAds> 0讓tester = app.otherElements.collectionViews.element(boundBy:0).cells.element(boundBy:2).buttons [「ABC」] for _in 0 .. <10 {guard!tester.isHittable else}其中{break; } Bump.swipeUp() } }' – user1642224

相關問題