6

I want to test a button's click behavior. When executing button.tap(), the test fails.

XCTContext.runActivity(named: "Validate reply click") { (activity) in
    let button = App.buttons.matching(identifier: "Reply-ok").firstMatch
    button.tap()
}

Error message: Failed to synthesize event: Failed to compute hit point for Button, identifier: 'Reply-ok', label: 'Reply 1: ok.': Accessibility error kAXErrorInvalidUIElement from AXUIElementCopyMultipleAttributeValues for 2062, 2021, 2123

Tried solution:

  1. Change tap to forceTap
    func forceTapElement(element: XCUIElement) {
        msleep(milliSeconds: 1000)

        if self.isHittable {
            self.tap()
        }
        else {
            let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0)).withOffset(CGVector(dx: element.frame.origin.x, dy: element.frame.origin.y))
            coordinate.tap()
        }
    }
  1. Check if the button exists or hittable
XCTContext.runActivity(named: "Validate reply click") { (activity) in
    let button = App.buttons.matching(identifier: "Reply-ok").firstMatch
    if button.exists, button.isHittable {
        button.tap()
    }
}

Neither solution worked, I still get the same error. Any idea why the error appears and how to resolve this?

ArgenBarbie
  • 571
  • 1
  • 4
  • 10

1 Answers1

0

The problem with your forceTapElement is that in some cases you'll get an error in the 4th line, because isHittable can fail.

Try to use this extension

extension XCUIElement {
    func tapUnhittable() {
        XCTContext.runActivity(named: "Tap \(self) by coordinate") { _ in
            coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()
        }
    }
}
Roman Zakharov
  • 2,185
  • 8
  • 19
  • Why we need to use the coordinate even if the button exists and hittable? – ArgenBarbie Jul 07 '20 at 09:31
  • Use the default `tap()` method wherever is possible. When you face some problems, use `tapUnhittable()` and check if it still available to VoiceOver and VoiceControl users. When you use `forceTapElement()` from your question, you can miss the moment when some buttons became unavailable to the default tap. – Roman Zakharov Jul 07 '20 at 10:33
  • 1
    @RomanZakharov I am also facing the same problem and this solution isn't working for me. I am getting following error. kAXErrorInvalidUIElement from AXUIElementCopyMultipleAttributeValues for 2021, 2123 – neeraj joshi Jun 23 '21 at 18:38