Call a custom function
In your scripts, you can call custom JavaScript functions to perform operations that are not possible in the codeless scripts.
Create a custom function
Create custom JavaScript function, defining what arguments the script has to pass to the function.
In the function, you can use JavaScript, as well as functions from the LFT, AI, and Web namespaces in the OpenText Functional Testing for Developers JavaScript reference. For details, see the OpenText Functional Testing for Developers Help Center.
Note: The following methods in the AI namespace are not supported in Design:
snapshot, getVisibleText, getTextLocations, doubleClick, longClick, highlightMatches, rightClick, verifyImageExists, verifyImageMatch, verifyImageMatchWithMask, registerCustomClass.
To create a JavaScript function
-
In the script editor, add a Call function step. You can type
Call function
or click the Add step buttonbutton and select Call function.
-
Hover over the word function, click the down arrow that is displayed, and click Create new function.
-
Provide a name for the function, and specify where in the Function tree to save it.
For function naming rules, see Naming conventions.
Note: After you create the first function, a Functions node is added to the Script tree. You can create subsequent functions from the Add new button in this node.
-
Define your function's signature; the arguments it expects to receive from the calling script.
-
Every function's first implicit argument is application, which is passed to the function by the calling script. This argument describes the application being tested, providing the context for the function.
-
In the Function details pane, specify any other arguments to pass from the script to the function.
Argument names are case-sensitive and must be unique within the function.
Argument names must not include JavaScript keywords or reserved keywords, such as
require
,LFT
,Web
andAI
. -
(Optional) Define default values for the arguments. All argument values are of type String.
-
(Optional) Provide descriptions of the function and its arguments, to assist in understanding and maintaining the function.
-
-
Write your function, using JavaScript and the OpenText Functional Testing for Developers SDK. The Web, LFT, and AI namespaces from this SDK are automatically included in your tests.
Because this function is asynchronous, preface each statement that affects the application with the
await
keyword. This instructs the function to wait until the operation is completed in the application before moving on to the next statement. Otherwise, when the function runs the next statement, the application may not yet be in the expected state.For example:
Copy code// Navigate to product page
await application.navigate("https://advantageonlineshopping.com/#/product/20");
await application.sync();
// Open sign in form
await application.$(AI.AiObject({
aiClass: AI.AiTypes.profile
})).click(); -
(Optional) Add a
return
statement to specify a value to return to the script.If you plan to set this value into a script parameter, the return value must be of type String.
Returning any other type of value into a parameter will result in an error.
Modify a custom function
To modify an existing function, open it from the Script tree.
Alternatively, in the script editor, select a Call function step, and in the step editing pane click the Open code button.
Modify the function following the rules described in the Create a custom function section.
Modifying any of the following may affect scripts that call this function:
-
Function name or location
-
Function argument names
-
The number of function arguments
-
Whether the function returns a value
The scripts are not automatically updated. Review and adjust the function calls in the scripts to accommodate any modifications.
Call a custom function from a codeless script
-
In the script editor, add a Call function step. You can type
Call function
or click the Add step buttonbutton and select Call function.
-
Hover over the word function, and click the down arrow that is displayed, and select from the available functions.
Alternatively, you can select a function in the step editing pane.
Note: You must select the function from the list and not type the name of the function in the step. If the function name changes, you must reselect it from the list of functions in the step.
The step is automatically constructed with the arguments required for the function.
-
Update the argument values this step passes to the function.
The step must include the exact number of argument defined in the function and their values. The arguments do not need to be listed in the order they are defined in the function.
-
Provide values for any arguments that do not have default values.
-
(Optional) Replace default values with values more relevant for your script's flow.
You can provide constant values, or enter @ to select an existing script parameter or create a new one.
-
-
(Optional) If the function returns a string value, you can set it into an output parameter.
Click Return result into output parameter and select an existing parameter or create a new one.
Example
One scenario you could use a JavaScript function for is to check the accuracy of a total sum in a shopping cart. In your script, find the prices of each item and assign them to parameters. Then, pass the prices to a function like the one below to add up the prices and return the sum to the script.
In the script, verify that the total amount in the shopping cart is equal to the sum returned by the function.
The JavaScript function might look like this:
Function Sum (application, Value1, Value2)
const cleanedVal = Value1.replace(/[$,]/g, '');
const cleanedVa2 = Value2.replace(/[$,]/g, '');
return (Number(cleanedVal)+Number(cleanedVa2)).toString();
See also: