Automating .msi based installers on Windows

Last edited on

To automate installation of “an .msi” file/installer the .msi file must be executed through the standard Windows tool msiexec.exe.

Here is an example that demonstrates automating an .msi based installer via msiexec.exe:

import os
import os.path
import subprocess



# Adjust the following to your setup/needs:



# In or relative to test suite folder:
#msi_file = squishinfo.testCase + "\\..\\putty-0.67-installer.msi"

# In or relative to current user's downloads folder:
msi_file = os.path.expanduser("~\\Downloads\\putty-0.67-installer.msi")



def main():
    global installer_file_path
    global installer_file_name



    test.startSection("Register msiexec.exe as AUT")
    try:
        msiexec_path = os.path.join(os.getenv("WINDIR"), "system32")
        args = [os.path.join(os.getenv("SQUISH_PREFIX"), "bin", "squishserver"), "--config", "addAUT", "msiexec", msiexec_path]
        test.log("Executing: %s" % args)
        output, exit_code = os_capture(args)
        if exit_code != 0:
            test.fatal("Registering installer as AUT failed:")
            test.fatal("Output: %s" % output)
            return
    finally:
        test.endSection()



    test.startSection("Start msiexec.ece as AUT to install our .msi")
    try:
        startApplication('msiexec /i "%s"' % msi_file)
    finally:
        test.endSection()



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

def os_capture(args, cwd=None):
    if cwd is None:
        cwd = os.getcwd()
    proc = subprocess.Popen(
        args=args,
        cwd=cwd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    stdout = proc.communicate()[0]

    return stdout, proc.returncode
test.py