测试扩展也有类似的问题。我发现你必须做的是点击元素在屏幕上的位置而不是元素本身来驱动交互。我还没有在你的场景中测试过这个,但是我还没有发现任何无法通过这种方法点击的东西。
这是一个在 Springboard 上点击“X”按钮以获得应用图标的 Swift 示例,同样无法通过典型交互点击该图标:
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()
通过获取superview和subview的frame,你可以计算出元素应该在屏幕上的什么位置。请注意,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;
}