【问题标题】:Swift ASYNC Event Handling With Xcode UI TestSwift ASYNC 事件处理与 Xcode UI 测试
【发布时间】:2017-01-02 12:32:24
【问题描述】:

我正在为 Xcode UI 测试应用程序使用 swift。我们的被测应用程序有时会弹出一个“Alert Box”,影响测试用例的正常工作流程。无法预测弹出窗口何时出现。它可能出现在测试用例 1 或测试用例编号 x 处。

我想关闭“警报框”并继续测试用例的其余部分。如何在不影响测试用例正常流程的情况下,使用 swift XCUITest 框架处理类似 ASYNC 性质的事件?

到目前为止我已经找到了:

expectationForPredicate(exists, evaluatedWithObject: alertbox, handler: nil)
waitForExpectationsWithTimeout(300, handler: nil)

由于两个原因,这是不可行的。

  1. 无法预测超时
  2. 正在阻塞测试用例流

    func testTestCase1 {
        let expectation = expectationWithDescription("Alert Found! Dismissing")
        do {
            // ...
            // test steps
            // ...
            expectation.fulfill()
        }
        waitForExpectationsWithTimeout(300) {
            dimissAlert()
        }
    }
    

参考1: https://www.bignerdranch.com/blog/asynchronous-testing-with-xcode-6/
参考2: XCTest and asynchronous testing in Xcode 6
参考3:https://adoptioncurve.net/archives/2015/10/testing-asynchronous-code-in-swift/

是否有一种通用的方法来处理整个套件中的异步事件?如何在另一个线程等待“警报框”出现事件时继续测试?

干杯!

【问题讨论】:

    标签: swift xcode macos asynchronous xcode-ui-testing


    【解决方案1】:

    Stephen 是对的,UI 中断监视器应该可以正常工作。我建议将它添加到您的测试设置中,以便它运行在您的所有测试中。

    class UITests: XCTestCase {
        let app = XCUIApplication()
    
        override func setUp() {
            super.setUp()
            addUIInterruptionMonitorWithDescription("Alert") { (alert) -> Bool in
                alert.buttons["OK"].tap()
                return true
            }
            app.launch()
        }
    
        func testFoo() {
            // example test
        }
    }
    

    【讨论】:

      【解决方案2】:

      Xcode 7.1 添加了 addUIInterruptionMonitorWithDescription,这似乎是您正在寻找的。此处的文档:https://developer.apple.com/reference/xctest/xctestcase/1496273-adduiinterruptionmonitorwithdesc?language=objc

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-28
        • 1970-01-01
        • 1970-01-01
        • 2012-09-02
        • 1970-01-01
        相关资源
        最近更新 更多