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/qt/addressbook"

    registerAUT(aut, path)
    startApplication(aut)

    openWindowsSystemMenu()
    o = getWindowsSystemMenuItem("Wiederherstellen", 1000)
    test.log(o.text)

    activateWindowsSystemMenuItem("Wiederherstellen")

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

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

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=4322):
    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
    os.system(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.