如何在 Xcode UI 测试脚本中执行点击和拖动? [英] How do I perform a tap and drag in Xcode UI Test scripts?

查看:31
本文介绍了如何在 Xcode UI 测试脚本中执行点击和拖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For example, I have a sketch pad app that I want to test by drawing on it. I'd like to specify an (x,y) coordinate and make it tap and drag to another (x,y) coordinate.

这在 Xcode UI 测试中可行吗?

Is this possible in Xcode UI Tests?

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

Joe provided a solution to this issue: Using objective C, my code looked something like this:

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

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

where element2 was the XCUIElement I had to perform the drag on.

推荐答案

您可以使用 XCUICoordinate 在 UI 测试中点击和拖动元素.这是如何拉刷新 通过表格单元格坐标.

You can use XCUICoordinate to tap and drag elements in UI Testing. Here is an example of how to pull to refresh via table cell coordinates.

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 创建任意坐标.

If you don't have elements to reference you should be able to create arbitrary coordinates based off of 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 参考放在一起,如果你对更多细节感兴趣.

UI Testing doesn't have official documentation, but I've scraped the headers and put together an XCTest Reference if you are interested in more details.

这篇关于如何在 Xcode UI 测试脚本中执行点击和拖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