无法在横向模式下点击 (x,y) 坐标

ted*_*ock 6 ios-ui-automation xcode-ui-testing swift3 xcode8

在 Xcode 8 / Swift 3 中,使用坐标(withNormalizedOffset: CGVector)函数与 XCUIElement 交互似乎只能在纵向模式下工作。

为了测试此功能,我创建了一个单屏项目,其中一个按钮位于视图中央。然后我运行了以下 UI 测试:

func testExample() {

    XCUIDevice.shared().orientation = .portrait

    let window = XCUIApplication().windows.element(boundBy: 0)

    let centerPoint = window.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))

    centerPoint.tap()
}
Run Code Online (Sandbox Code Playgroud)

这成功地点击了按钮。但是,如果我在 LandscapeLeft 或 LandscapeRight 中运行相同的测试,则不会点击该按钮。打印坐标的屏幕点显示它在纵向和横向模式下都位于按钮的框架内。

对于 Xcode 7 / Swift 2 中的所有方向,相同的逻辑都是成功的:

func testExample() {

    XCUIDevice.sharedDevice().orientation = .LandscapeLeft

    let window = XCUIApplication().windows.elementBoundByIndex(0)

    let centerPoint = window.coordinateWithNormalizedOffset(CGVectorMake(0.5, 0.5))

    centerPoint.tap()
}
Run Code Online (Sandbox Code Playgroud)

我是否遗漏了什么,或者这是一个合法的框架错误?它与从 Swift 2 中的 CGVectorMake 到 Swift 3 中的 CGVector(dx: dy:) 的转换有关吗?

Gle*_* A. 3

同样的问题 \xe2\x80\x94 屏幕点是正确的,但实际的手势坐标是混乱的。这个解决方法对我来说很有效:

\n\n
class SmartXCUICoordinate\n{\n    let element: XCUIElement\n    let normalizedOffset: CGVector\n\n    init(element: XCUIElement, normalizedOffset offset: CGVector) {\n        self.element = element\n        self.normalizedOffset = offset\n    }\n\n    var realCoordinate: XCUICoordinate {\n        guard XCUIDevice.shared().orientation.isLandscape else {\n            return element.coordinate(withNormalizedOffset: normalizedOffset)\n        }\n\n        let app = XCUIApplication()\n        _ = app.isHittable // force new UI hierarchy snapshot\n\n        let screenPoint = element.coordinate(withNormalizedOffset: normalizedOffset).screenPoint\n\n        let portraitScreenPoint = XCUIDevice.shared().orientation == .landscapeLeft\n            ? CGVector(dx: app.frame.width - screenPoint.y, dy: screenPoint.x)\n            : CGVector(dx: screenPoint.y, dy: app.frame.height - screenPoint.x)\n\n        return app\n            .coordinate(withNormalizedOffset: CGVector.zero)\n            .withOffset(portraitScreenPoint)\n    }\n\n    func tap() {\n        realCoordinate.tap()  // wrap other XCUICoordinate methods as needed\n    }\n}\n\nextension XCUIElement\n{\n    func smartCoordinate(withNormalizedOffset normalizedOffset: CGVector) -> SmartXCUICoordinate {\n        return SmartXCUICoordinate(element: self, normalizedOffset: normalizedOffset)\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

已知问题:不适用于您的应用不支持的方向。

\n