2017-03-15 28 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触发!

相关问题