【问题标题】:XCUITest - Failed to synthesize event: Failed to compute hit point for ButtonXCUITest - 无法合成事件:无法计算 Button 的命中点
【发布时间】:2020-07-03 11:45:40
【问题描述】:

我想测试一个按钮的点击行为。执行button.tap()时,测试失败。

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

错误信息: 无法合成事件:无法计算 Button 的命中点,标识符:“Reply-ok”,标签:“Reply 1: ok.”:来自 AXUIElementCopyMultipleAttributeValues 的 2062、2021、2123 的可访问性错误 kAXErrorInvalidUIElement

尝试过的解决方案:

  1. 将 tap 改为 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. 检查按钮是否存在或可点击
XCTContext.runActivity(named: "Validate reply click") { (activity) in
    let button = App.buttons.matching(identifier: "Reply-ok").firstMatch
    if button.exists, button.isHittable {
        button.tap()
    }
}

这两种解决方案都不起作用,我仍然遇到同样的错误。知道为什么会出现错误以及如何解决这个问题吗?

【问题讨论】:

    标签: ios xcuitest


    【解决方案1】:

    forceTapElement 的问题在于,在某些情况下,您会在第 4 行出现错误,因为isHittable 可能会失败。

    尝试使用此扩展程序

    extension XCUIElement {
        func tapUnhittable() {
            XCTContext.runActivity(named: "Tap \(self) by coordinate") { _ in
                coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()
            }
        }
    }
    

    【讨论】:

    • 为什么即使按钮存在且可点击,我们也需要使用坐标?
    • 尽可能使用默认的tap() 方法。当您遇到一些问题时,请使用tapUnhittable() 并检查它是否仍可供 VoiceOver 和 VoiceControl 用户使用。当您在问题中使用forceTapElement() 时,您可能会错过某些按钮无法通过默认点击获得的时刻。
    • @RomanZakharov 我也面临同样的问题,这个解决方案对我不起作用。我收到以下错误。 kAXErrorInvalidUIElement 来自 AXUIElementCopyMultipleAttributeValues for 2021, 2123
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多