In Xcode 8 / Swift 3, using the coordinate(withNormalizedOffset: CGVector) function to interact with an XCUIElement appears to work only in portrait mode.
To test this functionality, I created a single screen project with a button centered in the view. I then ran the following UI test:
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()
}
This successfully taps the button. However, if I run the same test in landscapeLeft or landscapeRight, the button is not tapped. Printing the coordinate's screenpoint reveals that it is located inside the button's frame in both portrait and landscape modes.
Identical logic is successful for all orientations in 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()
}
Am I missing something, or is this a legitimate framework bug? Does it have something to do with the transition from CGVectorMake in Swift 2 to CGVector(dx: dy:) in Swift 3?
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
Same issue — screen points are correct but the actual gesture coordinates are messed up. This workaround did the trick for me:
Known issue: doesn't work for orientations that your app does not support.