Dismiss alert on initial launch on iOS simulator

1.2k Views Asked by At

I'm working on the automated UI tests for my app and I'm having trouble when trying to set up the environment for running the tests. The plan is roughly this:

  • build the application
  • shutdown simulator if running
  • erase the simulator to make a clean install
  • install my app on the simulator
  • run UIAutomation tests

Everything is working except when the application is launched by instruments to execute the tests, the alert appears to ask if the user allows notifications. This is all as expected, but I can't find the way to get rid of the alert.

Things I have already tried:

  • creating onAlert as a first thing in my test script, in case it appears before the my alert callback is defined
  • delay the target by 5 seconds in case the tests actually run even before the UI of the app is visible in the simulator

I also went through all the permutations of the above that can be found on SO, I never get my onAlert callback invoked, no matter what I do. So another thing I tried was:

  • try dismissing the alert with applescript

The script I wrote:

tell application "System Events"
    tell process "iOS Simulator"
        set allUIElements to entire contents of window 1
        repeat with anElement in allUIElements
            try
                log anElement
            end try
        end repeat
    end tell
end tell

and it displays:

static text “MyApp” Would Like to Send You Notifications of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator
static text Notifications may include alerts, sounds, and icon badges. These can be configured in Settings. of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator
UI element 3 of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator

Looks like the buttons are placed inside the "UI element 3" but I can't retrieve any elements from inside it, let alone clicking on it. So I checked with Accessibility Manager:

UI element 3 in Accessibility Manager

It sits there as one of the children, the other ones are notification title and message. But when I go to that element, it is highlighted and I see this:

enter image description here enter image description here

It is identified as generic element, it doesn't have any children... The interesting thing is when I choose the OK button in the Accessibility Inspector, I can actually see it's a child of the window, yet it is never listed:

enter image description here

Can someone please shed some light on what is going on here? How can I press that button with Applescript?

2

There are 2 best solutions below

0
On

If you are doing automation using Instrument, the you will need to register callback (onAlert) for performing any action on alerts.

But the problem in your case is that the alert appears before your script actually start executing and at that time no callback is registered for alert.

So if the alert can come with a delay of around 10 sec when you start application, then only it can be handled. But this can only be controlled through source code and not by your Automation code.

So only option which is left is you need to manual dismiss the alert once fresh application is installed

I am also facing same problem and found it to be a limitation of the tool

There are too many limitaion of this tool and thats why i shifted to UFT

0
On

I had a similar problem. I just wanted position of the last control on the alert. So I came up with following piece of code:

on get_simulator_last_object_rect(simulator_index)

tell application "System Events"
    set ProcessList to (unix id of processes whose name is "iOS Simulator")
    set myProcessId to item simulator_index of ProcessList
    tell window 1 of (processes whose unix id is myProcessId)

        -- Forcefully print the UI elements so that Accessibility hierarchy is built
        UI elements
        -- Then wait precisely to let the Accessibility view hierarchy is ready
        delay 0.5

        set lowest_label_lowest_position to 0
        set _x to 0
        set _y to 0
        set _width to 0
        set _height to 0

        repeat with element in UI elements

            set {_x, _y} to position of element
            set {_width, _height} to size of element
            set current_control_lowest_position to _y + _height

            if current_control_lowest_position > lowest_label_lowest_position then set lowest_label_lowest_position to current_control_lowest_position - _height / 2

        end repeat
        return {{_x, _y}, {_width, lowest_label_lowest_position}}
    end tell
end tell
end get_simulator_alert_ok_button_position

I have a desktop app to control my actions. I use this apple script in my Desktop app to get the frame of the last control. Now that I have the frame, I create a mouse event and perform click on the frame, after activating the simulator.

Although I have not yet tried, but I am pretty sure that you can create mouse events from apple script and perform click on the frame / center of the frame.

Hope this helps.

Thanks, RKS