【问题标题】:Handler of addUIInterruptionMonitor is not called for Alert related to Photos与照片相关的警报不调用 addUIInterruptionMonitor 的处理程序
【发布时间】:2017-02-19 19:08:38
【问题描述】:
private func acceptPermissionAlert() {

    _ = addUIInterruptionMonitor(withDescription: "") { alert -> Bool in

        if alert.buttons["Don’t Allow"].exists { //doesnt get here second time

            alert.buttons.element(boundBy: 1).tapWhenExists()

            return true
        }

        return false
    }
}

这不适用于:

在应用程序的开头,它在接受通知权限时运行良好,但在这里......不起作用。

你知道为什么吗?

【问题讨论】:

    标签: ios swift xcode-ui-testing


    【解决方案1】:

    我发现 addUIInterruptionMonitor 有时无法及时处理警报,或者直到测试完成。如果它不起作用,请尝试使用管理 iOS 主屏幕的 Springboard。您可以从那里访问警报、按钮等,这对于您确切知道何时显示警报的测试特别有用。

    所以,是这样的:

    `let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard") 
    
    let alertAllowButton = springboard.buttons.element(boundBy: 1)
    if alertAllowButton.waitForExistence(timeout: 5) {
       alertAllowButton.tap()
    }`
    

    buttons.element(boundBy:1) 将确保您点击右侧的按钮,将 1 更改为 0 以点击左侧,(因为有时"Don't Allow" 中的 ' 会导致问题)。

    【讨论】:

    • addUIInterruptionMonitor 在本地工作,但在 CI 上无法触发访问摄像头。这是唯一可行的方法。
    【解决方案2】:

    添加:

    app.tap()
    

    在方法结束时。

    这是因为您需要与应用交互才能触发处理程序。

    【讨论】:

    • 或 app.swipeUp() 如果点击会导致您的应用中实际发生某些事情(而您不希望它发生)
    • 它对我有用。这有点奇怪......“你需要与应用程序交互才能触发处理程序。”那是在某个地方记录的吗?
    • 这个bug有雷达吗?
    • app.tap() 点击屏幕中间,这可能会导致不需要的操作,正如@xaphod 指出的那样。如果您也无法使用swipeUp(),也许您可​​以点击屏幕上的特定点,请参阅here
    【解决方案3】:

    添加中断监视器后,您应该继续与应用交互,就像它没有出现一样。

    另请注意,您的按钮标识符中有一个“智能引号”,而不是常规撇号。

    let photosAlertHandler = addUIInterruptionMonitor(withDescription: "Photo Permissions") { alert -> Bool in
        if alert.buttons["Don't Allow"].exists {
            alert.buttons.element(boundBy: 1).tapWhenExists()
            return true
        }
        return false
    }
    
    // Do whatever you want to do after dismissing the alert
    let someButton = app.buttons["someButton"]
    someButton.tap() // The interruption monitor's handler will be invoked if the alert is present
    

    当警报出现后发生下一次交互时,将调用中断监视器的处理程序并处理警报。

    当你认为你已经完成了中断监视器时,你还应该删除它,否则它将被调用以显示任何其他警报。

    removeUIInterruptionMonitor(photosAlertHandler)
    

    【讨论】:

    • 实际按钮具有“智能引用”字符 而不是'。此外,您的代码会产生编译器错误 Value of type 'XCUIElement' has no member 'tapWhenExists'
    • @AaronBrager tapWhenExists() 方法是问题代码中的一种,而不是标准库函数。我建议您改用tap(),并用支票包裹exists
    • “当下一次互动发生时”,请为我点赞
    • “当下一次交互发生时”对我来说确实是关键。
    猜你喜欢
    • 2018-05-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    相关资源
    最近更新 更多