RIT - ECMAScript

Many RIT ECMA Script links RTVS at GITHUB

Using ECMAScript

RIT uses ECMAScript. ECMAScript is a standardized version of JavaScript. Other locations where Javascript is used can be found here.

Are you scripting?
When the script is open in Test Factory.

Tip! Try this order:
  1. Ctrl-S to save your script.
  2. F5 to run the script (and see that it fails again :-) )
  3. F10 to quickly jump to the open script again.

Or use a Floating Window of the Test.

It's possible to have Groovy as a scripting language instead of ECMAscript. Groovy in RIT

Custom Functions

The Custom Functions expands the ability of the ECMAscript. Various functions which can be programmed in ECMAscript, are now available as a single function. These functions (plug ins) are implemented in Java.

Figure: An enhanced number of functions available via custom functions.

Several additional functions are implemented via an additional library. This library is available at Custom Functions.

ECMAScript examples

Adding values

We often make use of the Addnumbers application to demonstrate. This service adds two provided numbers to a returning result. But in Javascript:

tags["return"] = arg0+arg1;

Will result in a concatenation of the strings arg0 and arg1. You can easily solve this by using the following:

tags["return"] = +arg0 + +arg1;

Note: The tags["return"] notation is used while return is a reserved word.

A "Must See!" project

When you want to explore ECMAscript that you certainly have to take a closer look to this RIT/ECMAscript project. Lots of examples.
RIT/ECMAscript project

Comment

A comment is possible with the following syntax:

// Source is provided on AS-IS basis.

Print and Tags

The following image shows various aspects of using the print statement in a ECMAscript. The output will be visible in the RIT-execution console. Such statement are very help full when developing/debugging code. The print statement can be seen as a -in line- Log Action.

Figure: Print statement in ECMA script with different tag notation.

Date and Time

To put a date in the RIT-tag 'displayDate':

nowStr = Date(); // Get Now
print ("*** Now in full-string: " + nowStr +"\n" );
// Print now forced format
print ("*** Now formatted: " + now("yyyy-MM-dd HH:mm:ss.S") +"\n" );
pausecomp( 1500  ); // Expect some slight differences in ms delays ...
print ("*** Wait 1500 ms\n");
print ("*** Now again as formatted: " + now("yyyy-MM-dd HH:mm:ss.S") +"\n" );
print ("\n");

or ...

tags["OUTPUT"]=now("yyyy-MM-dd");

Format the date within the script. Check out the formatSDF function as Custom Function.

formatSDFs(dateInMs,formatString) {
	var output=formatString;
	var myDate1=new Date();
	myDate1.setTime(dateInMs);
	year = myDate1.getFullYear().toString();
	month = (myDate1.getMonth() + 1).toString();
	if (month.length<2) month="0"+month;

	day = myDate1.getDate().toString();
	if (day.length<2) day="0"+day;
	hours = myDate1.getHours().toString();
	if (hours.length<2) hours="0"+hours;
	minutes = myDate1.getMinutes().toString();
	if (minutes.length<2) minutes="0"+minutes;
	seconds = myDate1.getSeconds().toString();
	if (seconds.length<2) seconds="0"+seconds;
	mseconds = myDate1.getMilliseconds().toString();
	if (mseconds.length<2) mseconds="0"+mseconds;
	if (mseconds.length<2) mseconds="0"+mseconds;

	output=output.replace("yyyy",year);
	output=output.replace("MM",month);
	output=output.replace("dd",day);

	output=output.replace("hh",hours);
	output=output.replace("mm",minutes);
	output=output.replace("ss",seconds);
	output=output.replace("SSS",mseconds);

	return output;
	}

A code snippet to set and manipulate date information.

function formatDate(dateInMs)
	{
	return formatSDFs(dateInMs,"yyyy/MM/dd hh:mm:ss.SSS");
	};

// Get nowTime1
nowTime1 = formatDateUTC(now("yyyy-MM-dd HH:mm:ss.S"),"yyyy-MM-dd HH:mm:ss.S");
// For pausecomp see elsewhere
// Wait 1500ms
pausecomp( 1500  ); // Expect some slight differences in ms delays ...
// Get nowTime2
nowTime2 = formatDateUTC(now("yyyy-MM-dd HH:mm:ss.S"),"yyyy-MM-dd HH:mm:ss.S");

// Output them
print ("*** nowInMs1 in Miliseconds: " + nowTime1.valueOf() + "\n");
print ("*** nowInMs1 as formattedDate: " + formatDate(nowTime1) + "\n");
print ("*** nowInMs2 in Miliseconds: " + nowTime2.valueOf() + "\n");
print ("*** nowInMs2 as formattedDate: " + formatDate(nowTime2) + "\n");
deltaMs = nowTime2 -nowTime1;
print ("*** Difference: " + deltaMs + "\n");
print ("\n");


// Setting date
dateInputString="2015-03-04 08:07:06";
nowTime3 = formatDateUTC(dateInputString,"yyyy-MM-dd HH:mm:ss");
print("*** Set date and time based on string: "+dateInputString+ "\n");
print("*** Set date in Miliseconds: "+ nowTime3 + "\n" );
print("*** Set date as formatted "+ formatDate(nowTime3)+ "\n");

