Share this page

# Work with function libraries

This topic describes how to create TruClient functions and function libraries for your TruClient 2.0 - Web scripts.

In this topic:

## 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:

1. In the Development window, open the Function libraries view.

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

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

4. Click More in the new function to open the Step editor.

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

6. Close the Step editor.

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

1. In the Development window editing area, select the consecutive steps in the script that you want to include in the function.
2. Right-click the highlighted steps and select Group Into > New Function.

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

4. 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:

1. Open the Steps box > Functions tab.

2. Drag the Call function step into your script.

3. 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:

Path type | Description
--- | ---
Local relative path | Use when the global library is located on the local machine. The global library is placed in a default base folder. When you select the library for import, the relative path is determined by TruClient (according to the operating system in use, Windows or Linux). This is the recommended way to use global libraries. Default base folder locations: Windows: <installdir>\TruClient2Web\GlobalLibraries Linux:/var/lib/LoadGenerator/TruClient2Web/GlobalLibraries Note: You can change the base path value in the TC2_GLOBAL_LIBRARY_BASE_PATH environment variable on the machine.
Local absolute path | Use when the global library is located on the local machine. You specify the absolute file path to the global library for Windows and Linux. The value for Linux can be empty, if you don't want to replay the current script on a Linux load generator.
Network path | Use when the global library is located on a different machine. You specify the network path (SMB protocol) from which to load the global library. Note: Only anonymous access is supported. If authentication is required on the network sharing server, map the network path to a local file, and use as a local global library.

To export a local library as a global library:

1. From the views list at the top of the Development window, select Functions libraries.

2. In the library list, select a local library that you want to export as a global library.

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

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

5. If you chose to Export as global function library, and link from current script:

   1. The Import global function library window opens. Define the relevant path information. For details, see Path type.

   2. 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:

1. In the Functions libraries view, click the Import global function library button .

2. Browse to a saved .tc2 file and select it.

3. In the displayed Import global function library window, select a Path type from the dropdown list. For details, see Path type.

4. In the Path field, define the relevant path or paths, if required.

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

- Event handlers
- Steps box
- Work with JavaScript
- Work with C functions
- TruClient 2.0 - Web functions

https://admhelp.microfocus.com VuGen,Professional Performance Engineering,Performance Engineering for Developers 8942
Tips to improve your search results... OpenText™ Virtual User Generator Help Center OpenText™ VuGen Help 26.1-26.3 All View Local Help Center View Online Help Center Version: