Xcode UITest push notification alerts can't click "Allow"
Asked Answered
H

3

9

Anyone has had similar situation? So the app open, push notification alerts show up asking if users allow receive push notification, the tests click Don't Allow even I have the following code:

func testClickSystemAlert(){
        let app = XCUIApplication();
        XCUIApplication().alerts["“纳豆行” Would Like to Send You Notifications"].buttons["Allow"].tap()
    }

here is output of print(XCUIApplication().debugDescription);

Attributes: Application 0x60000016c540: {{0.0, 0.0}, {414.0, 736.0}}, label: '纳豆行'
Element subtree:
 →Application 0x60000016c540: {{0.0, 0.0}, {414.0, 736.0}}, label: '纳豆行'
    Window 0x60000016bc40: Main Window, {{0.0, 0.0}, {414.0, 736.0}}
      Other 0x60000016c780: {{0.0, 0.0}, {414.0, 736.0}}
        Other 0x60000016c9c0: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
          ScrollView 0x60000016bf40: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
            Image 0x60000016c0c0: traits: 8589934596, {{0.0, 0.0}, {414.0, 736.0}}, identifier: '1'
          PageIndicator 0x600000163000: traits: 8589939200, {{132.0, 691.0}, {150.0, 55.0}}, value: page 1 of 4
    Window 0x6000001693c0: {{0.0, 0.0}, {414.0, 736.0}}
      StatusBar 0x600000169240: {{0.0, 0.0}, {414.0, 20.0}}
        Other 0x60000016ca80: {{0.0, 0.0}, {414.0, 20.0}}
        Other 0x60000016cb40: {{0.0, 0.0}, {414.0, 20.0}}
          Other 0x60000016cc00: traits: 8388608, {{6.0, 0.0}, {39.0, 20.0}}
          Other 0x60000016ccc0: traits: 8388608, {{50.0, 0.0}, {13.0, 20.0}}, label: '3 of 3 Wi-Fi bars', value: SSID
          Other 0x60000016cd80: traits: 8389120, {{181.0, 0.0}, {56.0, 20.0}}, label: '12:29 AM'
          Other 0x60000016ce40: traits: 8388608, {{384.0, 0.0}, {25.0, 20.0}}, label: '-100% battery power'
Path to element:
 →Application 0x60000016c540: {{0.0, 0.0}, {414.0, 736.0}}, label: '纳豆行'
Query chain:
 →Find: Target Application 0x6000000b7340
  Output: {
    Application 0x60000016c540: {{0.0, 0.0}, {414.0, 736.0}}, label: '纳豆行'
  }
Hoskins answered 12/2, 2017 at 16:27 Comment(0)
H
8

I end up using this:

addUIInterruptionMonitor(withDescription: "Allow push") { (alerts) -> Bool in
            if(alerts.buttons["Allow"].exists){
                alerts.buttons["Allow"].tap();
            }
            return true;
        }

it will click the Allow buttons

Hoskins answered 13/2, 2017 at 7:22 Comment(1)
This is working fine for me.Vizzone
L
4

The original answer only works when you add application tapping

addUIInterruptionMonitor(withDescription: "YOUR TITLE") { (alert) -> Bool in
                if alert.buttons["OK"].exists {
                    alert.buttons["OK"].tap()
                }
                return true
            }
XCUIApplication().tap() // which is very important!!!
Lurdan answered 6/6, 2018 at 11:20 Comment(1)
I'm not sure why that extra tap is required but without it my test wouldn't tap the "Allow" button. Anyone know where the docs are on this?Ridotto
R
1

If you know when the "Allow Notifications" prompt comes up, just do this

XCUIApplication().tap()

It's working for me.

Note: I had the addUIInterruptionMonitor call in there as well, and since it didn't work alone, I removed it. Turns out it's not necessary.

Ridotto answered 22/7, 2022 at 0:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.