NeoLoad JavaScript API with Read and Write to File Examples

NeoLoad JavaScript API used to covert the data, variables, errors and cookies calculations and writing the data to a file and retrieving the data from file. You can achieve most of the logics of the script using NeoLoad User interface but there are some calcuations which can’t be achieved using GUI during that case you can use NeoLoad JavaScript API to change the data based on your requirement. You need to know both JavaScript and Java language to work with NeoLoad JavaScript API.

In NeoLoad there are two ways you can insert Javascript into the Neoload scripts.

First Option is

  1. Open NeoLoad
  2. Click on UserPaths
  3. Click on Create a UserPath and New User Path as shown in the below image.
  4. Right click on Actions, insert as a child and Click on JavaScript.

Create User Path in NeoLoad

JavaScript API

The Second Option is just drag and drop from below selected Javascript to action as shown in the following image.

NeoLoad JavaScript API

How to write to file using Javascript API

Example 1:

For example, you have submitted an order in eCommerce website like google shopping or amazon and you need to capture the order number from the response of the request and you do the same using correlation and using NeoLoad variable extractor and it will be saved in  C_OrderNumber. I will write the C_OrderNumber to file as shown in the below code.

var OrderID = context.variableManager.getValue("C_OrderNumber");
if (OrderID!="<NOT FOUND>") 
var writer = new"C:\\Users\\VJ\\Documents\\NeoLoad Projects\\core\\variables\\OrderID.txt",true);

How to read from file using Javascript API

Example 1:

In this example, I am reading data from the file that is written in the previous example using following code.

var br = new"C:\\Users\\VJ\\Desktop\\OrderID.txt"));
var line;
var OrderIDFromFile;
while ((line = br.readLine()) != null) {
            // process the line.
            OrderIDFromFile = line;      

Example 2

In this example, I am reading data from multiple columns.






var br = new""C:\\Users\\VJ\\Documents\\NeoLoad Projects\\core\\variables\\OrderID.txt""));
var lastLine = "";
var sCurrentLine;
while ((sCurrentLine = br.readLine()) != null)
            lastLine = sCurrentLine;
var columns=[];
logger.error("Invalid value: "+ lastLine);
columns= lastLine.split(",");

//You can use ReceiptNo and OrderID in the request as regular parameter value.

How to use javascript variable in neoload?

Context.variableManager function converts javascript variable to neoload parameter. 

Logging Data using NeoLoad JavaScript API

The logging helps to trace the information when there are errors and to validate the code that you have written. Following are the different loggings you can use for debugging your script.

  1. FATAL
  2. Error
  3. Warning
  4. Info
  5. Debug


  1. logger.error(“Data not found: “+OrderID);
  2. logger.fatal(“Print something here: “+OrderID);
  3. logger.warning(“this is a warning: “+OrderID);
  4.“this is a info: “+OrderID);
  5. logger.debug(“this is debug: “+OrderID);

The above debug functions help you to debug the issues in neoload javascript log.

Are you unable to execute the code or have challenges in writing the logic for your requirement, contact me in the comments below, I will try to resolve the issue.

Leave a Comment