XCUITest - 无法合成事件:无法计算按钮的命中点



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

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

错误信息: 无法合成事件:无法计算按钮的命中点,标识符:"回复正常",标签:"回复 1:正常"。 :辅助功能错误 kAXErrorInvalidUIElement 来自 AXUIElementCopyMultipleAttributeValues for 2062、2021、2123

尝试的解决方案:

  1. 将点击更改为强制点击
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()
}
}

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

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()
}
}
}

最新更新