我有一个应用程序与今日小工具.所以我想对它进行一些UI测试.
@H_301_2@我找到了打开今日/通知面板的方法.看起来很简单:
let statusBar = XCUIApplication().statusBars.elementBoundByIndex(0) statusBar.swipeDown()@H_301_2@但后来我找不到办法做一些有用的事情.可以在“今日/通知”面板中记录UI交互,但此类代码无法重现我的操作.
解决方法
测试扩展有类似的问题.我发现你必须做的就是点击元素在屏幕上的位置而不是元素本身,以便驱动交互.我没有用你的场景测试过这个,但我还没有找到任何通过这种方法无法访问的东西.
@H_301_2@这是一个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()@H_301_2@通过获取超视图和子视图的框架,您可以计算元素应在屏幕上的位置.请注意,coordinateWithNormalizedOffset采用范围[0,1]中的向量,而不是帧或像素偏移.在坐标上点击元素本身也不起作用,因此您必须点击superview / XCUIApplication()层. @H_301_2@更一般化的例子:
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()@H_301_2@如果您需要访问Springboard层并转到应用程序之外,您可以使用以下命令:
let springboard = XCUIApplication(privateWithPath: nil,bundleID: "com.apple.springboard") springboard.resolve()@H_301_2@但是您需要使用Objective-C公开一些私有的XCUITest方法:
@interface XCUIApplication (Private) { - (id)initPrivateWithPath:(id)arg1 bundleID:(id)arg2; } @interface XCUIElement (Private) { - (void) resolve; }