Xcode 8 - 选择允许使用UITests访问相册

时间:2017-03-15 11:42:24

标签: xcode alert uialertcontroller xcode-ui-testing

我在Xcode 8中运行UITests。我有一个测试,可以为应用添加照片。当我第一次安装应用程序时,我会获得弹出窗口,请求访问相册。

我尝试的所有内容都会导致选中“不允许”按钮,然后会中断测试。当我录制'允许'按钮时,单击UIAlert,但是当我运行po XCUIApplication()。debugDescription时,没有找到警报,尽管它们在屏幕上。

有没有人找到解决此问题的方法?

1 个答案:

答案 0 :(得分:2)

要在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)
}

要完成这项工作,您必须确保在您的UITest触发系统警报之前 之前添加了UI中断监视器!