Our app will pop up a system action sheet on the bottom of iPhone screen as below screenshots illustrated. I want to locate and tap the "Call..." and "Cancel" button on that action sheet in UI Test.
First, I tried to add an UI interruption handler, and put a break point into the handler closure but it is not triggered when action sheet showed.
// Add an UI interruption handler to handle system alert.
addUIInterruptionMonitor(withDescription: "Notification permisson request") { alert in
let notifyAllowBtn = alert.buttons["Allow"]
let callBtn = alert.buttons["Call 8663xxxxx"]
if notifyAllowBtn.exists {
notifyAllowBtn.tap()
return true
}
if callBtn.exists {
callBtn.tap()
return true
}
// A placeholder for other alerts
return true
}
// need to interact with the app again for the handler to fire
app.swipeUp()
Also I have another try with SpringBoard, still without luck. Need help here, how could I locate the element on system action sheet?
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let alertCallButton = springboard.buttons["Call 8663xxxxx"]
XCTAssert(alertCallButton.waitForExistence(timeout: 5))

As @stackich said, I add a 30 seconds delay to find elements on
Springboard. And it works. I put my test in below in case anyone meet the same problem in the future.Worth to mention that, for our app it is not Action sheet actually, instead it is text associated with UITextView, and
UITextViewDelegatemethodtextView(_ textView: UITextView, shouldInteractWith URL:...is fired when a user click a URL.UI Test, the call button is actually a staticText/Label. The cancel button is button. So be sure to give them the correct type.
Minimum test app