【问题标题】:Replicate pull to refresh in XCTest UI testing在 XCTest UI 测试中复制拉动刷新
【发布时间】:2015-09-26 21:39:38
【问题描述】:

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

我目前的方法是从表格拖到表格下方我能找到的任何元素。当表格下方有一个固定项目(如UIToolbarUITabBar)时,此方法有效/拖动动作,不使用XCUIElement中的方法。

func pressForDuration(duration: NSTimeInterval, thenDragToElement otherElement: XCUIElement)

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

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

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)

【问题讨论】:

  • 对于那些已经读到这里的人,my answer 适用于 Xcode 7 和 Xcode 7.1。

标签: ios swift xctest xcode7 xcode-ui-testing


【解决方案1】:

您可以使用XCUICoordinate API 与屏幕上的特定点进行交互,而不必绑定到任何特定元素。

  1. 获取对表中第一个单元格的引用。然后创建一个零偏移坐标CGVectorMake(0, 0)。这将使第一个单元格正上方的点标准化。
  2. 在屏幕下方创建一个假想坐标。 (我发现dy 6 是触发下拉刷新手势所需的最小量。)
  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)

More informationworking sample app 也可用。

【讨论】:

  • 谢谢。对我来说,它适用于模拟器,但不适用于真正的 iPhone 6。
  • 这在 Xcode 9b5 中是否适合您?我在 Xcode 7 和 8 中广泛使用了它,但现在它似乎被破坏了。
  • 让 start = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0)) 让完成 = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 6)) 开始。按(forDuration:0,然后DragTo:完成)@ChaseHolland
  • 关于这个stackoverflow.com/q/65635857的任何提示
【解决方案2】:

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

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

【讨论】:

  • 嗨,Tomte,如果我想切换控制中心,elementToSwipeOn 的值是多少?
  • @BillChan 尝试获取主视图的高度(或底部的最后一个 y 点)并向其添加 10 或其他值。这意味着,滑动将在屏幕外开始。但是您只需向上滑动即可实现此目的。此代码旨在用于滑动并按住(例如,用于刷新 tableview)。
【解决方案3】:

这是一个 Swift 5.1 版本

    let firstCell = staticTexts["Id"]
    let start = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
    let finish = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 100))
    start.press(forDuration: 0, thenDragTo: finish)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多