Interacting with the Windows system menu

Last edited on

On Windows most application windows have a system menu that can be made visible by left mouse click on the upper left corner of a window, or by pressing Alt + Space.

Here is an example script that demonstrates how to interact with this system menu:

import os
import sys

def main():
    aut = "addressbook"
    path = os.environ["SQUISH_PREFIX"] + "/examples/qt4/addressbook"

    registerAUT(aut, path)

    o = getWindowsSystemMenuItem("Wiederherstellen", 1000)


def activateWindowsSystemMenuItem(itemText, timeout_ms=20000):
    o = getWindowsSystemMenuItem(itemText, timeout_ms)

def getWindowsSystemMenuItem(itemText, timeout_ms=20000):
    obj_name = "{container={type='Menu'} text='" + itemText + "' type='MenuItem'}"
    waitFor('object.exists("' + obj_name + '")', timeout_ms)
        return findObject(obj_name)
        test.warning("getWindowsSystemMenuItem() requires a hooked application and a Squish for Windows license.")

def openWindowsSystemMenu():
    o = waitForObject("{type='Window'}")
    x = o.x
    y = o.y
    nativeMouseClick(x+10, y+10, MouseButton.LeftButton)

def registerAUT(aut, path, squishserver_host="localhost", squishserver_port=3000):
    s = '"' + os.environ["SQUISH_PREFIX"] + '/bin/squishrunner"'
    s += ' --host ' + squishserver_host
    s += ' port=' + str(squishserver_port)
    s += ' --config addAUT "' + aut + '" "' + path + '"'
    if sys.platform == "win32" and s.endswith('"'):
        s = '"' + s

Please note that the above is a Squish for Qt test script, but the general approach works for all Squish editions if there is a hooked application and if the Squish license key contains a Squish for Windows license.

Please also note that the openWindowsSystemMenu() function is a bit inaccurate and inflexible because it looks for and uses the first application window that it finds. So if your application shows multiple windows you might want to improve this function a bit, for example by allowing to pass a window title to it.