iOS UI Test: how to get message of UIAlertController
You can't directly test the alert's message. You can, however, test if the alert contains your error message's copy (at all).
For example, say your alert looks like this:
To assert that the alert contains the "Final Score" message, use:
XCTAssert(app.alerts.element.staticTexts["Final Score: 27 - 25"].exists)
You can also test the title of the alert directly:
XCTAssertEqual(app.alerts.element.label, "You won!")
More examples available in my UI Testing Cheat Sheet and Examples post and sample app.
Further to the answers above, which I struggled to get to work, there is another way.
Creata a Bool within your UItest method that is false:
var alertPressed = false
Then add a UIInterruptionMonitor and set the bool to true within it's closure:
addUIInterruptionMonitor(withDescription: "System Dialog") {
(alert) -> Bool in
alert.buttons["Allow"].tap()
alertPressed = true
return true
}
Then interact with the app again, and assert that the Bool is true
app.tap()
XCTAssert(alertPressed)
I hope this is helpful to someone.