Xcode UItesting – UItesting失败 – 点击search字段“取消”button时,无法滚动到可见(通过AX操作)

我试图通过点击search栏中的“取消”button来closuressearch栏。

testing用例未能find取消button。 它在Xcode 7.0.1中工作正常

我添加了谓词来等待button出现。 点击“取消”button,testing用例失败

let button = app.buttons[“Cancel”] let existsPredicate = NSPredicate(format: "exists == 1") expectationForPredicate(existsPredicate, evaluatedWithObject: button, handler: nil) waitForExpectationsWithTimeout(5, handler: nil) button.tap() // Failing here 

日志

  t = 7.21s Tap SearchField t = 7.21s Wait for app to idle t = 7.29s Find the SearchField t = 7.29s Snapshot accessibility hierarchy for com.test.mail t = 7.49s Find: Descendants matching type SearchField t = 7.49s Find: Element at index 0 t = 7.49s Wait for app to idle t = 7.55s Synthesize event t = 7.84s Wait for app to idle t = 8.97s Type 'vinayak@xmd.net' into t = 8.97s Wait for app to idle t = 9.03s Find the "Search" SearchField t = 9.03s Snapshot accessibility hierarchy for com.test.mail t = 9.35s Find: Descendants matching type SearchField t = 9.35s Find: Element at index 0 t = 9.36s Wait for app to idle t = 9.42s Synthesize event t = 10.37s Wait for app to idle t = 10.44s Check predicate `exists == 1` against object `"Cancel" Button` t = 10.44s Snapshot accessibility hierarchy for com.test.mail t = 10.58s Find: Descendants matching type Button t = 10.58s Find: Elements matching predicate '"Cancel" IN identifiers' t = 10.58s Tap "Cancel" Button t = 10.58s Wait for app to idle t = 10.64s Find the "Cancel" Button t = 10.64s Snapshot accessibility hierarchy for com.test.mail t = 10.78s Find: Descendants matching type Button t = 10.78s Find: Elements matching predicate '"Cancel" IN identifiers' t = 10.79s Wait for app to idle t = 11.08s Synthesize event t = 11.13s Scroll element to visible t = 11.14s Assertion Failure: UI Testing Failure - Failed to scroll to visible (by AX action) Button 0x7f7fcaebde40: traits: 8589934593, {{353.0, 26.0}, {53.0, 30.0}}, label: 'Cancel', error: Error -25204 performing AXAction 2003 

我想这里“取消”button返回false为可hittable属性,这是防止它的攻丝。

如果您在文档中看到tap() ,那就说明

 /*! * Sends a tap event to a hittable point computed for the element. */ - (void)tap; 

看起来事情已经被XCode 7.1破坏了。为了保持我自己(也是这样)解决这些问题,我在XCUIElement上写了一个扩展,允许点击元素,即使它不是可触发的。 以下可以帮助你。

 /*Sends a tap event to a hittable/unhittable element.*/ extension XCUIElement { func forceTapElement() { if self.hittable { self.tap() } else { let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0)) coordinate.tap() } } } 

现在你可以打电话给

 button.forceTapElement() 

更新 – 对于swift 3使用以下内容:

 extension XCUIElement { func forceTapElement() { if self.isHittable { self.tap() } else { let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx:0.0, dy:0.0)) coordinate.tap() } } } 

对我来说,根本原因是我想要挖掘的物体

  • 已被设置为隐藏(并返回)
  • 已被删除并重新附加

在这两种情况下, isAccessibilityElement属性都是false 。 把它重新设置成true固定它。