我有一个应用程序与今日小工具.所以我想对它进行一些UI测试.
我找到了打开今日/通知面板的方法.看起来很简单:
let statusBar = XCUIApplication().statusBars.elementBoundByIndex(0) statusBar.swipeDown()
但后来我找不到办法做一些有用的事情.可以在“今日/通知”面板中记录UI交互,但此类代码无法重现我的操作.
解决方法
测试扩展有类似的问题.我发现你必须做的就是点击元素在屏幕上的位置而不是元素本身,以便驱动交互.我没有用你的场景测试过这个,但我还没有找到任何通过这种方法无法访问的东西.
这是一个Swift示例,在Springboard上点击“X”按钮获取应用程序图标,同样无法通过典型的交互点击:
let iconFrame = icon.frame // App icon on the springboard let springboardFrame = springboard.frame // The springboard (homescreen) icon.pressForDuration(1.3) // tap and hold // Tap the little "X" button at approximately where it is. The X is not exposed directly springboard.coordinateWithNormalizedOffset(CGVectorMake((iconFrame.minX + 3) / springboardFrame.maxX,(iconFrame.minY + 3) / springboardFrame.maxY)).tap()
通过获取超视图和子视图的框架,您可以计算元素应在屏幕上的位置.请注意,coordinateWithNormalizedOffset采用范围[0,1]中的向量,而不是帧或像素偏移.在坐标上点击元素本身也不起作用,因此您必须点击superview / XCUIApplication()层.
更一般化的例子:
let myElementFrame = myElement.frame let appFrame = XCUIApplication().frame let middleOfElementVector = CGVectorMake(iconFrame.midX / appFrame.maxX,iconFrame.midY / appFrame.maxY) // Tap element from the app-level at the given coordinate XCUIApplication().coordinateWithNormalizedOffset(middleOfElementVector).tap()
如果您需要访问Springboard层并转到应用程序之外,您可以使用以下命令:
let springboard = XCUIApplication(privateWithPath: nil,bundleID: "com.apple.springboard") springboard.resolve()
但是您需要使用Objective-C公开一些私有的XCUITest方法:
@interface XCUIApplication (Private) { - (id)initPrivateWithPath:(id)arg1 bundleID:(id)arg2; } @interface XCUIElement (Private) { - (void) resolve; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。