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

编程入门 行业动态 更新时间:2024-10-25 22:25:56
本文介绍了如何在 Xcode UI 测试脚本中执行点击和拖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

例如,我有一个素描板应用程序,我想通过在它上面绘图来测试.我想指定一个 (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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 22:52:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1401116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拖动   脚本   测试   如何在   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!