TruWeb JavaScript SDK

This section describes the TruWeb JavaScript SDK.

Note: If you are writing the script manually, make sure it uses UTF-8 encoding without BOM.

load.initialize(callback)

Registers the given callback to be run in the initialization phase of the test. The callback may return a promise that will be used to determine if the callback has succeeded or failed.

load.initialize(async function (){

//This code will run during Vuser initialization

});

Back to top

load.finalize(callback)

Registers the given callback to be run in the finalization phase of the test. The callback may return a promise which will be used to determine if the callback has succeeded or failed.

load.finalize(async function(){

//This code will run during Vuser finalization

});

Back to top

load.action(name, callback)

Registers the given callback as a named action of the script with the given name. The actions will be run in the same order as they are registered by calls to load.action() as currently we do not support any kind of run logic. The callback may return a promise that will be used to determine if the callback has succeeded or failed.

load.action("myAction", async function(){

//This will run first during the run phase

});

load.action("mySecondAction",async function(){

//This will run second during the run phase

});

Back to top

WebRequest

This object allows you to send web requests to the AUT. When creating a WebRequest, you can pass an options object with all the configuration you need for the request. Then you can send the request asynchronously or synchronously.

Example:

const jsonPage = new load.WebRequest({

url: "http://myserver/cgi-bin/json/generate_json_simple.asp",

method: "GET",

returnBody: true

});

const jsonResponse = jsonPage.sendSync();

  

Methods

Back to top

WebResponse

This object is returned as a WebRequest result. You do not need to create it on your own.

Properties

status number The status code of the request.
headers object A key value store of the headers in the response sent by the server.
size number The size (in bytes) of the response.
body string The body of the response. Note that body is available only if the request had the property returnBody set to true.
jsonBody object The body of the response as an object (only applicable when the body is a valid json). Note that jsonBody is available only if the request had the property returnBody set to true.
request WebRequest The WebRequest object that caused this response to be generated.
resources array, object

A list of all the resources that were downloaded as part of the request. Each resource object contains the following fields:

  • url (string). The URL of the resource.
  • status (number). The status code that was returned when the resource was downloaded.
  • size (number). The size (in bytes) of the downloaded resource.
redirectUrls array A list of all the URLs passed through while redirecting to this response
correlations object The results of the correlation applied on the response. For more information, see Correlation results.

Example:

const response1 = request1.sendSync(); //Create a WebResponse

load.log(response1.body); //Print the response body to the console

Methods

Back to top

Transaction

Transactions are the means to measure the time it takes to execute specific, well-defined parts of the script.

constructor(name) : Transaction

Creates a new transaction with the given name.

Note: The new transaction has the NotStarted state. You must call the start() method explicitly to start the transaction.

Properties

name string Always set. The name of the transaction as it was passed to the constructor.
state string

Always set. Can be one of:

  • load.TransactionState.NotStarted. After the transaction is created but not yet started.
  • load.TransactionState.InProgress. After the transaction was started but not yet ended.
  • load.TransactionState.Ended. After the transaction has ended.
startTime number Set on call to start. The UNIX timestamp (in milliseconds) of the engine time on which the transaction has started.
duration number Set on call to stop, set, or update . The number of milliseconds passed from the call to start until the transaction was ended either by a call to end or by the engine.
status string

Always set but you may need to call update to get the updated value. The current status of the transaction, can be one of:

  • load.TransactionStatus.Passed. If the transaction is currently considered passed.
  • load.TransactionStatus.Failed. If the transaction is currently considered failed.

Example:

let transaction = new load.Transaction("My Transaction");

Methods

Back to top

config

A global configuration object that is used to supply various configuration data to the running Vuser.

Properties

Back to top

General Methods

Back to top

Parameters

Parameters are values generated during runtime by the runtime engine and are exposed to the script through the load.params variable.

Each time you use a parameter variable, the next value is loaded automatically based on the next value selection strategy in the parameters definition file.

Example:

var paramValue = load.params.myParam1;

var nextParamValue = load.params.myParam1; //if nextValue is "always" this will be a new value

var someString = `The value of myParam1 is ${load.params.myParam1}`; //You can put it in a string with the standard JavaScript syntax

For more information, see parameters.yml in TruWeb script files.

Back to top

Correlation

WebRequest Correlations

You can specify which values are extracted from the response of a specific WebRequest by providing the correlations option parameter. This parameter is an array of correlation objects which are created using the following helper constructor functions:

Correlation results

The results of all the correlations are merged into a single object in the WebResponse returned from the WebRequest by the correlation name. The name is specified as the first argument of the correlation object constructor.

Note: If a correlation object has the same name as another correlation object, only the last correlation result is saved and a warning is printed to the log.

Each correlation object returns the results in a different format. For the particular format, see the particular correlation object definitions in WebRequest Correlations.

Example:

const documentTitleCorrelation = new load.RegexpCorrelation("title","<title>(.*?)</title>","i");

const myPageRequest = new load.WebRequest({

url: "http://myServer.com/main/product.html",

method: "GET",

returnBody: false, //You don't need to return the body to get the correlations

correlations:[documentTitleCorrelation]

});

 

const myResponse = myPageRequest.sendSync();

 

//The correlation result value is available under the name of the correlation

load.log(`The title of the page is ${myResponse.correlations.title}`);

Back to top

Cookie

An object that encapsulates all the fields of a cookie.

Back to top

Cookies

Allow you to add, remove, or clear the cookies used in web requests.

Back to top

utils

The utils objects has some useful functions that may help you with common scripting tasks.

Methods

Back to top

VTS

The VTS integration API allows you to connect to a VTS server and perform various operations on it such as reading and writing from columns and managing indices.

Back to top

VTSClient

The VTSClient is responsible for issuing commands to the VTS server. Use it to obtain other VTS-related constructs such as VTSColumn and VTSRow.

The client allows you to perform general operations that affect more than one column, row, or field.

Methods

Back to top

VTSColumn

The VTSColumn is a reference to a column in the VTS server. Use this object to perform operations on the underlying column.

Properties

Methods

Back to top

VTSRow

The VTSRow is a reference to a row in the VTS server. Use this object to perform operations on the underlying row.

Properties

Methods

Back to top