Attaching to running Windows applications

Last edited on

Overview

With Squish for Windows attaching to running applications can be done in two different ways:

Directly starting an application in attachable state

Start application with “<SQUISH_DIR>\bin\startwinaut.exe”:

Here is the usage information for startwinaut (from 6.2 and higher):

Usage: startwinaut [--port=<port>] [--cwd=<workingdir>] <command> <arg1> <arg2> ...
       startwinaut [--port=<port>] --pid=<PID>
       startwinaut [--port=<port>] --window-title=<window title>

(Also see startwinaut in the Squish manual.)

Example:

Step 1: Start AUT as an attachable AUT on a specific port:

"C:\squish\bin\startwinaut" --port=4444 "--cwd=%USERPROFILE%\Desktop" "%WINDIR%\notepad"
Starting notepad attachable in cmd.exe

(USERPROFILE and WINDIR are environment variables whose values are being inserted into the above command before execution by enclosing them in percent characters as shown above.)

Step 2: Register an “Attachable AUT” for the specific port in the Squish IDE

Step 2.1: In Squish IDE open menus Edit > Server Settings > Manage AUTs...:

Step 2.2: Register an “Attachable AUT” by clicking on “Attachable AUTs” and the “Add” button; choose IP address / hostname of “localhost” (or “127.0.0.1”) and a port number of your choice (“4444” for the rest of this article):

Step 3: Configure “No Application” in test suite settings:

Step 4: Start recording and choose the “Attachable AUT”:

If you already have a test script (instead of recording a new one) you need to change the startApplication() call in it to attachToApplication():

startApplication(mapped_aut_name)

->

attachToApplication(attachable_aut_name)

It is important to note that you must provide the name of an Attachable AUT to attachToApplication(), as shown in Step 2 above.

Making an already running application attachable

Use “<SQUISH_DIR>\bin\startwinaut.exe” with the –pid or –window-title parameter:

startwinaut help:

Usage: startwinaut [--port=<port>] [--cwd=<workingdir>] <command> <arg1> <arg2> ...
       startwinaut [--port=<port>] --pid=<PID>
       startwinaut [--port=<port>] --window-title=<window title>

(Also see startwinaut in the Squish manual.)

Example #1:

"C:\squish\bin\startwinaut" --port=4444 "--window-title=My Application V1"
Make running application attachable in cmd.exe

Example #2:

"C:\squish\bin\startwinaut" --port=4444 --pid=3497
Make running application attachable in cmd.exe

Please note that the process ID “3497” is just an example. You can look up the process ID of a application in Task Manager, or, more comfortably, in Process Explorer. (Also see Finding the process that a window belongs to.)

It is also possible to determine the PID at runtime, as described in Getting PIDs for window titles, executables on Windows.

Attaching to running applications in test scripts

The following example requires the helper script squish_make_win_aut_attachable.

Python

import os
import os.path
import subprocess

def init():
    # Kill all running "attachers":
    os.system("taskkill /f /im _hookwinaut.exe >nul 2>&1")
    os.system("taskkill /f /im _startwinaut.exe >nul 2>&1")

def main():
    # TO BE CONFIGURED:
    squish_make_win_aut_attachable_path = r"C:\Users\myuser\squish_make_win_aut_attachable"

    # TO BE CONFIGURED:
    # Here we are assuming that the current Squish
    # package is Squish for Windows:
    squish_for_windows_path = os.environ["SQUISH_PREFIX"]

    # TO BE CONFIGURED:
    unused_port_for_attaching = "4444"

    # TO BE CONFIGURED:
    # Supported operators:
    #   window_title_contains
    #   window_title_equals
    #   window_title_starts_with
    #   window_title_ends_with
    title_match_operator = "window_title_ends_with"

    # TO BE CONFIGURED:
    window_title = "- Editor"



    p = subprocess.Popen([
            "python",
            os.path.join(squish_make_win_aut_attachable_path, "squish_make_win_aut_attachable.py"),
            squish_for_windows_path,
            unused_port_for_attaching,
            title_match_operator,
            window_title])
    p.communicate()

    if p.returncode != 0:
        test.fatal("An error occurred while running squish_make_win_aut_attachable. See Runner/Server Log (or squishrunner output) for more information.")
        return

    # Simply use the port number as AUT name, too:
    attachable_aut_name = "%s" % unused_port_for_attaching
    host_and_or_port = "%s" % unused_port_for_attaching
    register_attachable_aut(attachable_aut_name, host_and_or_port)

    attachToApplication(attachable_aut_name)

    return

def register_attachable_aut(
    attachable_aut,
    host_and_or_port,
    squishserver_host=None,
    squishserver_port=None):

    cmd = []
    cmd.append(os.environ["SQUISH_PREFIX"] + "/bin/squishrunner")
    if squishserver_host is not None:
        cmd.append("--host")
        cmd.append(squishserver_host)
    if squishserver_port is not None:
        cmd.append("--port=%s" % squishserver_port)
    cmd.append("--config")
    cmd.append("addAttachableAUT")
    cmd.append(attachable_aut)
    cmd.append(host_and_or_port)
    test.log("Executing: %s" % cmd)
    subprocess.Popen(cmd).communicate()
test.py

Related information: