2017-03-15 27 views
0

我正在Xcode 8中運行UITests。我有一個測試嚮應用添加了一張照片。當我第一次安裝應用程序時,我得到彈出窗口請求訪問相冊。Xcode 8 - 選擇允許使用UITests訪問相冊

我試過的一切都會導致'不允許'按鈕被選中,然後中斷測試。當我記錄'允許'按鈕時,單擊一個UIAlert被找到,但是當我運行po XCUIApplication()。debugDescription時,儘管它們在屏幕上,但沒有發現警報。

有沒有人找到解決這個問題的方法?

+0

joern的回答是對的。打印調試描述時看不到任何內容的原因是因爲在顯示警報之前視圖層次結構沒有更新。當您下一次嘗試與應用程序交互時(忽略警報),視圖層次結構將會更新,並且它會發現警報並運行中斷處理程序來關閉警報,然後執行您的交互。 – Oletha

回答

1

要在UITest期間處理系統提醒您必須添加一個UI中斷監視器

func testPhotoLibraryAccess() { 
    let app = XCUIApplication() 
    app.launch() 

    // when system alert is shown -> dismiss it by pressing "OK" 
    // (the description parameter is only there for debugging purposes 
    // so it can be anything you like) 
    addUIInterruptionMonitor(withDescription: "Photos Access Alert") { (alert) -> Bool in 
     alert.buttons["OK"].tap() 
     return true 
    } 

    // tap button that tries to open user's photo library 
    app.buttons["Open Photos"].tap() 

    // select "Moments" 
    app.buttons["Moments"].tap() 

    XCTAssert(app.navigationBars["Moments"].exists) 
} 

爲了使這項工作,你有系統前請確保UI中斷監視器添加警報由您的UITest觸發!

相關問題