Automating native macOS file dialogs with nativeType()

Last edited on

Introduction

Squish 8.0.0 and onwards offers the feature of recording file selection in native macOS file dialogs. However, this feature has to work around certain limitations, namely that Squish cannot easily record the individual mouse clicks within the file dialog. What Squish records and subsequently replays is a signal sent by macOS to the AUT that one or more files have been selected. This record/replay logic has been observed to fail in some conditions such as sandboxed applications that do not use security-scoped bookmarks. In the observed cases, the file dialog did not show up while in record mode, preventing any file selection. Squish provides a workaround for this.

Workaround

In the case of macOS native file dialogs not appearing while recording, it is still possible to automate file dialog interaction by falling back to the nativeType() function, which was the standard for file dialog interaction before Squish 8.0.0.

Squish 9.0.1 allows the use of nativeType() by changing the following configuration:

  1. In the Squish installation directory, navigate to etc/macwrapper.ini
  2. Set UseChooseFileHooking = false

It is now possible to select a file using nativeType(). Note that this logic has to be added manually to the test script.

Example:

def main():
    # ...
    # the file dialog gets opened here

    # Snooze long enough for the dialog to pop up and be ready
    snooze(2)
    # Open path input field:
    nativeType("<Command+Shift+g>")
    # Wait long enough for the sheet to appear
    snooze(1)
    # Enter desired path:
    nativeType("/Users/myuser/temp.txt")
    # Give the dialog a moment's pause
    snooze(1)
    nativeType("<Return>")
    # Give the dialog a moment's pause
    snooze(1)
    # Close the dialog
    nativeType("<Return>")
    # Give the AUT a chance to handle the closing of the file dialog
    snooze(1)

Tip: To continue recording after the file selection has been added to the script, use snippet recording:

  1. Set a breakpoint at the point where the file has been loaded
  2. Run the script
  3. Once paused on the breakpoint, select "Run" -> "Record Snippet" from the application
  4. Start recording more user interactions to extend the test case

⚠️ The snooze() statements are important to avoid sending the key presses before the dialog appears, or faster than the dialog can process them.

⚠️ The Command+Shift+g key press causes the "Go to the folder" sheet to appear: