【问题标题】:Xcode 8 - selecting Allow access to photo album with UITestsXcode 8 - 选择允许使用 UITests 访问相册
【发布时间】:2017-08-06 03:11:19
【问题描述】:

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

我尝试过的所有操作都导致选择了“不允许”按钮,这会破坏测试。当我记录“允许”按钮时,发现了一个 UIAlert,但是当我运行 po XCUIApplication().debugDescription 时,没有发现任何警报,尽管它们在屏幕上。

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

【问题讨论】:

  • joern 的回答是正确的。打印调试描述时看不到任何内容的原因是因为在显示警报之前视图层次结构没有更新。当您下次尝试与应用交互(忽略警报)时,视图层次结构将更新,它会发现警报并运行中断处理程序以解除警报,然后执行您的交互。

标签: xcode alert uialertcontroller xcode-ui-testing


【解决方案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)
}

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

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 2017-11-12
    • 2014-11-14
    • 1970-01-01
    • 2010-12-30
    • 2015-04-25
    • 2017-04-10
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多