在XCTest UItesting中复制拉来刷新

我正在尝试使用Xcode 7(testing版3)中新的Xcode UItesting框架来复制UITableView刷新操作,

我目前的做法是从桌子上拖动到我能find的桌子下面的任何元素。 这工作时有一个像UIToolbarUITabBar表下面的固定项目我宁愿不依赖于UITabBarUIToolbar但我不能找出一种方法来执行拉动刷新/拖动动作,而不使用该方法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)

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

  1. 获取表格中第一个单元格的引用。 然后创build一个零偏移坐标CGVectorMake(0, 0) 。 这将正常化第一个单元格顶部的一个点。
  2. 在屏幕下方创build一个虚坐标。 (我发现, dy的六是触发pull-to-refresh手势所需的最小量。)
  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) 

更多的信息,以及一个工作示例应用程序也可用。

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

现在我可以从一个特定点拖到另一个特定点。 我对我的一些观点实施了自定义刷新。 在实现这个的同时,我也发现我甚至可以使用它来访问控制中心或顶级菜单。