UI Testing XCUIApplication Cannot Find Alert

Hello Everyone,


I've been trying to work with the new XCTest UI Automation. I'm having some trouble dealing with Alerts.


When launching the app for the first time, there's an alert that pops up and asks if you want to allow Push Notifications. There is a

"Don't Allow" and "OK" button attached to the Alert.

The problem comes when attempting to actually interact with this alert.


When using the new "UI Record" option to select the "OK" button the following is recorded.

func testExample() {
        let app = XCUIApplication()

        XCUIApplication().alerts["“AppName” Would Like to Send You Notifications"].collectionViews.buttons["OK"]


Alternatively the following is another option when selecting on the available lists of elements off of the .XCUIApplication().alerts["“APPName” Would Like to Send You Notifications"] element:

XCUIApplication().descendantsMatchingType(.Unknown).alerts["“AppName” Would Like to Send You Notifications"]


Why is the .descendentsMatchingType(.Unknown).alerts[ ] in there?


The problem is when Playing back the Recorder, it never clicks the "OK" button and cannot find the element and an Error is thrown.


The following Error taken from the logs is:

UI Test Activity:

Assertion Failure: UI Testing Failure - The operation couldn’t be completed. No matches found for Alert, input was {(

Application 0x7fa71d9067c0: {{0.0, 0.0}, {375.0, 667.0}}, title: 'AppName', label: 'AppName'

)}


When setting a break point the app never finds any Alerts. I've tried doing the following in the debugger console.

po app.alerts.count

The returned output is always 0.


What I think is going on is:

1) The App Opens

2) The Alert Comes up, but is not found

3) The test can't find the Alert Element to interact with it.

4) The Test Fails.


Can anyone offer up any Advice for the following?

1) Finding and listing all known Elements on a Simulator's screen at one point?

2) Determine why the descendentsMatchingType(.Unknown) is there?

3) Being able to tell if an Alert is apart of an "XCUIApplication" or if I may have two XCUIApplication states going on?

4) Determine if I need to "wait" for something to be present before the "Alert" is actually recognized on screen?

After doing some more research this appears to be an issue with the type of Alert that the user is being prompted with. The Alert is a "System Alert" and is not contained within the scope of the app it appears.


If the App has no way of handling this system alert then I'm not sure really what to do with this. Common Alerts for "Access to Location", "Access to the Camera", "Access to the Microphone", "Access to the Photos" are going to be troublesome here. Does anyone have any experience yet with working with "System Alerts" and trying to use the new XCTest UI Automation to handle them?


To help anyone else who may be having trouble too, using the Debugger and running the following command is very useful for getting information about what's viewable and accessable throughout the element heirarchy tree.

p print(app.debugDescription)

Seems to fixed in the new beta, it seems to automaticlly tap OK in the alert

Confirmed "working" in that tests can proceed in the new beta. But to me it is a workaround--I'd like to be able to test scenarios with "Don't Allow" as well.

Yes, this just blindly accepts the default "OK" action most of the time for alerts, and I agree it is probably a temporary workaround. There are many cases / scenarios which you want to test the negative actions as well.

use addUIInterruptionMonitorWithDescription method to fix the problem

UI Testing XCUIApplication Cannot Find Alert
 
 
Q