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

标签 swift xcode xcode8 xcode-ui-testing

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

当我们运行我们的测试时,它们会在此之后的那个点失败:

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

根据 other answers因此,我尝试使用 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 中断监视器与基于超时的期望相结合(否则,中断监视器将在警报解除之前返回!)。

使用问题中的 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 秒后没有消失,则不会满足预期并且测试将失败,但如果满足,则测试将成功。

关于swift - 显示没有按钮的 UIAlertController 时 UI 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40916607/

相关文章:

ios - 为什么 PHAssetCollection 计数为 0?

ios - 重新加载后 uicollectionviewcell 标签未对齐

iphone - 左侧的 UITableView 图像

ios - Xcode 8.21 (8C1002) 在 VirtualBox 上处理 iPhone 6s 10.1.1 (14B100) 的符号文件时卡住

swift - 更新模型以匹配请求主体与 Swift 的简洁方法?

json - 使用 SwifyJson 从 JSON 中获取数据

ios - 无法将新版本添加到 testflight

ios - UI 测试时无法选择 UIPicker

ios - Xcode 8 预处理器宏?

xcode - Core Data 代码生成导致构建失败