Automating file dialogs with type() and nativeType()

Last edited on

Introduction

It is possible to automate interaction (but not necessarily record the same) with file dialogs across machines that have the same operating system but different file system layouts, and even across machines that have different operating systems (and therefore different file dialogs).

The key to achieving this is to use the type() (for Java , Mac OS X , Qt , Tk , Windows ) or nativeType() function.

In case of nativeType() (which sends the key presses to whatever window which has the focus) this works because commonly file dialogs get the input focus into a text field for entering a file name or path when they appear on the screen.

Toolkit specific automation using type()

The above automation approach works with most editions of Squish, but it does not provide any control over which widgets are currently available for input.

To make the file dialog interaction more robust — at the price of making it toolkit-specific, instead of the above approach, record the entering of a filename and a Return keypress (or OK or Open button click). For example:

def main():
    ...
    # the file dialog gets opened here
    type(waitForObject(names.Filename_InputField), "/Users/myuser/temp.txt")
    type(waitForObject(names.Filename_InputField), "<Return>")

Toolkit agnostic automation using nativeType()

The following approach should work for most Squish editions because it does not require any object references. However, its successful use depends on two assumptions:

  1. The file dialog opens within the anticipated amount of time.

  2. After opening, the file dialog's filename input field has the keyboard focus.

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

    # Snooze long enough for the dialog to pop up and be ready
    test.warning("It can take 10 seconds and more for the native Windows file dialog to appear!")
    test.warning("Therefore PLEASE adjust the following snooze value to meet your requirements!")
    snooze(10)

    # Enter desired file name including its full path:
    nativeType("/Users/clemens/temp.txt")

    # Give the dialog a moment's pause; adjust if required!
    snooze(1)

    # Close the dialog
    nativeType("<Return>")

    # Give the AUT a chance to handle the closing of the file dialog
    snooze(1)