Skip to content

Instantly share code, notes, and snippets.

@pilot34
Created April 24, 2018 09:22
Show Gist options
  • Save pilot34/0eb347eaafbf451dc6a22c352efafaf8 to your computer and use it in GitHub Desktop.
Save pilot34/0eb347eaafbf451dc6a22c352efafaf8 to your computer and use it in GitHub Desktop.
private func coordinate(forWebViewElement element: XCUIElement) -> XCUICoordinate? {
// wait for element to appear before searching
wait(forWebViewElement: element)
// parse description to find its frame
let descr = element.firstMatch.debugDescription
guard let rangeOpen = descr.range(of: "{{", options: [.backwards]),
let rangeClose = descr.range(of: "}}", options: [.backwards]) else {
return nil
}
let frameStr = String(descr[rangeOpen.lowerBound..<rangeClose.upperBound])
let rect = CGRectFromString(frameStr)
// get the center of rect
let center = CGVector(dx: rect.midX, dy: rect.midY)
let coordinate = XCUIApplication().coordinate(withNormalizedOffset: .zero).withOffset(center)
return coordinate
}
func tap(onWebViewElement element: XCUIElement) {
// xcode has bug, so we cannot directly access webViews XCUIElements
// as workaround we can check debugDesciption, find frame and tap by coordinate
let coord = coordinate(forWebViewElement: element)
coord?.tap()
}
func exists(webViewElement element: XCUIElement) -> Bool {
return coordinate(forWebViewElement: element) != nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment