XCUITest: Auto-Accepting System-Alerts. Need more fine grained control - ios

I have read that addUIInterruptionMonitorWithDescription could be used to accept / tap on a particular button in a system alert. On recently trying some test code out, I was doing the following:
Adding a Photos Alert
Adding a Calendar Alert
Adding a Location Alert.
On the simulator, I was surprised to find that the Calendar and Location prompt automatically had their "Allow" buttons tapped. For the Photos prompt, the "Don't Allow" was hit. My question is - is there no need for addUIInterruptionMonitorWithDescription anymore? I tried using it for tapping on the dialogs but it didn't do anything. Even when I tried to hit another button on the alerts, I didn't see it working. How do I tap on the individual buttons on a system alert her?

If there is an alert on screen, and none of your interruption handlers handle it, XCTest will dismiss it for you if you are using Xcode <9.1.
To gain control of the alerts, you should create an interruption handler for each alert, returning true from the closure when (and only when) you have handled the alert that handler was intended for.
If the test tries all your alert handlers or receives a true return value from any of your handlers, and there is still an alert on screen, XCTest will handle the alert itself.

Related

Xcode 7 UI Testing XCUIElementQuery Randomly Not Updating Properly

I have a UI Test that dismisses an alert, checks to see if the alert had been dismissed (if not it attempts to dismiss it again) and then continues on with the rest of the test.
Most of the time the test passes just fine, but sometimes seemingly randomly (though more often in the simulator on a machine with older hardware), the test will do the following:
check for the alert
tap on the button that dismisses the alert
the alert is dismissed
check for the alert (and still find it - I have logged the query at this point and it is indeed finding the same alert that was just dismissed)
attempt to tap the button that dismisses the alert
complain that it can't find the alert and fail
A couple of notes:
I have not, as of yet, observed this behaviour when stepping through the test with the debugger.
Forcing the test to wait a bit after dismissing the alert (using things like usleep()) can still cause this behaviour.
Why is the query still finding the alert after it has been dismissed? Why is this behaviour seemingly random?
Have you tried interacting with the alert via a UI Interruption handler?
addUIInterruptionMonitorWithDescription("Alert") { (alert) -> Bool in
alert.buttons["Allow"].tap()
return true
}

Delaying application while UIAlertView is active

I added an Alert view, asking for user input, at the start of my app. The app works fine without the Alert view. But with the code for the Alert view added, part of the UI is blacked out after hitting the 'ok' button on the alert.
I'm not well versed in ios, but is there a good way to delay the app from running until the Alert (text input) is completed (ok button pressed). This might avoid whatever is causing the screen to go black in one section. Apparently the app is executing while the alert is active, and the alert is affecting the UI. Basically, I am asking the user to input their phone number via an alert that will be used later in the app.
When alert view is shown on screen, getting back ground dimmed (reduced alpha) is a normal thing and is practiced by iOS.
However, if you feel some part of the code you want to run only on tap on OK button on alert, move that method call to OK button action handler.

Get completion event from default warning dialog?

what I thought should be simple, doesn't seem so simple...
I have a BT application, so if BT is off on launch, the default warning "Turn On Bluetooth to Allow...", which has the options "Settings" and "OK" as the options. Since this is a default dialog, how do I get the button index clicked?
I am working with the iOS 8 SDK, so I assume this dialog is making use of the new UIAlertController, which now uses a completion block instead of a delegate method.

Need info on handling system notification on iPhone in automation

We initiate pairing with iPhone,
On iPhone a notification “Do you want to pair with this device” with 2 buttons “Pair” and “cancel” is displayed.
I need to click on this “Pair” button.
I notice that this notification displayed is not part of the any mobile (iPhone) application instead its system notification.
With the current tool, I am unable to click on this “Pair” button, hence can you please suggest an automation tool for iPhone which can perform click on this “Pair” button displayed as part of system notification?
its true you can not click buttons on alert and notification with inbuild automation. automation have inbuild functionality which cancel notification and alert by click on cancel
If you are using Instrument tool provided by apple then also it is possible to tap on "Pair". Only thing you need to do is register a callback for alert before your alert come. So limitation with this is if your alert come at starting point only then it is not possible with this tool but if this alert comes in between then definitely you can tap on "Pair".
You can see in UIAutomation doc hon how to register a callback for alert.
If not able to then I can help you more.

UI Automation onAlert method not being called on simulator

Upon initial launch of my app, I get a permissions alert asking if I will allow the app to use my current location. My onAlert method successfully dismisses the alert on my device. When I run it on the simulator, it never gets called. Other internal alerts are handled by the onAlert method on the simulator. The permission alert coming from SpringBoard is not handled on the simulator. Any ideas?
UIATarget.onAlert = function onAlert(alert)
{
var title = alert.name();
UIALogger.logMessage(title);
return false;
}
This problem happens because the alert you're seeing comes from the system itself -- before the app actually launches and your automation environment is initialized.
To see this happen, add a debug line before the function definition for UIATarget.onAlert:
UIALogger.logDebug("Now setting up the alert function");
UIATarget.onAlert = function onAlert(alert) {}
Next, Reset Content and Settings... on your simulator and re-run your automation. You should notice that the debug line will not appear until after you manually dismiss the alert about using the current location.
I do not see how this would be fixable from javascript code. You have to delay the alert until the app has properly launched, or follow the example shown in this answer.
If default handler is not working for you, then you can simply use 'return true' instead of the 'return false' so that you can manually dismiss the popover.
Before 'return true' statement you can write some statement for tapping the button (dismiss button) you wish to.
I had the same problem with an app that presents an alert immediately after launching. When I logged the element tree, I could see the additional alert window, and I could let UIAutomation tap the OK button in the alert. But the alert handler was never called.
The reason was that the alert appeared before UIAutomation was set up properly to handle it. If I delayed the presentation of the alert, UIAutomation did catch it.

Resources