Replacing Squish script functions (Python)

Last edited on

Overview

The functions provided by Squish can be replaced. (The original functions can still be used if the replacement is done correctly.)

This can be a convenient way to modify the behavior of the commands that Squish records, without having to modify the recorded script code.

Functions can only be replaced after the AUT has started

When replacing Squish script functions it is important to do this after the application has been started by Squish. (This is because Squish creates some of its functions after the AUT has started, so these are not available to be replaced until that point.)

A typical test case script that replaced some Squish functions would look like this:

def main():
    startApplication("myapp")
    source(findFile("scripts", "squish_func_replace.py"))
    # ...
test.py

(The source() function is discussed below.)

Replacing functions

The source() call is used to execute another script inside this one. The other script can create variables, functions, classes, and so on, that can then be used as if they had been created in the current test case. The advantage of having all this in a separate script is that the script can then easily be used by multiple test cases.

For example, the statement...

source(findFile("scripts", "squish_func_replace.py"))

...loads the file squish_func_replace.py which contains the replacement functions and their "registration".

Here is an example of such a file which shows how to replace Squish's mouseClick() function with a customized version:

# Make sure that the AUT is running
try:
    mouseClick;
except:
    raise 'Function "mouseClick" not found. Please start an AUT before loading this file via source().'

def mouseClick(obj, xoff, yoff, modifierState, button):
    # Perform a nativeMouseClick() instead
    test.log("Performing nativeMouseClick")
    nativeMouseClick(obj, xoff, yoff, modifierState, button);

# The original function can be accessed like this:
#  squish.mouseClick(...)
squish_func_replace.py

Note that the original mouseClick() function can still be accessed like this:

squish.mouseClick(...)