在XCTest UI测试中复制pull以刷新

Kev*_*vin 25 ios xctest swift xcode7 xcode-ui-testing

我试图UITableView在Xcode 7(beta 3)中使用新的Xcode UI测试框架复制一个pull来刷新

我目前的方法是从表格拖到我能找到的表格下面的任何元素.这可以在表格下面有一个固定项目时使用,UIToolbar或者UITabBar我宁愿不依赖于具有UITabBarUIToolbar但我无法找到一种方法来执行拉动以刷新/拖动动作而不使用该方法XCUIElement.

func pressForDuration(duration: NSTimeInterval, thenDragToElement otherElement: XCUIElement)
Run Code Online (Sandbox Code Playgroud)

刷新成功

但是当我没有工具栏/标签栏并尝试使用单元格拖动时,它会失败

刷新失败

这是我的代码的相关部分:

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()
}
Run Code Online (Sandbox Code Playgroud)

其他尝试失败:

  • swipeDown() (也是倍数)
  • scrollByDeltaX/deltaY (仅限OS X)

Joe*_*tti 20

您可以使用XCUICoordinateAPI与屏幕上的特定点进行交互,而不必与任何特定元素相关联.

  1. 获取对表中第一个单元格的引用.然后创建一个零偏移的坐标CGVectorMake(0, 0).这将使第一个单元格顶部的点正常化.
  2. 在屏幕下方创建一个虚拟坐标.(我发现dy六个中的六个是触发拉动刷新手势所需的最小量.)
  3. 通过抓住第一个坐标并将其拖动到第二个坐标来执行手势.

在此输入图像描述

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

还提供了更多信息以及工作示例应用程序.


Tom*_*mte 5

我设法用乔建议的坐标来完成这些任务。但我更进一步使用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)
Run Code Online (Sandbox Code Playgroud)

现在我可以从一个特定的点拖动到另一个特定的点。我对我的一些视图实施了自定义刷新。在执行此操作时,我还发现我什至可以使用它来访问控制中心或顶部菜单。