【问题标题】:How to wait for Location Alert ( which is system alert) to show?如何等待位置警报(即系统警报)显示?
【发布时间】:2017-05-08 17:12:12
【问题描述】:

我想出了如何关闭系统警报,但我无法等待它显示,因为应用程序看不到系统警报。我尝试使用 app.debugDescription 和 app.alerts.count 进行调试,但没有成功。

【问题讨论】:

标签: xcode8 xctest ios-ui-automation


【解决方案1】:

您应该使用@Oletha 所写的addUIInterruptionMonitor

这里的棘手部分是系统警报按钮不使用辅助功能标识符,因此您必须搜索文本才能点击它们。此文本会翻译成您运行模拟器/设备的语言,如果您想针对英语以外的多种语言运行测试,这可能会很困难。

您可以使用AutoMate framework 来简化此操作。这里有一个如何使用 AutoMate 处理系统警报的示例:

func locationWhenInUse() {
    let token = addUIInterruptionMonitor(withDescription: "Location") { (alert) -> Bool in
        guard let locationAlert = LocationWhenInUseAlert(element: alert) else {
            XCTFail("Cannot create LocationWhenInUseAlert object")
            return false
        }

        locationAlert.allowElement.tap()
        return true
    }

    // Interruption won't happen without some kind of action.
    app.tap()
    // Wait for given element to appear
    wait(forVisibilityOf: locationPage.requestLabel)
    removeUIInterruptionMonitor(token)
}

在上面的示例中,locationAlert.allowElement.tap() 是可能的,因为 AutoMate 可以处理 iOS 模拟器支持的任何语言。

有关如何使用 AutoMate 处理系统警报的更多示例,请查看:PermissionsTests.swift

【讨论】:

    【解决方案2】:

    使用addUIInterruptionMonitor:withDescription:handler: 注册中断监视器。要“等待”系统警报出现,请在处理完毕后使用处理程序设置变量,并在您希望等待警报时与应用程序执行良性交互。

    您必须在等待期间继续与应用交互,因为交互是触发中断监视器的原因。

    class MyTests: XCTestCase {
    
        let app = XCUIApplication()
    
        func testLocationAlertAppears() {
            var hasDismissedLocationAlert = false
            let monitor = addUIInterruptionMonitor(withDescription: "LocationPermissions") { (alert) in
                // Check this alert is the location alert
                let location = NSPredicate(format: "label CONTAINS 'Location'")
                if alert.staticTexts.element(matching: location).exists {
                    // Dismiss the alert
                    alert.buttons["Allow"].tap()
                    hasDismissedLocationAlert = true
                    return true
                }
                return false
            }
    
            // Wait for location alert to be dismissed
            var i = 0
            while !hasDismissedLocationAlert && i < 20 {
                // Do some benign interaction
                app.tap()
                i += 1
            }
    
            // Clean up
            removeUIInterruptionMonitor(monitor)
    
            // Check location alert was dismissed
            XCTAssertTrue(hasDismissedLocationAlert)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多