6.12. Windows Object API

[Important]Windows-specific

The Windows Object API is only available for the Squish for Windows editions.

[Note]Terminology

The Squish documentation uses the term widget when referring to GUI objects. Windows developers may be more familiar with the terms control and container, both of which are covered by the term widget in the Squish documentation.

[Note]Windows Object API Function Parameters

For all of the Windows Object API functions that take an objectOrName argument, this argument can be a reference to an object or the name of an object as a string—the object must be (or name) a native Windows object.

Some of the Windows Object API functions can take a modifierState argument which indicates which special keys are pressed at the time of a mouse click. And some of the functions can also take a mouseButton argument which indicates which mouse button was clicked.

The modifierState can be one or more of the following: Modifier.None, Modifier.Alt, Modifier.Control, Modifier.Shift. If more than one is used they must be OR-d together, for example, Modifier.Alt|Modifier.Shift. The form shown here works e.g., Modifier::Control, and for Tcl use the enum function, e.g., enum Modifier Control.

The mouseButton can be any one of: MouseButton.None, MouseButton.Wheel, MouseButton.LeftButton, MouseButton.MiddleButton, MouseButton.RightButton.

The form shown above works for Python and JavaScript.

For Perl use this: MouseButton::LeftButton, etc.

For Ruby use this: MouseButton::LEFT_BUTTON, etc.

For Tcl use this: enum MouseButton LeftButton, etc.

Here are some quick links to the Windows Object API's functions and properties:

clickButton(objectOrName);

This function clicks the objectOrName widget which should be the name of a button or a reference to a button object.

clickItem(objectOrName, itemText);

clickItem(objectOrName, itemText, mouseButton);

clickItem(objectOrName, itemText, mouseButton, modifierState);

This function clicks the objectOrName widget's item that has the specified itemText. This will work for any widget that has a text property.

By default MouseButton.LeftButton is used, but this can be changed by specifying the optional mouseButton argument. Similarly a default modifier state of Modifier.None is used, but this can be changed by specifying the modifierState argument. Note that to specify the modifier, the button must also be specified. See Windows Object API Function Parameters for which values are valid for the mouseButton argument and for the modifierState argument.

collapse(objectOrName);

If the objectOrName is a tree or tree item, it is collapsed so that none of its child items (if it has any) are visible. If the objectOrName is a combobox, its pop-up list of items is hidden. (See also collapseItem, expandItem, and expand.)

collapseItem(objectOrName, itemName);

The objectOrName should be a tree or tree item. It is collapsed so that none of the child items of the item with the specified itemText are visible. (See also expandItem and expand.)

doubleClick(objectOrName);

doubleClick(objectOrName, x, y);

doubleClick(objectOrName, x, y, mouseButton);

doubleClick(objectOrName, x, y, mouseButton, modifierState);

This function double-clicks the objectOrName widget.

By default the widget is double-clicked in the middle, but this can be changed by passing widget-relative coordinates, x and y. By default MouseButton.LeftButton is used, but this can be changed by specifying the optional mouseButton argument. Similarly a default modifier state of Modifier.None is used, but this can be changed by specifying the modifierState argument. Note that to specify the button, the coordinates must also be specified; and to specify the modifier state, the coordinates and the button must also be specified. See Windows Object API Function Parameters for which values are valid for the mouseButton argument and for the modifierState argument.

doubleClickItem(objectOrName, itemText);

doubleClickItem(objectOrName, itemText, mouseButton);

doubleClickItem(objectOrName, itemText, mouseButton, modifierState);

This function double-clicks the objectOrName widget's item that has the specified itemText. This will work for any widget that has a text property.

By default MouseButton.LeftButton is used, but this can be changed by specifying the optional mouseButton argument. Similarly a default modifier state of Modifier.None is used, but this can be changed by specifying the modifierState argument. Note that to specify the modifier, the button must also be specified. See Windows Object API Function Parameters for which values are valid for the mouseButton argument and for the modifierState argument.

dragAndDrop(source_objectOrName, target_objectOrName);

dragAndDrop(source_objectOrName, sx, sy, target_objectOrName);

