【问题标题】:How do I perform a tap and drag in Xcode UI Test scripts?如何在 Xcode UI 测试脚本中执行点击和拖动?
【发布时间】:2015-12-08 19:44:54
【问题描述】:

例如, 我有一个草图板应用程序,我想通过在上面绘图来测试它。我想指定一个(x,y)坐标并使其点击并拖动到另一个(x,y)坐标。

这在 Xcode UI 测试中可行吗?

Joe 为这个问题提供了一个解决方案: 使用 Objective 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


    【解决方案1】:

    您可以使用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 放在一起。

    【讨论】:

    • 谢谢乔!像魅力一样工作!我唯一遇到的问题是试图找到执行操作的元素。
    【解决方案2】:

    或从 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多