如何在Xcode UI测试脚本中执行点击和拖动操作?

时间:2015-12-08 19:44:54

标签: testing xcode7 xcode-ui-testing

例如, 我有一个草图垫应用程序,我想通过绘制它来测试。我想指定一个(x,y)坐标,然后点击并拖动到另一个(x,y)坐标。

这在Xcode UI测试中是否可行?

Joe为此问题提供了解决方案: 使用目标C,我的代码看起来像这样:

XCUICoordinate *start = [element2 coordinateWithNormalizedOffset:CGVectorMake(0, 0)];
XCUICoordinate *finish = [element2 coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
[start pressForDuration:0 thenDragToCoordinate:finish];

其中element2是我必须执行拖动的XCUIElement。

2 个答案:

答案 0 :(得分:24)

您可以使用XCUICoordinate在UI测试中点按并拖动元素。以下是通过表格单元坐标的how to pull to refresh示例。

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

如果您没有要引用的元素,则应该能够基于XCUIApplication创建任意坐标。

let app = XCUIApplication()
let fromCoordinate = app.coordinateWithNormalizedOffset(CGVector(dx: 0, dy: 10))
let toCoordinate = app.coordinateWithNormalizedOffset(CGVector(dx: 0, dy: 20))
fromCoordinate.pressForDuration(0, thenDragToCoordinate: toCoordinate)

UI测试没有官方文档,但如果您对更多详细信息感兴趣,我已经删除了标题并汇总了XCTest Reference

答案 1 :(得分:1)

或者从Swift 4开始:

let view = window.staticTexts["FooBar"]
let start = view.coordinate(withNormalizedOffset: CGVector(dx: 10, dy: 20))
let finish = view.coordinate(withNormalizedOffset: CGVector(dx: 100, dy: 80))
start.press(forDuration: 0.01, thenDragTo: finish)
相关问题