2016-08-25 62 views
1

我正在使用Swift進行Xcode UI測試應用程序。我們的測試應用程序有時會彈出一個「警告框」,影響測試用例的正常工作流程。沒有辦法預測彈出窗口何時出現。它可能出現在測試用例1或測試用例編號x處。使用Xcode UI測試進行Swift ASYNC事件處理

我想解僱「Alert Box」並繼續測試案例的其餘部分。我如何使用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() 
        } 
    } 
    

Ref1至: https://www.bignerdranch.com/blog/asynchronous-testing-with-xcode-6/
至Ref2: XCTest and asynchronous testing in Xcode 6
REF3:https://adoptioncurve.net/archives/2015/10/testing-asynchronous-code-in-swift/

是否有通用的方法來處理整個套件中的異步事件?如何在另一個線程等待「Alert Box」出現事件時繼續測試?

乾杯!

回答

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 
    } 
}