'iOS Swift UI Test tap alert button
A really small UI test fails when trying to tap an alert button, what step am I missing?
I'm trying to tap the "Continue" button in the alert displayed below with the following code (that I get from recording my steps).
let app = XCUIApplication()
let salesforceloginbuttonButton = app.buttons["salesforceLoginButton"]
salesforceloginbuttonButton.tap()
let posWantsToUseSalesforceComToSignInAlert = app.alerts["“POS” Wants to Use “salesforce.com” to Sign In"]
let continueButton = posWantsToUseSalesforceComToSignInAlert.buttons["Continue"]
continueButton.tap()
When I run the test it fails at the last line (i.e: continueButton.tap()
) with the error No matches found for Find: Descendants matching type Alert from input
.
Notes:
- I already tried waiting a few seconds before tapping the continue button with the same result.
- When the test is ran, the app launches and the alert gets displayed after tapping the
salesforceloginbuttonButton
Solution 1:[1]
I think your alert is not recognized, maybe because of the double quotation marks
You can try and explicitly set the identifier of your alert like this:
let alert = UIAlertController(title: "Alert", message: "msg", preferredStyle: .alert)
alert.view.accessibilityIdentifier = "myAlert"
Then in your tests:
let alert = app.alerts["myAlert"]
let button = alert.buttons["Continue"]
button.tap()
Solution 2:[2]
Had a similar issue but I could solve it by using addUIInterruptionMonitor
. In your XCTestCase add an override setUp like this. One caveat is that it's a little flakey... Will update the answer if I find a better solution.
class ExamplelUITests: XCTestCase {
var app: XCUIApplication!
override func setUp() {
super.setUp()
app = XCUIApplication()
continueAfterFailure = false
addUIInterruptionMonitor(withDescription: "Login Dialog") {
(alert) -> Bool in
let okButton = alert.buttons["Continue"]
okButton.tap()
return true
}
app.launch()
}
func testExample() throws {
app.buttons["Login"].tap()
app.tap() // This one is needed in order for the InteruptionMonitor to work
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alfonse |
Solution 2 | Swaxer |