如何在 Xcode 中的 UITests 下等待,直到可以看到某些视图以供点击?

Posted

技术标签:

【中文标题】如何在 Xcode 中的 UITests 下等待,直到可以看到某些视图以供点击?【英文标题】:How to wait under UITests in Xcode until some view will be visible for tap? 【发布时间】:2016-09-30 09:54:20 【问题描述】:

有时在 Xcode 中的 UITests 下,编译器会在加载和呈现按钮之前尝试点击按钮。然后出现no matched found for...之类的问题。

但是解决这个问题的简单方法是:

sleep(1) //wait 1 second and give `mybutton` time to load and be accessible for uitests
mybutton.tap()

但这太可怕了,因为我不能把0.1 作为参数放在那里。在很多按钮之前等待 1 秒让我很烦。

有没有办法等到它对 uitests 可见?

【问题讨论】:

【参考方案1】:

您应该创建一个XCTestExpectation 并等待它完成

expectationForPredicate(NSPredicate(format: "hittable == true"), evaluatedWithObject: mybutton, handler: nil)
waitForExpectationsWithTimeout(10.0, handler: nil)

mybutton.tap()

【讨论】:

对于 Swift 3,您可以留下 handler 参数。

以上是关于如何在 Xcode 中的 UITests 下等待,直到可以看到某些视图以供点击?的主要内容,如果未能解决你的问题,请参考以下文章

XCode 中禁用的 UITests

在 XCUITests 中,如何等待多个 ui 元素的存在?

UITests(快照)中的本地化字符串始终为英文

如何在 UITests 下重新排序单元格?

Xcode UITesting - 如何移动 UISlider?

UITests - 如何在UITests中获取当前语言