我正在尝试使用 Xcode 7 (beta 3) 中的新 Xcode UI 测试框架在 UITableView
上复制拉动刷新
我目前的方法是从表格拖动到我能找到的表格下方的任何元素。当表格下方有一个固定项(如 UIToolbar
或 UITabBar
)时,此方法有效 我不想依赖 UITabBar
或 UIToolbar
但我想不出不使用 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,不一定与任何特定元素相关联。
- 获取对表格中第一个单元格的引用。然后创建一个零偏移坐标,
CGVectorMake(0, 0)
。这将规范化第一个单元格顶部的点。 - 在屏幕下方创建一个假想坐标。 (我发现
dy
为 6 是触发下拉刷新手势所需的最小量。) - 通过捕获第一个坐标并将其拖动到第二个坐标来执行手势。
let firstCell = app.staticTexts["Adrienne"]
let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 6))
start.pressForDuration(0, thenDragToCoordinate: finish)
关于ios - 在 XCTest UI 测试中复制拉动刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301798/