Miscellaneous Scripts

This topic presents miscellaneous CloudTest scripts. You can copy and paste scripts directly into the CloudTest Script Editor using Central > Scripts. Longer scripts have an accompanying image that can be clicked to pop out the full example script. Shorter scripts are presented inline (in the right column).

In those cases where you need a script to skip playing the remainder of the current repeat of the current test clip, use this script: $context.currentClip.end();
If the clip is serially repeating and you want to end it completely and stop it from repeating any further, use this script: $context.currentClip.end(); $context.currentClip.endRepeat();
If the clip is serially repeating and you want the current repeat to finish, but not repeat any more after that, use this script: $context.currentClip.endRepeat();
Optionally, if you want to cause the container to error out, supply error text like in the following example: $context.currentClip.end("There was an Error");
The last line of this script ( msg.clearResponse(); ) is the main part of this script. It is useful for ensuring that large responses do not stay in memory if they are no longer needed. var msg = $context.currentItem.previousItem; msg.clearResponse();
This Random Think Times script resets all subsequent delays to have a random think time between the interval specified in the script below. If you want to set all delays to zero, just set both the minimum and maximum delay times to zero.


This script immediately aborts play of the current Script, and considers the Script to have failed with the given error text. If no error text is provided, the text “Script aborted.” is used. So, for example, in those cases where the eval/throw statement is used (such as in the Validation Scripts), use this script: $context.abortScript("Error text here");
This script overrides a target to either use or not use SSL (i.e. HTTP to HTTPS or vice versa).

$context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("UseSSL", "true");

Stopping a composition using a script is a simple, one-line operation calling the stop() method of the composition object:

Stopping a composition can be used to conditionally terminate a composition on the basis of some error.

$context.composition.stop();
This example shows how to URL encode text. This is useful in cases where certain characters like a plus sign (+) need to be encoded to go on a URL – as %2B. This script is also useful because it shows how to create a function that performs the encoding. This function can be called multiple times in the script.


Another example of using the replace function to do encoding is presented in the right column. This script uses different statements for each replacement rather than combining them together (as the first example shows). var msg = $context.currentItem.previousItem; var origText = msg.getResponse(msg.RESPONSE_TEXT); var newText = origText.substring(pos + 6); newText = newText.replace("%2F","/","g"); newText = newText.replace("+","%2B","g"); newText = newText.replace("%3D","=","g"); $prop.set("MessageClip", "ID", newText);

Math calculations using “ISSE” expressions

The example script on the right is not run in a separate script object – it is run directly in the query string parameter of a message. In this example, it is adding the VUNumber property value to 500.
Note: All properties are a string, so the property needs to be converted to a number before it can be used in math calculations.
{%%expr: Math.floor(Number($context.currentTrack.systemPropertyList.getPropertyValue( "VUNumber")) + 500).toString() %%}

Extract All Links from a Given Response

This creates an array with all of the values of the href tags in the response.
var links = msg.getResponse(msg.RESPONSE_TEXT_AS_HTML, "//@href");
To print out the values in the result as a comma-separated list: var details = links.length +" "+ links.join(); $context.result.postMessage($context.result.LEVEL_INFO, "Links", details);
To print out the values in the result as separate lines: for(var i = 0; i < links.length; i++) { var text = links[i]; if(text.charAt(0) != '#') { $context.result.postMessage($context.result.LEVEL_INFO, text); } }
The Determine dates 30 and 31 days from now script calculates today's date in the individual pieces of month, day, and year in both 2 and 4 digits. It then calculates two other dates in individual parts based on today's date. These two dates are 30 and 31 days into the future from today's date.


The following brief script reads a clip property into a test using a variable.

var auth_id = $prop.value("MessageClip", "auth_id");

This script is used to trim spaces from a string. function trim(stringToTrim) { return stringToTrim.replace(/^\s+|\s+$/g,""); } function ltrim(stringToTrim) { return stringToTrim.replace(/^\s+/,""); } function rtrim(stringToTrim) { return stringToTrim.replace(/\s+$/,""); }
The Conditional Logic using Chains and Random Numbers script plays a chain 10% of the time and turns it off the remaining 90% of the time.


Lines 12-14 generate a random integer contained within the two extremes. function generateRandomNumber(lowerExtreme, upperExtreme) { return Math.floor((Math.random() * (upperExtreme - lowerExtreme)) + lowerExtreme); }
Line 20 creates a variable, chain, that gets the child of the name specified. var chain = $context.currentClip.getChild("DeleteMovie");
Lines 23-25 generate a random number between 1-10. var num1 = "" + generateRandomNumber(1,11); $context.result.postMessage($context.result.LEVEL_INFO, "RandomNum=", num1);
Use the var, num1, from above to play the clip, otherwise set it to 0 using setRepeat . if (num1 != 1) { chain.setRepeat(chain.REPEAT_TIMING_SERIAL,chain.REPEAT_TYPE_COUNT_CONSTANT,0, //THIS means do it ZERO times chain.REPEAT_DISTRIBUTION_CONSTANT,0); }
This script replaces any spaces in the school_id variable with + signs. Such a script is used in cases where parameterized data (i.e. from a CSV file) has spaces in the field, but POST data requires that these spaces be converted to plus signs (+). var school_id = school_id.replace(/\s+/g,'+');
This script precedes a chain in a test clip and sets the number of chain repeats to 10 dynamically during test execution. // This Script sets repeats for the chain that follows it in the Clip. var chain = $context.currentItem.nextItem; chain.setRepeat(chain.REPEAT_TIMING_SERIAL, chain.REPEAT_TYPE_COUNT_CONSTANT, 10, chain.REPEAT_DISTRIBUTION_CONSTANT, 0); $context.result.postMessage($context.result.LEVEL_INFO, "Set repeat spec for item: " + chain.name);
Script 14-1 and Script 14-2 work together to simulate DNS lookups (over a defined set of IP addresses). Script 14-1 randomly picks an IP address from a specified list and sets the target IP address and hostname for the next message with the randomly selected IP information. In cases where both HTTP and HTTPS targets need to be set with the randomly selected IP information, Script 14-2 performs a host override for the second target. For example, if the first target is HTTP, place Script 14-1 before the first message that uses the HTTP target (e.g. using nextItem) and Script 14-2 before the first message that uses the HTTPS target.

In the Script 14-1 example, there are four IP addresses in use:

  • 1.1.1.1

  • 2.2.2.2

  • 3.3.3.3

  • 4.4.4.4

On each test clip, randomly select one of these IP addresses to run the test against.

Once the random IP is selected, Script 14-2 does a host override to point to YOUR_DOMAIN_URL.

Put Script 1 before the first target. Put Script 2 before the second target using that hostname. So, if the first target is HTTP, put Script 1 the first message that uses the HTTP target and Script 2 before the first message that uses the HTTPS target.

Script 14-1

var IPs = [ "1.1.1.1" "2.2.2.2", "3.3.3.3", "4.4.4.4" ] var ip = IPs[Math.floor(IPs.length * (Math.random()))]; $prop.set("MessageClip", "random_ip", ip); $context.result.postMessage($context.result.LEVEL_INFO, "random_ip: " + ip); $context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("HostName", ip); $prop.set("MessageClip", "host_override", " < http://your.domain.com/ > your.domain.com < http://your.domain.com > "); $context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("HttpHostOverride", " < http://your.domain.com/ > your.domain.com < http://your.domain.com > ");

Script 14-2

var ip = $prop.value("MessageClip", "random_ip"); var host_override = $prop.value("MessageClip", "host_override"); $context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("HostName", ip); $context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("HttpHostOverride", host_override);