Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Automate GUI Tests for MacOS ‘.dmg’ Installer Images

DZone's Guide to

How to Automate GUI Tests for MacOS ‘.dmg’ Installer Images

The title pretty much gives it away, doesn't it? Learn how to automate your GUI test (particularly Squish) using MacOS (with a link for you Windows users).

· Performance Zone ·
Free Resource

Maintain Application Performance with real-time monitoring and instrumentation for any application. Learn More!

If you have used Squish or any other automated GUI testing tool before, you probably already test your application thoroughly. However, for a new user, there are a few steps to take until they even run the application in the first place: they need to install it. That procedure is part of the first impression you make, so it would be nice to get it right, wouldn’t it? Let’s take a look!

Just in case you are (also) on Windows, we outlined the same procedure for MSI installers in an earlier blog post.

Since we are on macOS, applications are packaged inside disk image (.dmg) files. They usually contain either the app bundle itself or an installer package (.pkg) that performs additional setup besides extracting an app bundle to /Applications. The easiest way to get to grips with the contents of a disk image (from Squish) is to open it via the command line. But before we get our hands dirty, let’s start by looking at the general idea in pseudo code:

openDmg("my_application_installer.dmg")
registerInstallerAsAUT("application_installer")
startApplication("application_installer")
closeDmg()

As you can see, there are three functions. You might recognize the next-to-last one; it is the built-in Squish function to start an application under test. The others are helpers we are going to implement in the next paragraphs to access the dmg installer image.

Opening the DMG Installer Image

Let’s start with openDmg() so that we can see what’s inside the image:

def openDmg():
    test.startSection("Open/mount .dmg file")
    args = ["open", os.path.join(dmg_file_path, dmg_file_name)]
    test.log("Executing: %s" % args)

    try:
        output = subprocess.check_output(args)
    except subprocess.CalledProcessError:
        test.fatal("Opening/mounting failed:")
        test.fatal("File: %s" % os.path.join(dmg_file_path, dmg_file_name))
        test.fatal("Output: %s" % output)
        return False
    finally:
        test.endSection()
    return True

This snippet essentially just executes the command open ~/path/to/Application.dmg and makes use of macOS’s automatic .dmg mounting mechanism. (The open command performs the same action as clicking an item in a Finder window would.) You’ll notice we use this code pattern several times to execute the commands we need.

Registering the Installer As a Squish AUT

The disk image is mounted and macOS should have opened a window as if we had clicked it manually. The next step is to execute the installer so that we can record and replay actions on it as usual. For that to work the installer has to be registered with Squish as a separate application under test (AUT) – which boils down to another command line call:

def registerInstallerAsAUT():
    test.startSection("Register installer as AUT")
    args = [os.path.join(os.getenv("SQUISH_PREFIX"), "bin", "squishserver"), "--config", "addAUT", installer_file_name, installer_file_path]
    test.log("Executing: %s" % args)

    try:
        output = subprocess.check_output(args)
    except subprocess.CalledProcessError:
        test.fatal("Registering installer as AUT failed:")
        test.fatal("Output: %s" % output)
        return False
    finally:
        test.endSection()
    return True

The command line this time: $SQUISH_PREFIX/bin/squishserver --config addAUT your_installer_name /Volumes/Disk_Image/. As you can see: no magic involved. We have completed the two helper functions from the pseudo code we started out with. Let’s get them to do something helpful!

Putting Everything Together

If we put everything together it could look like this:

def main(): 
    if not openDmg():
        return
    if not registerInstallerAsAUT():
        return

    # Start running the installer as the AUT from here 
    test.startSection("Start installer as AUT")
    try:
        startApplication('"%s"' % installer_file_name)
    finally:
        test.endSection()

    # Set breakpoint on the next line, execute to it, start
    # recording a snippet (Run > Record Snippet):
    snooze(1)

You can now teach Squish to complete your installer by recording the necessary steps. At the snooze(1) in the last line, the installer is up and running. Place a breakpoint there and start recording!

Concerning cleaning up after ourselves, you might remember the closeDmg() function in the pseudo code far above. That one just calls umount /Volumes/Your_Image to eject the image after the installer has terminated. A working implementation as well as all the source code from this post can be found in the Knowledge Base article that covers this topic as well.

If You Have an App Bundle

If the AUT just consists of an app bundle (Application.app) as outlined in the beginning, we could simply copy it to a temporary location, register it with Squish (like we did with the installer above) and run it right away.

Collect, analyze, and visualize performance data from mobile to mainframe with AutoPilot APM. Learn More!

Topics:
gui testing ,macos ,test automation ,test automation tools

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}