在 Xcode UI 测试中等待 30 秒

Wait 30 sec in Xcode UI Test

在某一时刻调用了许多 Web 服务调用。我只想等 30 秒,因为我知道所有的事情都会在这段时间内完成。我需要第一个粗略的解决方案。

我试过了,但它引发了一些错误:

tablesQuery.buttons["Button"].tap()
        DispatchQueue.main.asyncAfter(deadline: .now() + 30.0) {
            let tablesQuery2 = tablesQuery

你有什么想法吗?

最简单的方法就是暂时休眠执行:

sleep(30)

但是,如果您希望出现某些东西,最好使用 the built-in function 等待存在:

element.waitForExistence(30)

如果什么都没有出现,它不会失败,所以如果它是你逻辑的关键部分,你可能最好通过带有计时器的期望来检查它:

let exists = NSPredicate(format: "exists == %@", true)
expectationForPredicate(exists, evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(30, handler: nil)