Xcode UI 测试 - UI 测试失败 - 当点击搜索字段 "Cancel' 按钮时无法滚动到可见(通过 AX 操作)

标签 xcode xcode7 xcode-ui-testing

我试图通过点击搜索栏中的“取消”按钮来关闭搜索字段。

测试用例找不到取消按钮。它在 Xcode 7.0.1 中运行良好

我添加了谓词来等待按钮出现。当我们点击“取消”按钮时,测试用例失败

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

最佳答案

我猜这里“取消”按钮会为 hittable 属性返回 false,这会阻止它点击。

如果您在文档中看到 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()
        }
    }
}

关于Xcode UI 测试 - UI 测试失败 - 当点击搜索字段 "Cancel' 按钮时无法滚动到可见(通过 AX 操作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422681/

相关文章:

iphone - 使用 NSUserDefault : Good or Bad idea? 保存游戏进度

iOS:使用客户的配置文件签署内部分发的应用程序

ios - Swift 2、Xcode 7.2 'NSInternalInconsistencyException

iOS 用户界面测试 : Get image filename (Swift)

swift - Xcode 8 中没有 IOS 10 模拟器

ios - 删除 UIPageViewController 中的循环 AVPlayer Observer

ios - 如何在 CorePlot - 堆积条形图上显示条形值

iOS: Assets 目录中启动图像上的黑线

ios - Xcode 7 : Write UI Tests with a deployment target below iOS 9?

ios - Xcode 7.1 UI 测试和代码覆盖率