Formatting ms value in DHHMMSSsss
function formatDHHMMSS(TimeInMs) {
	function ChunkTime(TimeRest,Chunk) {
		return TimeRest - Chunk*Math.floor( TimeRest / Chunk );
	}

	function RestTime(TimeRest,Chunk) {
		return Math.floor( TimeRest / Chunk );
	}
	var responce="";
	var TimeChunk=ChunkTime(TimeInMs,1000);
	var TimeRest=RestTime(TimeInMs,1000);
	if (TimeChunk < 10) TimeChunk="0"+TimeChunk;
	if (TimeChunk < 100) TimeChunk="0"+TimeChunk;
	responce=TimeChunk; // MiliSeconds
	TimeChunk=ChunkTime(TimeRest,60);
	TimeRest=RestTime(TimeRest,60);
	if (TimeChunk < 10) TimeChunk="0"+TimeChunk;
	responce=TimeChunk+"."+responce; // Seconds added
	TimeChunk=ChunkTime(TimeRest,60);
	TimeRest=RestTime(TimeRest,60);
	if (TimeChunk < 10) TimeChunk="0"+TimeChunk;
	responce=TimeChunk+":"+responce; // Minutes added
	TimeChunk=ChunkTime(TimeRest,24);
	TimeRest=RestTime(TimeRest,24);
	responce=TimeChunk+":"+responce; // Hours added
	responce=TimeRest+"D "+responce; // Days added
	return responce;
}

Pause

Pause to be used within a script (as the difference with the Action: Pause).

function pausecomp(millis)
 {
  var date = new Date();
  var curDate = null;
  do { curDate = new Date(); }
  while(curDate-date < millis);
}

(Random) Timestamp

Generate a time-stamp in format 16:24:02

function rndNumber(maxNr) {
	var nrDigits=floor(Math.log(maxNr));
	var multiplier=Math.pow(10, nrDigits);
	var dummy=Math.random()*multiplier;
	while (dummy > maxNr) {
		dummy=Math.random()*multiplier;
		}
	dummy=""+dummy;
	dummy=dummy.substring(0,dummy.indexOf("."));
	if (dummy.length==1) dummy="0"+dummy; // Will give at least 2 digits.
	return dummy;
};

function rndHour() {
	return rndNumber(24);
	};

function rndMinutes() {
	return rndNumber(60);
	};
function rndSeconds() {
	return rndMinutes();
	};


    print(rndHour()+":"+rndMinutes()+":"+rndSeconds()+"\n");
    print(rndNumber(1000)+"\n");
    print(rndNumber(100)+"\n");
    print(rndNumber(10)+"\n");

Replacement of characters

print("Source value: "+tags["SourceValue"]+"\n");
TargetValue=SourceValue.replace(",",""); // Remove comma's
print("Target value: "+tags["TargetValue"]+"\n");

parseInt

Make sure you force the damn (some frustration here) function to the decimal numbering. parseInt("0012") will result in value 10 (using 8 as radix = octal counting)!!

Warning Force the function to decimal numbering!!
a = parseInt("0012",10)

That will give you the expected 12.0
This is especially important in COBOL environments where you have frequent leading zeros.

a = parseInt("FF",16)

Will give you 255.0.

Length Calculation

This piece of code calculates the length of variables. This is done by adding them to the length_fields array. COBOL_integer formats the value into a COBOL type of interger. Code can be found here.

// Calculate length of NAME and put in LEN-NAME;
var length_fields = [ "LEN-NAME" ];

for (var i=0; i<length_fields.length;i++) {
	var curr_len_field=length_fields[i];
	var curr_field=length_fields[i].substr(4);
	var curr_field_length=( tags[curr_field] ).length;
	tags[ curr_len_field ] = COBOL_Integer(curr_field_length,4);
}

CobolSignedTrailingSeparate

An example to format the COBOL signed Trailing Separate via a ECMAscript.

NAME=CobolSignedTrailingSeparate(NAME,10); // length is 10 digits and a sign (total 11).

function CobolSignedTrailingSeparate(numberValue,len) {
	if (numberValue<0) {
		return zfill(-numberValue,len)+"-";
	} else {
		return zfill(numberValue,len)+"+";
	};
}
function zfill(numberValue,len) {
	return (new Array(len - String(numberValue).length + 1 )).join("0").concat(numberValue);
}

Typeof usage

tags["NAME-TYPE"] = ( typeof NAME );

Processing a list #1

This is used when an SOAP element is repeated once or multiple times. Depending on a error percentage the value is placed in the processed list or the error list.

tags.processed = new java.util.ArrayList(); // tag 'list2' should already have been created in the tag data store dialog.
tags.error = new java.util.ArrayList();
processed.clear(); // This line is needed as assigning a list to a tag will include the existing tag value in the list
error.clear(); // This line is needed as assigning a list to a tag will include the existing tag value in the list
for(var i=0; i<list2.size(); i++) {
	var rndValue = Math.random()*100;
	if (rndValue > tags["errorpercentage"] ) {
		processed.add(list2.get(i) );
	} else {
		error.add(list2.get(i) );
	}
}

Processing a list #2

Source files:

See for list processing SOAP Message.

The following images shows how RIT can read a data-file (TransactionInput.txt) and process the contained message. This message contains one element which can be repeating.

Use a subscribe to read the transaction_request.xsd. Apply repeating elements for those elements which are repeated.

Figure: Reading the file

Make sure you are creating a list of elements.

Figure: Select the Append to list of values for every field.

Process the element-lists.

Figure: The Iterate Action, iterating across the obtained list.

Figure: Processing each list item.

Null, 0

Some information on o, Zero, null

tag["VALUE"]=0Assign integer zero to VALUE
tag["VALUE"]="0"Assign string containing zero to VALUE
tag["VALUE"]==0Compare VALUE with integer zero
tag["VALUE"]=null'undefine' VALUE tag

AddNumbers

When you use add numbers and you want to make a calculation based on the inputs use the following ECMAscript in an Function action:

tags["return"]=parseInt(tags["arg0"])+parseInt(tags["arg0"]);

Where the arg0,arg1 and return are quick tags of the 3 fields.

Other sources