ios - 在 XCTest UI 测试中复制拉动刷新

标签 ios swift xctest xcode7 xcode-ui-testing

我正在尝试使用 Xcode 7 (beta 3) 中的新 Xcode UI 测试框架在 UITableView 上复制拉动刷新

我目前的方法是从表格拖动到我能找到的表格下方的任何元素。当表格下方有一个固定项(如 UIToolbarUITabBar)时,此方法有效 我不想依赖 UITabBar UIToolbar 但我想不出不使用 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)

最佳答案

您可以使用 XCUICoordinate与屏幕上特定点交互的 API,不一定与任何特定元素相关联。

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

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)

More information连同 working sample app也可用。

关于ios - 在 XCTest UI 测试中复制拉动刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301798/

相关文章:

ios - Xcode 5 表示测试失败但显示绿色复选标记

ios - 找不到有关 forTraitCollection 的苹果文档

swift - 在 Swift 3 中将数据拆分为 block

ios - #selector 指令在 Xcode 7.3 中的使用

swift - 谷歌地图不显示当前位置或定位按钮

ios - 没有二维数组的快速 UICollectionView 部分分离

swift - 在导航 Controller 中测试 topviewcontroller

ios - 用相同的测试测试两个目标

ios - iPhone 应用程序在运行 iOS 5.0 的 iPad 上运行时可帮助接收错误代码 3

ios - 在 iOS 开发门户中创建配置文件时未显示推送证书