我正在使用 swift、xcode 8.3.3 和 XCTest。我正在尝试使用 XCTKVOExpectation 等待屏幕上存在的元素。即使元素存在,它也总是返回超时 (2) 的结果。
这是我的代码:
func waitForElementToAppear(element: XCUIElement) -> Bool {
let expectation = XCTKVOExpectation(keyPath: "exists", object: element,
expectedValue: true)
let result = XCTWaiter().wait(for: [expectation], timeout: 10)
print(element.exists)
print(result.rawValue)
return result == .completed
}
当我打印 element.exists 时,它打印为 true。但是 result.rawValue 是 2 (.timedout) 增加超时值也不能解决这个问题。
我能够成功使用 XCTNSPredicateExpectation:
let myPredicate = NSPredicate(format: "exists == true")
let myExpectation = XCTNSPredicateExpectation(predicate: myPredicate,
object: element)
let result = XCTWaiter().wait(for: [myExpectation], timeout: 10)
return result == .completed
想知道为什么 XCTKVOExpectation 不起作用?