Replacing Squish script functions (JavaScript)

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.)

AUTs can be started in one of two ways. The old-style approach is to check the test suite setting's "Automatically start the AUT" checkbox; the new-style approach is to do a call to the startApplication() function at or near the beginning of the test case's main() function. If the new-style approach is being used, functions should only be replaced after the startApplication() call has been made.

When "Automatically start the AUT" is disabled

When the setting "Automatically start the AUT" is disabled, a typical test case script that replaced some Squish functions would look like this:

function main() {
    startApplication("myapp");
    source(findFile("scripts", "squish_func_replace.js"));
    // ...
}
test.js

(The source() function is discussed below.)

When "Automatically start the AUT" is enabled

When the setting "Automatically start the AUT" is enabled the script would look similar to this:

function main() {
    source(findFile("scripts", "squish_func_replace.js"));
    // ...
}
test.js

(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, this statement:

source(findFile("scripts", "squish_func_replace.js"));

Loads the file squish_func_replace.js 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:

try { // Make sure that the AUT is running
    mouseClick;
} catch (e) {
    throw('Function "mouseClick" not found. Please start an AUT before loading this file via source().');
}

// Store original and overridden functions in an object
var custom_functions = {
    original: {},

    mouseClick: function(obj, xoff, yoff, modifierState, button) {
        // Perform a nativeMouseClick() instead
        nativeMouseClick(obj, xoff, yoff, modifierState, button);
    }
};

// Store the original function in the custom_functions object.
// The original can then be accessed as:
//	custom_functions.original.mouseClick(...);
custom_functions.original.mouseClick = mouseClick;

// Set our replacement function:
mouseClick = custom_functions.mouseClick;
squish_func_replace.js

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

custom_functions.original.mouseClick(...);