dragAndDrop(source_objectOrName, target_objectOrName, tx, ty);

dragAndDrop(source_objectOrName, sx, sy, target_objectOrName, tx, ty);

This function performs a drag and drop operation. It begins by initiating a drag on the source_objectOrName widget and then it does the drop on the target_objectOrName widget. By default the drag is from the middle of the source object to the middle of the target object, unless specific coordinates are given, in which case the coordinates are honored.

Either, both, or neither sets of coordinates can be specified. If the source coordinates are given, the drag starts at position sx and sy (in the source_objectOrName widget's coordinates), and if the target coordinates are given the drop occurs at position tx and ty (in the target_objectOrName widget's coordinates).

expand(objectOrName);

If the objectOrName is a tree or tree item, it is expanded to show its child items (if it has any). If the objectOrName is a combobox, its pop-up list of items is shown. (See also expandItem, collapseItem, and collapse.)

expandItem(objectOrName, itemText);

The objectOrName should be a tree or tree item. It is expanded to show the item with the specified itemText, and that item's child items (if it has any). (See also collapseItem and collapse.)

Image grabWidget(object);

This function takes a screenshot of the object window (or widget) and returns it as an Image Object (Section 6.3.12).

See the waitForObject and findObject functions for how to get an object reference to a window or widget.

focusedWindow();

This function returns an object reference to the window that has the keyboard focus. (See also setFocusedWindow.)

installEventHandler(eventName, handlerFunctionName);

This function installs a global event handler. The script function named in handlerFunctionName (which must be passed as a string, not as a function reference, except for Python, which supports passing a function reference, too), will be called when an event of the eventName type occurs.

The eventName can be the name of any of the following event types:

Crash
This event occurs if the AUT crashes.
DialogOpened
This event occurs when a dialog window is opened; a "dialog window" is a window with the (extended) styles DS_MODALFRAME + WS_EX_DLGMODALFRAME + WS_EX_WINDOWEDGE + WS_EX_CONTROLPARENT or DS_SYSMODAL + DS_SETFOREGROUND + WS_EX_TOPMOST + WS_EX_WINDOWEDGE + WS_EX_CONTROLPARENT (styles can be checked with Microsoft's Spy++ tool from Visual Studio).
MessageBoxOpened
This event is occurs when Squish detects that a dialog window was opened (i.e. the same requirements as given in the explanation of the DialogOpened event apply), but the dialog window also matches a certain structure. The exact structure depends on the particular GUI toolkit but it usually means that the dialog should contain just text and some standard buttons like OK and Cancel.
Timeout
This event occurs when the Squish response timeout is reached.

The function named in handlerFunctionName is called with a single argument—the object on which the event occurred.

[Note]Python-specific

In Python scripts, you can specify the callback function to invoke by passing an actual function or a lambda function.

For examples see How to Use Event Handlers (Section 5.10).

[Important]The AUT Must be Running

The installEventHandler function will only work if it is called after the AUT has been started (e.g., using the startApplication function).

mouseClick(objectOrName);

mouseClick(objectOrName, mouseButton);

mouseClick(objectOrName, mouseButton, modifierState);

mouseClick(objectOrName, x, y, mouseButton);

mouseClick(objectOrName, x, y, mouseButton, modifierState);

This function clicks the mouse on the specified objectOrName widget.

If only the objectOrName is specified, then the object is clicked in the middle by the MouseButton.LeftButton. All the other usages require the mouseButton to be specified, optionally preceded by by object-relative coordinates, x and y which say where the click takes place, and optionally followed by a modifierState (which defaults to Modifier.None if not specified).

See Windows Object API Function Parameters for which values are valid for the mouseButton argument and for the modifierState argument.

mouseDrag(objectOrName, dx, dy);

mouseDrag(objectOrName, x, y, dx, dy);

mouseDrag(objectOrName, x, y, dx, dy, mouseButton);

mouseDrag(objectOrName, x, y, dx, dy, mouseButton, modifierState);

This function performs a mouse drag from the objectOrName widget to a position that is horizontally offset by dx pixels and vertically offset by dy pixels from the object's center (or from position x, y in the objectOrName widget's coordinates if these are specified). If you need to specify the mouse button or modifier state but don't want to specify the origin (because the center of the object is sufficient), pass 0 for the x coordinate and for the y coordinate.

