Work with function libraries
This topic describes how to create TruClient functions and function libraries for your TruClient 2.0 - Web scripts.
About functions and libraries
A function is a reusable group of steps that together perform a task. TruClient includes predefined API functions to perform standard actions in your scripts. In addition, you can define your own custom functions.
You store created functions in libraries. A library can be local or global:
-
A local library can be accessed by the script in which it was created.
-
A global library can be shared locally or on the network, and accessed by all TruClient 2.0 scripts that can access the share location.
Note:
-
Library names and function names can contain only alphanumeric characters and underscore.
-
Recursive calls to functions are not supported.
Create functions in the Function libraries view
You can add unspecified functions to a library, and then define the function arguments.
To create a function:
-
In the Development window, open the Function libraries view.
-
If the library where you want to add the function already exists, select it in the dropdown list of libraries.
If you want to add a new library, click the New library button
and enter a name for the library. This creates a local library for the current script.
-
Click the New Function button
and enter a name for the function. The new function is added to the list in the Function libraries view.
-
Click More in the new function to open the Step editor.
-
Define the function arguments. Give the arguments meaningful names, so that when you are using the function, it is clear what value you need to specify.
-
Close the Step editor.
-
In the new function, click the Add button
to add the substep. Drag or paste steps into the substep box.
Reference a function argument within the function
To access the function arguments within the function, use the syntax FuncArgs.<arg_name>
.
For example:
Note: This is only relevant when using the JavaScript editor.
Create functions from script steps
You can save steps in your script as a custom function. This enables you to reuse the task when required, without needing to re-record the steps.
To create functions from script steps:
- In the Development window editing area, select the consecutive steps in the script that you want to include in the function.
- Right-click the highlighted steps and select Group Into > New Function.
-
In the Create New Function dialog box, define the required information:
UI Element Description Function name Specify the function name. Add to library -
Select existing. Select the library in which to save the new function.
-
Create new. Define the name of a new library in which to save the new function.
Replace steps with function call If selected, the selected steps are replaced in the script with a Call function step that calls the new function. -
-
To assign steps that contain arguments, expand the argument section, and insert the function
FuncArgs.<argument name>
.Tip: To insert JavaScript as the argument value, click the JavaScript editor button
button to access the editor.
Use custom functions in your script
You can add custom functions to your script from local or global libraries. To add from a global library, the library must first be imported. For details, see Use global function libraries .
Note: If you rename a library, you must manually modify all references to it in your script steps.
To add a custom function to your script:
-
Open the Steps box > Functions tab.
-
Drag the Call function step into your script.
-
In the added step, define the library and function name, and any other relevant information.
Use global function libraries
Saving custom functions in a global function library enables the functions to be used by multiple TruClient 2.0 scripts.
When using a global library in your script, you can add and edit the functions as you would in a local library. When you save your script, the changes are updated to the global library file.
A global library can be stored on the local machine, or on the network. When importing a global library, you specify the path type in the Import global function library window, enabling use in Windows and/or Linux. Path types are as follows:
To export a local library as a global library:
-
From the views list at the top of the Development window, select Functions libraries.
-
In the library list, select a local library that you want to export as a global library.
-
Use the relevant export method to export the local library to a location on your machine or network.
Export option Method After export, the current local library is replaced with the created global library. Click the Export as global function library, and link from current script button .
The local library is exported as a global library. The current local library remains unchanged.
Click the arrow to the right of the button and select Export as global function library from the dropdown list.
-
In the Export function library window, give a name to the exported file (this is the file name, it does not affect the library name). Click Save. The exported library is saved as a global function library (.tc2) file.
-
If you chose to Export as global function library, and link from current script:
-
The Import global function library window opens. Define the relevant path information. For details, see Path type.
-
The original entry in the library list is changed to a link to the global library. Global libraries are indicated by (global) added as a suffix to the library name. Hover over the global library name to see the path.
-
To import a global library to your script:
-
In the Functions libraries view, click the Import global function library button
.
-
Browse to a saved .tc2 file and select it.
-
In the displayed Import global function library window, select a Path type from the dropdown list. For details, see Path type.
-
In the Path field, define the relevant path or paths, if required.
-
Click OK. The library is added to the available list of function libraries, with (global) added as a suffix to the library name.
Note: You can delete a library from your script using the Delete library button . For global libraries, this removes the link to the library, but does not delete the global library file.
Copy a test object between object repositories
The local object repository contains the object definitions for a script and the script's local library. A global object repository contains the object definitions for one global library. There are as many independent global object repositories as there are global libraries. If you copy and paste a step that contains a test object within one object repository, the new step is linked to the test object of the step you copied.
However, if you copy steps containing test objects between object repositories, you create new, independent test objects that point to the same UI element. These copies are not linked to each other. To keep these objects pointing to the same UI element:
- If you change or update one of the test objects in the local library or the script, you must update that object in the global library or libraries.
- If you change or update one of the test objects in a global library, you must update that object in the script or local library and in any other global library that contains the same object.
Examples of pasting between object repositories that create new test objects are:
- Copying from a script or local library to a global library.
- Copying from a global library to a script or local library.
- Copying between global libraries.
If you copy more than one step between object repositories, you create as many copies of the test object as there were in the steps you pasted. This applies even if you copy more than one step in a single paste operation.
See also: