Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode UITest push notification alerts can't click "Allow"

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: '纳豆行'
  }
like image 386
harryfeng Avatar asked Dec 23 '22 20:12

harryfeng


2 Answers

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

like image 57
harryfeng Avatar answered Jan 10 '23 16:01

harryfeng


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!!!
like image 29
Allen Avatar answered Jan 10 '23 15:01

Allen