Work with JavaScript in TruClient 2.0 scripts

This topic contains tips for coding in JavaScript in TruClient scripts.

JavaScript support

The arguments listed in the Arguments section of each step are all JavaScript-based and can accept JavaScript expressions which are evaluated during script replay.

Strings are expressed between quotes. For example, City is interpreted as a variable whereas "City" or 'City' is evaluated as a string.

All other fields that have the option to use the JS argument type can accept JavaScript expressions.

The JavaScript editor uses standard JavaScript functionality, including automatic word completion, and provides access to the TruClient functions. Use the right-click menu to access JavaScript actions.

Back to top

Custom code snippets

In addition to the built-in JavaScript code snippets, you can create your own custom code snippets for reusable blocks of code. When editingJavaScript code, for example, in an Evaluate JavaScript step, you can use the snippets to speed up the coding process.

To use snippets in the JavaScript code:

  1. Open the General settings > Code snippets tab.

  2. Click Add a new code snippet . A new code snippet template is opened, using a default label.

  3. Edit the snippet Label and define Documentation to describe its purpose.

    The label is used to access the snippet in the editor.

  4. Click Save in the General settings dialog box.

  5. When creating or editing an argument for a step in the JavaScript editor, start typing the label for the snippet. Select the snippet from the auto-suggest list, to add the snippet content to the code.

Back to top

Work with the ArgsContext object

Object identification variables and step variables do not share the same context. This means that variables that are defined in one context are not recognized in the other. To use a variable in object identification that is defined in the Arguments section of a step, add the prefix ArgsContext before the variable name.

For example, if the variable firstname is defined as a value for an argument of a step, use the firstname variable in object identification, refer to the variable as ArgsContext.firstname.

For an example of using ArgsContext in a for loop, see Insert and modify loops.

Back to top

Use regular expressions

To use regular expressions in JavaScript code, there are two options:

  • Use the '/' notation: Replace the quotation marks of a string with a slash.

    For example, /LoadRunner/ is a regular expression that will match any string that contains "LoadRunner".

  • If you need to dynamically create a regular expression, you can use the regular expression constructor and specify the string. For example, the equivalent of the above example is RegExp("LoadRunner"). You can use parameters in the RegExp constructor.

  • For the full list of supported regular expressions, see the JavaScript Reference documentation.

Back to top

Use JavaScript in the AUT window

The AUT API provides a common mechanism for handing the AUT window and document for all TruClient protocols.

Note:  

  • For TruClient Web protocols, the API also works without the AUT prefix to support backward compatibility.

  • For security reasons, the AUT.window API cannot access the JavaScript written in the AUT's window.

  • AUT.window. Points to the global window object of the application.

  • AUT.document. The global document object of the application.

When working with JavaScript on the AUT window or document, it is recommended to use the AUT prefix so that all your scripts are aligned.

Example usage:

Add an Evaluate JavaScript step. In the Arguments section enter the following code to get an element by its ID name (the example assumes you have an input element with the ID of ‘myInput’):

var elem = AUT.document.getElementById('myInput');

Back to top

See also: