Skip to content

Instantly share code, notes, and snippets.

@garkhipov
Created November 19, 2016 10:58
Show Gist options
  • Save garkhipov/9b2bc64bfce0dd4299c522df16866822 to your computer and use it in GitHub Desktop.
Save garkhipov/9b2bc64bfce0dd4299c522df16866822 to your computer and use it in GitHub Desktop.
Workaround for XCUICoordinate in landscape
import XCUI
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment