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

问题描述 投票:5回答:2

例如,我有一个草图垫应用程序,我想通过绘制它来测试。我想指定一个(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我必须执行拖动。

testing xcode7 xcode-ui-testing
2个回答
27
投票

您可以使用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
投票

或者从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)
© www.soinside.com 2019 - 2024. All rights reserved.