web-dev-qa-db-ja.com

XCTestUIテストでプルを複製して更新します

Xcode 7(beta 3)の新しいXcode UIテストフレームワークを使用して、プルを複製してUITableViewで更新しようとしています。

私の現在のアプローチは、テーブルからテーブルの下にある要素にドラッグすることです。これは、テーブルの下にUIToolbarUITabBarのような固定アイテムがある場合に機能します。UITabBarUIToolbarに依存したくないのですが、できます。 XCUIElementのメソッドを使用せずに、プルして更新/ドラッグアクションを実行する方法を理解していません。

_func pressForDuration(duration: NSTimeInterval, thenDragToElement otherElement: XCUIElement)
_

Refresh success

しかし、ツールバー/タブバーがなく、セルを使用してドラッグしようとすると失敗します

Refresh failure

これは私のコードの関連部分です:

_func testRefresh() {
    //For testing cell
    for _ in 0...9 {
        addCell()
    }
    refreshTable()
}

func refreshTable(var tbl: XCUIElement? = nil) {
    let app = XCUIApplication()

    if tbl == nil {
        let tables = app.tables
        if tables.count > 0 {
            tbl = tables.elementAtIndex(0)
        }
    }

    guard let table = tbl else {
        XCTFail("Cannot find a table to refresh, you can provide on explicitly if you do have a table")
        return
    }

    var topElement = table
    let bottomElement: XCUIElement?

    //Try to drag to a tab bar then to a toolbar then to the last cell
    if app.tabBars.count > 0 {
        bottomElement = app.tabBars.elementAtIndex(0)
    }
    else if app.toolbars.count > 0 {
        bottomElement = app.toolbars.elementAtIndex(0)
    }
    else {
        let cells = app.cells
        if cells.count > 0 {
            topElement = cells.elementAtIndex(0)
            bottomElement = cells.elementAtIndex(cells.count - 1)
        }
        else {
            bottomElement = nil
        }
    }
    if let dragTo = bottomElement {
        topElement.pressForDuration(0.1, thenDragToElement: dragTo)
    }
}

func addCell() {
    let app = XCUIApplication()
    app.navigationBars["Master"].buttons["Add"].tap()
}
_

追加の失敗した試行:

  • swipeDown()(複数回も)
  • _scrollByDeltaX/deltaY_(OS Xのみ)
25
Kevin

XCUICoordinate APIを使用して、画面上の特定のポイントと対話できます。必ずしも特定の要素に関連付けられている必要はありません。

  1. テーブルの最初のセルへの参照を取得します。次に、オフセットがゼロの座標CGVectorMake(0, 0)を作成します。これにより、最初のセルの真上のポイントが正規化されます。
  2. 画面のさらに下に架空の座標を作成します。 (6のdyが、プルトゥリフレッシュジェスチャをトリガーするために必要な最小量であることがわかりました。)
  3. 最初の座標を取得し、それを2番目の座標にドラッグして、ジェスチャを実行します。

enter image description here

let firstCell = app.staticTexts["Adrienne"]
let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 6))
start.pressForDuration(0, thenDragToCoordinate: finish)

詳細情報 と一緒に 作業サンプルアプリ も利用できます。

20
Joe Masilotti

私はジョーが提案したような座標でそのようなタスクを実行することができました。しかし、私はcoordinateWithOffset()を使用してさらに一歩進んだ

let startPosition = CGPointMake(200, 300)
let endPosition = CGPointMake(200, 0)
let start = elementToSwipeOn.coordinateWithNormalizedOffset(CGVectorMake(0, 0)).coordinateWithOffset(CGVector(dx: startPosition.x, dy: startPosition.y))
let finish = elementToSwipeOn.coordinateWithNormalizedOffset(CGVector(dx: 0, dy: 0)).coordinateWithOffset(CGVector(dx: endPosition.x, dy: endPosition.y))
start.pressForDuration(0, thenDragToCoordinate: finish)

これで、特定のポイントから別の特定のポイントにドラッグできるようになりました。一部のビューにカスタム更新を実装しました。これを実装しているときに、これを使用してコントロールセンターやトップメニューにアクセスできることも発見しました。

4
Tomte