JavaScript Web HTTP Scripts

You can record Web HTTP/HTML Vuser scripts in JavaScript. You can also regenerate a C language script to convert it to JavaScript. The JavaScript engine is Google's V8 engine and is ECMAScript 5 compatible.

Note: VuGen's code regeneration overwrites all manual changes that you made to a recorded script; it only regenerates the recorded functions.

JavaScript Vuser scripts support asynchronous behavior and functionality. For details, see Create an Asynchronous Vuser Script.

JavaScript Vuser scripts support Virtual User Tables (VTS). For more information see VTS and Parameterization

To run a script faster, you can disable JavaScript debugging at
Runtime Settings -> Preference -> JavaScript.

Recording your Vuser Script in JavaScript

When you create a Web HTTP/HTML Vuser script, you are notified that you can choose to generate the script in JavaScript. See General Options Tab to disable the message.

To record your script in JavaScript: 

  1. Click Record > Recording Options. Select Script in the General menu on the left.
  2. Select JavaScript in the Scripting Language dropdown list.

  3. Click OK.

After recording the script, the default C files are replaced by JS files.

Note: Once you have recorded your script the Language option in the Recording Options dialog box is disabled and cannot be changed. You can change the language of a script after it is recorded only by regenerating it.

Back to top

Converting a C Script to JavaScript

You convert a C script to JavaScript by regenerating a recorded script. You cannot convert a script that you coded.

Caution: If you changed a recorded script, your changes are lost when you regenerate.

To regenerate C scripts into JavaScript: 

  1. Click or Record > Regenerate Script. A warning appears that any changes to the code are overwritten.
  2. Click Options. The Regenerate Options dialog box appears.
  3. In the Script section, select the target language to convert the script to.
  4. Click OK in the Regenerate Options dialog box. Click OK to approve regenerating the code.

Back to top

JavaScript Function Libraries

The native LoadRunner JavaScript library includes string functions, database connectivity functions and XML functions among others. Native library functions are available from four global objects:

  • web: Web/HTTP Protocol API.
  • lr:  LoadRunner native API. 
  • vtc:  VTC multiple connections API.
  • lrvtc:  VTC single connections API.

Custom Libraries

In addition to LoadRunner standard functions, you can create JavaScript function libraries, or use JavaScript Language libraries such as underscore, lodash, moment, and so on.

Before you can invoke custom functions, add your custom library into the Extra files section. Your custom library must have a 'js' extension.

To activate auto-completion on your library, right click on the js file name and select Add to Parsing List.

Note: If you regenerate your script in another language, any additional files, such as a user-defined .js file, are removed from the parsing list.

External Libraries

In addition, the lr.require and lr.loadLibrary functions enable you to import JavaScript files at runtime. Third-party libraries are supported for libraries that are not MS-Windows or document-object based. Therefore, asynchronous functions and libraries, such as promise libraries, are not supported.

lr.loadLibrary is used to “include” a single file into the script. The code is evaluated and its definitions are added to the global scope of the runtime context. For example: lr. loadLibrary (‘external.js’);. lr.loadLibrary can be used for browser based libraries.

lr.require loads a CommonJS file or NPM module into a string, then wraps it with a function and evaluates it. Additional required files are loaded recursively along the “require” link.

Example: var lodash = lr.require("..\\node_modules\\lodash");

var sum = lodash.sum([1,2,3,4]);

Back to top

See also: