在XCTest UI测试中复制pull以刷新 [英] Replicate pull to refresh in XCTest UI testing

查看:109
本文介绍了在XCTest UI测试中复制pull以刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我当前的方法是从表中拖动到我能找到的表下面的任何元素。当表格下方有固定项目时,如 UIToolbar UITabBar 我宁愿不依赖于 UITabBar UIToolbar 但我无法找到一种方法来执行拉动以刷新/拖动动作而不使用 XCUIElement 中的方法。

  func pressForDuration(持续时间:NSTimeInterval,thenDragToElement otherElement :XCUIElement)



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





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

  func testRefresh(){
//用于测试单元格
for _ in 0 ... 9 {
addCell()
}
refreshTable()
}

func refreshTable(var tbl :XCUIElem耳鼻喉科? = 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(找不到表格要刷新,你可以明确地提供你是否有一个表)
返回
}

var topElement = table
let bottomElement:XCUIElement?

//尝试拖动到标签栏然后拖到工具栏,然后拖到最后一个单元格
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
}
}
如果让dragTo = bottomElement {
topElement.pressForDuration(0.1,thenDragToElement:dragTo)
}
}

func addCell(){
let app = XCUIApplication()
app.navigationBars [Master]。buttons [Add]。tap()
}

其他失败尝试:




  • swipeDown()(也是倍数)

  • scrollByDeltaX / deltaY (OS X只有)


解决方案

您可以使用

 让firstCell = app.staticTexts [Adrienne] 
let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0,6))
start.pressForDuration(0,thenDragToCoordinate:finish)

更多信息以及工作示例应用也可用。


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

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.

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

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

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)
  • scrollByDeltaX/deltaY (OS X only)

解决方案

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

  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. 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.)
  3. Execute the gesture by grabbing the first coordinate and dragging it to the second one.

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.

这篇关于在XCTest UI测试中复制pull以刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