Search code examples
xcodeapplescriptios-simulatorxcode-ui-testingautomator

Automator script to automatic toggle menu setting in Simulator.app


I need to automate the process of enrolment of Face ID and Touch ID for my UITests. For this purpose, I'm working on an automator script.

My current automator script, which at the moment can automatic click "Enrolled" in the menu:

on run {input, parameters}

if application "Simulator" is running then
    tell application "System Events"
        set theName to name of the first process whose frontmost is true
    end tell
    tell application "Simulator" to activate
    tell application "System Events"
        tell process "Simulator"
            tell menu bar 1
                tell menu bar item "Hardware"
                    tell menu "Hardware"
                        tell menu item "Face ID"
                            tell menu "Face ID"
                                click (menu item "Face ID" where its name starts with "Matching")
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
    tell application theName to activate
end if
return input
end run 

The problem is as following. I need to determine, if the device already is enrolled. There is a checkmark, which shows the current state. I have tried to check if the checkmarks is there or not. But I have not been able to make it work yet.

enter image description here

So my question. How can I do, so the script only will press the 'Enrolled' menu item, if the checkmark isn't there?


Solution

  • There's an attribute of menu items called AXMenuItemMarkChar, which is either set to the character representing a check mark next to the menu item (if checked), or missing value.

        use application "System Events"
    
        tell application "Simulator" to activate
    
        tell process "Simulator" to tell menu bar 1 to ¬
            tell menu bar item "Hardware" to tell menu "Hardware" to ¬
                tell menu item "Face ID" to tell menu "Face ID" to ¬
                    tell menu item "Enrolled"
    
                        if the value of the attribute "AXMenuItemMarkChar" is not "✓" then ¬
                            click it
    
                        delay 1 -- !important
    
                        return the value of the attribute "AXMenuItemMarkChar"
    
                    end tell
    

    In my testing, the attribute returns the correct value if the Simulator app is in focus at the time. The return value of this script should always be "✓", because if the menu item isn't checked, then the script proceeds to click it and put a check mark next to it; and if the menu item is checked, then there's nothing to do but confirm that it is by getting the attribute's value.

    One of the issues with your script is that you had the wrong reference to a non-existent menu item (menu item "Face ID" of menu "Face ID" of menu item "Face ID"), and then proceeded to filter it with a where clause against a name that had a different value; hence it would return no value, and you'd have no menu item to click.

    Hopefully my script will work for you. Let me know if you have any problems.


    The AppleScript contained in this answer was tested on MacOS 10.13. It is an example script to illustrate how to meet your objective. The delay command may need to be adjusted according to your system, and the script may benefit from some error-handling to make it robust.