JavaScript

How it works

NeoLoad allows the execution of JavaScript within Virtual Users, which means you may insert a JavaScript action when designing a User Path. The code used may invoke common JavaScript functions declared in the NeoLoad JavaScript libraries.

For more information about writing Java code, see Execute Java code.

For information about the JavaScript Action API, see API overview.

What you can do

The NeoLoad API is mainly used to manipulate NeoLoad variables and set up cookies for the current Virtual User. Basic JavaScript functions allow the handling of strings and dates, calculation, and so on.

For more information about the NeoLoad API features, see API overview.

What you cannot do

The script does not run within the context of the HTML request, so manipulating a request response DOM tree is impossible. Consequently, functions built into browsers for extracting or manipulating the HTML document are not included.

The following is a non-exhaustive list of functions or objects that are not supported:

  • window

  • document (of which document.cookie, document.forms[0], document.getElementById(), ...)

  • navigator

  • alert()

  • confirm()

  • ...

Note: Scripts using these objects compile correctly but their execution halts when it reaches the non-supported object.

JavaScript actions

A JavaScript action may be added to a Virtual User definition. A JavaScript action is identified by a name and a description and contains the script to be executed.

Create a JavaScript action

To create a JavaScript action, follow these steps:

  1. In the Design section, click on the Virtual User tab.

  2. Select the JavaScript logical action.

  3. Drag and drop the action at the desired point in the Virtual Users tree.

NeoLoad creates the action using a skeletal script, which may be either modified or deleted entirely to create a new script.

Edit a JavaScript action

Any change made to a script during the editing process must be validated for them to be applied. Alternatively, changes may be canceled prior to validation, in which case the last script version saved will be reloaded. Scripts may be edited directly in the project files (scripts directory).

A click on the Reload button allows reloading the content of the edited file into NeoLoad.

The JavaScript scripts can be compiled prior to run them. A click on the Open button starts the Operating System editor associated to the .js extension. Compiling checks the script syntax (missing parentheses...) but not its validity (method names ...).

Errors

During a test, a JavaScript action will be flagged as an error in the following cases:

  • The script does not compile (syntax error)

  • The script is invalid (a method does not exist)

  • The script calls the API context.fail(message) method.

JavaScript actions are systematically displayed during the User Path checking process and will appear in the error table if the script has been error-flagged.

JavaScript libraries

JavaScript libraries allow creating functions that can be used in all User Path scripts. Libraries not only allow code sharing between several scripts, they also help reduce memory usage on the Load Generators, since the code stored in the libraries is shared by the users, whereas code contained in scripts is loaded into memory for each Virtual User. It is therefore recommended, wherever possible, to use functions whose code is stored in a library.

Create a JavaScript library

A JavaScript library is identified by its name and library file. You can use an existing file, or create a new, empty file. Each library may contain one or several JavaScript functions.

To create a JavaScript library, follow these steps:

  1. Select the Edit > JS Libraries menu item.

  2. Click on New library.

  3. In the following wizard, enter the name of the library.

  4. Choose either to create a new library or use an existing file, whichever is appropriate.

  5. Confirm the choice to exit the wizard.

  6. Select the created library in the library list.

Edit a JavaScript library

Access the libraries through the Edit > JS Libraries menu item.

If the file has been edited using an external editor, it must be reloaded into the interface using the Reload button.

Use the Compile button to compile the JavaScript libraries prior to execution. Compiling will check the script syntax (missing parentheses...) but not its validity (call method names ...).