【问题标题】:How to dismiss System Authorization Alerts in XCUITest in iOS 12如何在 iOS 12 的 XCUITest 中关闭系统授权警报
【发布时间】:2019-09-04 08:54:58
【问题描述】:

我仍然无法在 XCUITest 中对“照片库的授权请求”等系统警报做出反应。我尝试了以下代码 sn-ps。但它们都不起作用。有什么想法吗?

UI 中断监视器

addUIInterruptionMonitor(withDescription: "System alert") { (alerts) -> Bool in
    if alert.buttons["OK"].exists {
        alert.buttons["OK"].tap()
    }
    return true
}

XCUIApplication().sheets.buttons["Aufnahme"].firstMatch.tap()

等待 NSPredicate

XCUIApplication().sheets.buttons["Aufnahme"].firstMatch.tap()

let existsPredicate = NSPredicate(format: "exists == true")
let expectation = XCTNSPredicateExpectation(predicate: existsPredicate, object: app.alerts.firstMatch)
let result = XCTWaiter.wait(for: [expectation], timeout: timeout)
if result == .completed {
    app.alerts.firstMatch.tap()
} else {
    print("### TIMEOUT ###")
}

等待存在

XCUIApplication().sheets.buttons["Aufnahme"].firstMatch.tap()
app.alerts.firstMatch.waitForExistence(timeout: 10)

跳板解决方案

此解决方案基于Lepidopteron 的帖子。它通常可以工作,但似乎很慢(请参阅下面的日志)。在 t=14.51s 我点击按钮“打开照片库。然后系统警报直接出现在我的应用程序中。但随后一切似乎冻结了大约 70 秒。 在 t = 78.08 时,setPermission() 函数被调用。

有人遇到过类似的行为吗?

### OPEN PHOTO LIBRARY
    t =    14.51s Tap "Aufnahmen" Button
    t =    14.51s     Wait for bone.self.TargetShooter to idle
    t =    14.56s     Find the "Aufnahmen" Button
    t =    14.62s         Check for interrupting elements affecting "Aufnahmen" Button
    t =    14.84s     Synthesize event
    t =    14.98s     Wait for bone.self.TargetShooter to idle
    t =    75.08s         App animations complete notification not received, will attempt to continue.
### START AUTHORITATION PROCESS
    t =    75.08s Get number of matches for: Descendants matching type Alert
    t =    75.12s     Snapshot accessibility hierarchy for app with pid 13439
    t =    75.16s     Find: Descendants matching type Alert
### ALLOW SYSTEM ALERT
    t =    75.16s Find the Alert
    t =    75.16s     Snapshot accessibility hierarchy for app with pid 13439
    t =    75.23s     Find: Descendants matching type Alert
    t =    75.23s     Find: Element at index 0
    t =    75.23s Get number of matches for: Descendants matching type Button
    t =    75.29s     Snapshot accessibility hierarchy for app with pid 13439
    t =    75.33s     Find: Descendants matching type Alert
    t =    75.33s     Find: Element at index 0
    t =    75.33s     Find: Descendants matching type Button
    t =    75.33s Checking existence of `Button`
    t =    75.33s     Snapshot accessibility hierarchy for app with pid 13439
    t =    75.39s     Find: Descendants matching type Alert
    t =    75.39s     Find: Element at index 0
    t =    75.39s     Find: Descendants matching type Button
    t =    75.39s     Find: Element at index 1
    t =    75.39s Find the Button
    t =    75.39s     Snapshot accessibility hierarchy for app with pid 13439
    t =    75.45s     Find: Descendants matching type Alert
    t =    75.45s     Find: Element at index 0
    t =    75.45s     Find: Descendants matching type Button
    t =    75.45s     Find: Element at index 1
    t =    75.59s Tap "OK" Button
    t =    75.59s     Wait for com.apple.springboard to idle
    t =    75.65s     Find the "OK" Button
    t =    75.65s         Snapshot accessibility hierarchy for app with pid 13439
    t =    75.72s         Find: Descendants matching type Alert
    t =    75.72s         Find: Element at index 0
    t =    75.72s         Find: Descendants matching type Button
    t =    75.72s         Find: Element at index 1
    t =    75.83s         Check for interrupting elements affecting "OK" Button
    t =    75.88s             Snapshot accessibility hierarchy for app with pid 13439
    t =    75.93s             Find: Descendants matching type Alert
    t =    75.98s     Synthesize event
    t =    76.13s     Wait for com.apple.springboard to idle
### END AUTHORITATION PROCESS

【问题讨论】:

    标签: swift ios12 xcuitest


    【解决方案1】:

    这就是它在我的代码中的工作方式,试试吧:

    var photoAccessGranted = XCTestExpectation(description: "photo access")
    
    
    addUIInterruptionMonitor(withDescription: "system permissions") { (alert) -> Bool in
            print("1) addUIInterruptionMonitor:\(alert)")
    
            if alert.staticTexts["Put here the text that you get in the alert"].exists { // Photo permission
                alert.buttons["OK"].tap()
                self.photoAccessGranted.fulfill()
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 2017-03-17
      相关资源
      最近更新 更多