2016-03-04 51 views
2

我想編寫一個測試,像這樣:XCUITesting許可彈出:出現警報,但UIInterruptionMonitor不火

當我的應用程序進入到一定的窗格中,應該要求使用相機的權限。

我想測試窗格是否出現。我正在使用XC的內置UITest框架來執行此操作。根據我在谷歌和這裏找到的,似乎我應該這樣做:

let dialogAppearedExpectation = expectationWithDescription("Camera Permission Dialog Appears") 

addUIInterruptionMonitorWithDescription("Camera Permission Alert") { (alert) -> Bool in 
    dialogAppearedExpectation.fulfill() 
    return true 
} 

goToCameraPage() 

waitForExpectationsWithTimeout(10) { (error: NSError?) -> Void in 
    print("Error: \(error?.localizedDescription)") 
} 

測試從失敗開始,很好。我實現了goToCameraPage,它正確地導致出現「授予權限」彈出窗口。不過,我希望這會觸發中斷監視器。然而,沒有這樣的中斷被捕獲,並且不會發生履行。

我在對話框出現後的某個地方閱讀你應該做的app.tap()。但是,當我這樣做時,它會點擊「允許」按鈕。該對話框消失,仍然沒有中斷處理。

是否有某種權限對話框不被視爲「警報」或無法處理?我甚至進去用一個看起來在app.alerts的東西代替了中斷位,但結果是空的,即使我正好看着模擬器中的彈出窗口。

謝謝!我正在使用iPhone 6s的Xcode7.2,iOS 9.2模擬器。

回答

2

我也注意到了這個問題。看起來中斷處理程序是異步運行的,無法斷言它們是否被調用。等待期望似乎也會阻止中斷監視器運行。看起來系統正在等待期望完成,期望等待中斷監視器觸發。經典的死鎖案例。

然而,我發現,使用基於NSPredicate一個expecations而古怪的解決方案:

var didShowDialog = false 
expectation(for: NSPredicate() {(_,_) in 
    XCUIApplication().tap() // this is the magic tap that makes it work 
    return didShowDialog 
}, evaluatedWith: NSNull(), handler: nil) 

addUIInterruptionMonitor(withDescription: "Camera Permission Alert") { (alert) -> Bool in 
    alert.buttons.element(boundBy: 0).tap() // not sure if allow = 0 or 1 
    didShowDialog = true 
    return true 
} 

goToCameraPage() 

waitForExpectations(timeout: 10) { (error: Error?) -> Void in 
    print("Error: \(error?.localizedDescription)") 
} 

顯然,做XCUIApplication().tap()內的謂詞塊某種程度上允許運行中斷監控,即使測試用例正在等待期待。

我希望這對你的工作,因爲它爲我做!

+0

我不喜歡這種方法,但又回到了類似的問題,我沒有得到這個工作。 – Quintana

+0

我也不喜歡它:D – pancake