Ios 在XCTest UI测试中复制拉入刷新

Ios 在XCTest UI测试中复制拉入刷新,ios,swift,xctest,xcode7,xcode-ui-testing,Ios,Swift,Xctest,Xcode7,Xcode Ui Testing,我正在尝试使用Xcode 7(beta 3)中新的Xcode UI测试框架在UITableView上复制一个pull刷新 我目前的方法是从表中拖动到表下我能找到的任何元素。当表下面有一个固定项时,如UIToolbar或uitabar我不希望依赖于有一个uitabar或UIToolbar,但如果不使用xguielement中的方法,我就无法找到执行拉刷新/拖动操作的方法 func pressForDuration(duration: NSTimeInterval, thenDragToElemen

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

我目前的方法是从表中拖动到表下我能找到的任何元素。当表下面有一个固定项时,如
UIToolbar
uitabar
我不希望依赖于有一个
uitabar
UIToolbar
,但如果不使用
xguielement
中的方法,我就无法找到执行拉刷新/拖动操作的方法

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)
您可以使用API与屏幕上的特定点进行交互,而不必绑定到任何特定元素

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

  • 还有一个也可用。

    我设法用乔建议的坐标完成了这些任务。但是我使用了
    coordinateWithOffset()


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

    这是一个Swift 5.1版本

    let firstCell=statictext[“Id”]
    
    让start=firstCell.coordinate(带有规格化偏移:CGVector(dx:0,dy:0)) 让finish=firstCell.coordinate(带有规格化偏移:CGVector(dx:0,dy:100)) 开始。按(持续时间:0,然后拖动到:完成)
    对于那些已经阅读了本文的读者,可以在Xcode 7和Xcode 7.1中使用。谢谢。对我来说,它可以在模拟器上运行,但不能在真正的iPhone6上运行。这在Xcode 9b5中适用吗?我在Xcode 7和8中广泛使用了这个函数,但现在它似乎被破坏了。let start=firstCell.coordinate(使用normalizedoffset:CGVector(dx:0,dy:0))let finish=firstCell.coordinate(使用normalizedoffset:CGVector(dx:0,dy:6))start,如果我想切换控制中心,ElementToSwippen的值是多少?@BillChan尝试获取主视图的高度(或底部最后一个y点),并向其添加10或更多。这意味着,刷卡将从屏幕外开始。但您只需向上滑动即可实现这一点。此代码用于滑动和保持(例如,刷新表视图)。
    let firstCell = app.staticTexts["Adrienne"]
    let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
    let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 6))
    start.pressForDuration(0, thenDragToCoordinate: finish)
    
    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)