UI 显示没有按钮的 UIAlertController 时测试失败

UI Testing Failure when displaying UIAlertController with no buttons

我们使用 UIAlertController 作为网络请求发生时的加载指示器。没有与此 UIAlertController 关联的操作,因为它会在网络 activity 完成时自动关闭。我们会在用户点击我们应用程序的登录按钮后显示此警报。

当我们 运行 我们的测试时,他们在这之后失败了:

UI Testing Failure - Did not receive view did disappear notification within 2.0s  

根据 SO 上的 other ,我尝试使用 addUIInterruptionMonitor 来处理警报,但没有成功。我认为这是因为 UIAlertController 上没有可操作的按钮。由于无法对警报采取任何操作,因此中断监视器如下所示:

addUIInterruptionMonitor(withDescription: "Loading") { handler in  
    return true  
}

尽管如此,我还是得到了同样的错误。我该如何解决这个问题?

编辑:相关UI 测试代码如下:

class UI_Tests: XCTestCase {
    override func setUp() {
        super.setUp()

        continueAfterFailure = true

        XCUIApplication().launch()
    }

    func testLogin() {
        let app = XCUIApplication()
        let tablesQuery = app.tables

        let secureTextField = tablesQuery.cells.containing(.staticText, identifier:"PIN").children(matching: .secureTextField).element
        secureTextField.tap()
        secureTextField.typeText("1234")

        app.buttons["Login"].tap()

        addUIInterruptionMonitor(withDescription: "Loading") { handler in
            return true
        }

        // Test failure occurs here.

        let searchField = tablesQuery.searchFields.element(boundBy: 0)
        searchField.tap()
        searchField.typeText("hey")
    }
}

联系 Apple DTS 后,发现当显示 UI 中断/UIAlertController 根据超时解除时,您需要结合 UI 中断使用基于超时的期望进行监视(否则,中断监视器将 return 在警报解除之前!)。

使用问题中的 UI 测试示例,此方法如下所示:

class UI_Tests: XCTestCase {
    override func setUp() {
        super.setUp()

        continueAfterFailure = true

        XCUIApplication().launch()
    }

    func testLogin() {
        let app = XCUIApplication()
        let tablesQuery = app.tables

        let secureTextField = tablesQuery.cells.containing(.staticText, identifier:"PIN").children(matching: .secureTextField).element
        secureTextField.tap()
        secureTextField.typeText("1234")

        app.buttons["Login"].tap()

        addUIInterruptionMonitor(withDescription: "Loading") { alert in
            self.expectation(for: NSPredicate(format: "exists == false"), evaluatedWith: alert, handler: nil);
            self.waitForExpectations(timeout: 10, handler: nil)
            return true
        }

        // Test failure occurs here.

        let searchField = tablesQuery.searchFields.element(boundBy: 0)
        searchField.tap()
        searchField.typeText("hey")
    }
}

这个期望将等待 10 秒被填充。如果警报在 10 秒后没有消失,则不会满足预期并且测试将失败,但如果是,则测试会成功。