如何在 XCUITests 中等待复制/粘贴提示消失?

时间:2021-07-02 20:45:18

标签: ios swift xctest xcode-ui-testing xcuitest

在我的 UI 测试中,我需要等待复制/粘贴通知消失。在大多数测试案例中,通知看起来:

Copy/paste notification

我试着这样等待:

    func waitForPasteboardInfoDissappear(file: StaticString = #file, line: UInt = #line) {
        let elements = [
            "Pola pasted from PolaUITests-Runner",
            "CoreSimulatorBridge pasted from Pola",
        ].map { app.staticTexts[$0] }
        if !elements.waitForDisappear(timeout: 10) {
            XCTFail("Pasteboard info still visible", file: file, line: line)
        }

    }
extension XCUIElement {
    func waitForDisappear(timeout: TimeInterval) -> Bool {
        let result = XCTWaiter().wait(for: [waitForDisappearExpectation()],
                                      timeout: timeout)
        return result == .completed
    }

    func waitForDisappearExpectation() -> XCTestExpectation {
        let predicate = NSPredicate(format: "exists == false")
        return XCTNSPredicateExpectation(predicate: predicate, object: self)
    }
}

extension Array where Element == XCUIElement {
    func waitForDisappear(timeout: TimeInterval) -> Bool {
        let expectations = map { $0.waitForDisappearExpectation() }
        let result = XCTWaiter().wait(for: expectations,
                                      timeout: timeout)
        return result == .completed
    }
}

大多数情况下都能用,但我使用快照测试来验证测试,有时提示仍然可见(没有文字)

Copy/paste notification without text

有什么办法可以检查这个提示是否仍然在屏幕上可见?

0 个答案:

没有答案