web-dev-qa-db-ja.com

写真に関連するアラートに対してaddUIInterruptionMonitorのハンドラーが呼び出されない

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
    }
}

これは次の場合には機能しません:

enter image description here

アプリの最初は、通知の許可を受け入れながら完璧に機能しますが、ここでは...機能しません。

なぜなのかご存知ですか?

追加:

app.tap()

メソッドの最後に。

これは、ハンドラーを起動するためにアプリを操作する必要があるためです。

割り込みモニターを追加した後は、アプリが表示されていないかのように操作を続ける必要があります。

通常のアポストロフィではなく、ボタン識別子に「スマートクォート」が含まれていることにも注意してください。

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)
7
Oletha

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"_の 'が問題を引き起こすことがあるため)。

2
Taylor Lindsay