Share this page

# Use custom JavaScript and C code

This task describes how to insert code into a TruClient script. You can insert code into a pre-existing step as part of a step argument or insert steps that are completely comprised of external code (C or JavaScript).

In this topic:

## Insert code into a step

You can insert JavaScript code into preexisting steps in the arguments fields of most steps. This allows you to perform multiple customizations.

## Insert code-only steps

You can insert steps comprised entirely of code into your script. To do so, click Step in the toolbar, select Miscellaneous and drag the Eval JavaScript, Eval C, or Eval JS on Object step to the required location in the script.

The Eval JS on Object step runs the JavaScript code after the specified Object has loaded. We recommend avoiding Eval C and using JavaScript instead wherever possible. You can refer to this object as the variable "object" in the JavaScript code within the step.

Example:

The following code creates a variable called amount that generates a random number between 1 and 5. You can then use this variable in the argument fields of other steps.

    var amount = Math.floor(Math.random() * 5) + 1;

## Edit multi-line JavaScript code

Click the JS icon in the element where you want to insert or edit multi-line JavaScript.

Do not use a `return` statement. The last line evaluated is the return value. In the example below, the value of the Text argument is "`Testtask-"+datetime`

## Set up commonly used JavaScript functions

You can set up commonly used functions in the JS-functions.js file.

Note: Libraries now replace the older method of keeping custom code in JS-functions.js (but this file is still supported for backward compatibility). For details, see TruClient functions and function libraries.

For example:

`/**
* Returns a random integer between min (inclusive) and max (inclusive)
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}`

and in a step you can set variables:

`var ToMonth = getRandomInt(6,12);
var ToDayOfMonth = getRandomInt(1,30);`

or you can directly use the functions from JS-functions.js file when setting an object value:

getRandomInt(6,12) + "/" + getRandomInt(1,30) + "/16" // random dates from 6/1/2016 to 12/30/2016

## See also

- Work with JavaScript in TruClient scripts
- TruClient functions
- TruClient step arguments

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: