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

时间:2021-01-18 20:39:35

I am trying to replicate a pull to refresh on a UITableView using the new Xcode UI testing framework in Xcode 7 (beta 3)

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

My current approach is dragging from the table to whatever element below the table I can find. This works when there is a fixed item below the table like a UIToolbar or UITabBar I would rather not rely on having a UITabBar or UIToolbar but I can't figure out a way to do the pull to refresh/drag action without using the method in XCUIElement.

我目前的做法是从表格拖到我能找到的表格下面的任何元素。如果UIToolbar或UITabBar下面有一个固定的项目,我觉得这不行,我宁愿不依赖于UITabBar或UIToolbar,但我无法想办法在不使用方法的情况下进行刷新/拖动操作XCUIElement。

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

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

But it fails when I don't have a toolbar/tabbar and try to drag using the cells

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

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

This is the relevant portion of my code:

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

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()
}

Additional failed attempts:

其他尝试失败:

  • swipeDown() (multiples times as well)
  • swipeDown()(也是倍数)
  • scrollByDeltaX/deltaY (OS X only)
  • scrollByDeltaX / deltaY(仅限OS X)

2 个解决方案

#1


18  

You can use the XCUICoordinate API to interact with specific points on the screen, not necessarily tied to any particular element.

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

  1. Grab a reference to the first cell in your table. Then create a coordinate with zero offset, CGVectorMake(0, 0). This will normalize a point right on top of the first cell.
  2. 获取对表中第一个单元格的引用。然后创建一个零偏移坐标CGVectorMake(0,0)。这将使第一个单元格顶部的点正常化。
  3. Create an imaginary coordinate farther down the screen. (I've found that a dy of six is the smallest amount needed to trigger the pull-to-refresh gesture.)
  4. 在屏幕下方创建一个虚拟坐标。 (我发现六分之一是触发拉动刷新手势所需的最小量。)
  5. Execute the gesture by grabbing the first coordinate and dragging it to the second one.
  6. 通过抓住第一个坐标并将其拖动到第二个坐标来执行手势。

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

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 along with a working sample app is also available.

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

#2


4  

I managed to do such tasks with coordinates like Joe suggested. But i went a step further using coordinateWithOffset()

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

Now i am able to drag from a specific point to another specific point. I implemented a custom refresh on some of my views. While implementing this, i also discovered that i can even use this to access the control center or the top menu.

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

#1


18  

You can use the XCUICoordinate API to interact with specific points on the screen, not necessarily tied to any particular element.

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

  1. Grab a reference to the first cell in your table. Then create a coordinate with zero offset, CGVectorMake(0, 0). This will normalize a point right on top of the first cell.
  2. 获取对表中第一个单元格的引用。然后创建一个零偏移坐标CGVectorMake(0,0)。这将使第一个单元格顶部的点正常化。
  3. Create an imaginary coordinate farther down the screen. (I've found that a dy of six is the smallest amount needed to trigger the pull-to-refresh gesture.)
  4. 在屏幕下方创建一个虚拟坐标。 (我发现六分之一是触发拉动刷新手势所需的最小量。)
  5. Execute the gesture by grabbing the first coordinate and dragging it to the second one.
  6. 通过抓住第一个坐标并将其拖动到第二个坐标来执行手势。

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

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 along with a working sample app is also available.

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

#2


4  

I managed to do such tasks with coordinates like Joe suggested. But i went a step further using coordinateWithOffset()

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

Now i am able to drag from a specific point to another specific point. I implemented a custom refresh on some of my views. While implementing this, i also discovered that i can even use this to access the control center or the top menu.

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