By default the mouse is dragged from the middle of the object, but this can be changed by passing object-relative coordinates, x and y. The amount to drag, dx, dy, must be specified. By default MouseButton.LeftButton is used, but this can be changed by specifying the optional mouseButton argument. Similarly a default modifier state of Modifier.None is used, but this can be changed by specifying the modifierState argument. Note that to specify the modifier, the button must also be specified. See Windows Object API Function Parameters for which values are valid for the mouseButton argument and for the modifierState argument.

mousePress();

mousePress(mouseButton);

mousePress(mouseButton, modifierState);

mousePress(x, y, mouseButton);

mousePress(x, y, mouseButton, modifierState);

mousePress(objectOrName);

mousePress(objectOrName, mouseButton);

mousePress(objectOrName, mouseButton, modifierState);

mousePress(objectOrName, x, y, mouseButton);

mousePress(objectOrName, x, y, mouseButton, modifierState);

This function performs a mouse press. All the parameters are optional excepting that either both or neither of the coordinates must be present.

If x and y coordinates are given, the press takes place at those coordinates, either relative to the objectOrName object if one is specified, or relative to the current screen otherwise. If no coordinates are given the press takes place on the middle of the objectOrName object if one is specified, or wherever the mouse happens to be otherwise.

By default MouseButton.LeftButton is used, but this can be changed by specifying the optional mouseButton argument. Similarly a default modifier state of no modifiers is used, but this can be changed by specifying the modifierState argument. See Windows Object API Function Parameters for which values are valid for the mouseButton argument and for the modifierState argument.

mouseRelease();

mouseRelease(mouseButton);

mouseRelease(mouseButton, modifierState);

mouseRelease(x, y, mouseButton);

mouseRelease(x, y, mouseButton, modifierState);

mouseRelease(objectOrName);

mouseRelease(objectOrName, mouseButton);

mouseRelease(objectOrName, mouseButton, modifierState);

mouseRelease(objectOrName, x, y, mouseButton);

mouseRelease(objectOrName, x, y, mouseButton, modifierState);

This function performs a mouse release. All the parameters are optional excepting that either both or neither of the coordinates must be present.

If x and y coordinates are given, the release takes place at those coordinates, either relative to the objectOrName object if one is specified, or relative to the current screen otherwise. If no coordinates are given the release takes place on the middle of the objectOrName object if one is specified, or wherever the mouse happens to be otherwise.

By default MouseButton.LeftButton is used, but this can be changed by specifying the optional mouseButton argument. Similarly a default modifier state of no modifiers is used, but this can be changed by specifying the modifierState argument. See Windows Object API Function Parameters for which values are valid for the mouseButton argument and for the modifierState argument.

Object nativeObject

Squish automatically adds a property called nativeObject to every Windows Object's list of properties. The native object provides access to all the underlying object's methods and properties.

For examples of how to use the nativeObject see How to Use the nativeObject Property (Section 5.5.2).

setForegroundWindow(objectOrName);

This function tries to bring the objectOrName window into the foreground, that is, raises it above any overlapping windows if possible.

setFocusedWindow(objectOrName);

This function gives the keyboard focus to the objectOrName window or to the window that contains the objectOrName widget. (See also focusedWindow.)

setValue(objectOrName, integer);

This function sets the value of the, objectOrName editable widget to the given integer value.

This function can be used to set the numeric value in a spinbox, or to set the value of a TrackBar (a slider widget) or of a ScrollBar.

type(objectOrName, text);

This function types the specified text (as if the user had used the keyboard) into the objectOrName editable widget. If the text is surrounded by angle brackets (<>), it is interpreted as a key combination, e.g "<Ctrl+Return>". The input is case-sensitive, so type(object, "R") is different from type(object, "r"). (For a list of the supported special keys see the nativeType function's documentation.)

uninstallEventHandler(eventName, handlerFunctionName);

This function uninstalls an event handler that has been previously installed using installEventHandler.