Unable to tap (x,y) coordinate in landscape mode

夙愿已清 提交于 2019-12-04 12:34:52

Same issue — screen points are correct but the actual gesture coordinates are messed up. This workaround did the trick for me:

class SmartXCUICoordinate
{
    let element: XCUIElement
    let normalizedOffset: CGVector

    init(element: XCUIElement, normalizedOffset offset: CGVector) {
        self.element = element
        self.normalizedOffset = offset
    }

    var realCoordinate: XCUICoordinate {
        guard XCUIDevice.shared().orientation.isLandscape else {
            return element.coordinate(withNormalizedOffset: normalizedOffset)
        }

        let app = XCUIApplication()
        _ = app.isHittable // force new UI hierarchy snapshot

        let screenPoint = element.coordinate(withNormalizedOffset: normalizedOffset).screenPoint

        let portraitScreenPoint = XCUIDevice.shared().orientation == .landscapeLeft
            ? CGVector(dx: app.frame.width - screenPoint.y, dy: screenPoint.x)
            : CGVector(dx: screenPoint.y, dy: app.frame.height - screenPoint.x)

        return app
            .coordinate(withNormalizedOffset: CGVector.zero)
            .withOffset(portraitScreenPoint)
    }

    func tap() {
        realCoordinate.tap()  // wrap other XCUICoordinate methods as needed
    }
}

extension XCUIElement
{
    func smartCoordinate(withNormalizedOffset normalizedOffset: CGVector) -> SmartXCUICoordinate {
        return SmartXCUICoordinate(element: self, normalizedOffset: normalizedOffset)
    }
}

Known issue: doesn't work for orientations that your app does not support.

Excellent answer. I had the same coordinate problem testing a [Xcode 8 Swift 3] SpriteKit landscape mode app which I initially incorrectly chalked up to SpriteKit. SmartXCUICoordinate got me back on track.

I added two methods under your comment // wrap other XCUICoordinate methods as needed

//support UILongPressGestureRecognizer
func longPress(){
    realCoordinate.press(forDuration: 2.25)
}
//support drag an element to a second element
//For SpriteKit this drags one SKNode to a second SKNode)
func pressThenDragTo(element:  XCUIElement){
    realCoordinate.press(forDuration: 0.1, thenDragTo: element.smartCoordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).realCoordinate)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!