我正在使用 XCTWaiter 等待 UI 自动化设置中的某些条件。这是我的自定义 waitFor 方法:
// UITools.swift
public class func waitFor(_ element:Any, timeout:UInt, clause:String) -> Bool
{
let predicate = NSPredicate(format: clause)
let expectation = testcase.expectation(for: predicate, evaluatedWith: element)
print("Waiting for \(element) to become \"\(clause)\" within \(timeout) seconds ...")
let result = XCTWaiter.wait(for: [expectation], timeout: TimeInterval(timeout))
switch result
{
case .completed:
return true
case .invertedFulfillment:
print("waitFor result is in inverted fulfillment.")
return true
case .timedOut:
print("waitFor result is timed out.")
case .incorrectOrder:
print("waitFor result is in incorrect order.")
case .interrupted:
print("waitFor result is interrupted.")
}
return false
}
这种方法在我等待 XCUIElements 但我想等待网络请求完成的情况下工作正常,所以我使用一个在网络请求完成后设置为 true 的标志。这是一个简化的示例:
class Hub : NSObject
{
var isTestRailCasesRetrived = false
func retrieveTestRailCaseData()
{
isTestRailCasesRetrived = false
testrailClient.getTestCases()
{
(response:TestRailModel) in
// Do processing here ...
print("Found \(totalCases) TestRail cases for suite with ID \(suite.id).")
self.isTestRailCasesRetrived = true
}
UITools.waitFor(self, timeout: 30, clause: "isTestRailCasesRetrived == true")
}
}
但是,XCTWaiter 永远不会完成,只是在超时后超时。在这种情况下似乎isTestRailCasesRetrived
从未评估过。有人知道为什么吗?