1

我正在使用 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 不起作用?

4

1 回答 1

0

这不是一个真正的答案,但我在 Xcode 9.3.1 中也看到了这一点。完全相同的谓词组合在许多地方的其他地方都能很好地工作,但特别是一个几乎总是超时。

我现在已经通过在发出等待之前检查对象是否存在来解决它,然后还检查“result == XCTWaiterResultCompleted || object.exists”(目标 C)。

我很想知道这是一个错误还是我做错了什么?

于 2018-05-17T15:43:50.813 回答