Find Out How

Print Friendly Version

Miscellaneous Scripts

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

 

Miscellaneous Scripts

 
  1. Script 1: Stop Test Clips (and Optionally Clip Repeats) via Script

In those cases where you need a script to skip playing the remainder of the current repeat of the current test clip, you would do:

$context.currentClip.end();

If the clip was serially repeating and you want to end it completely, including stopping it from repeating any further, you would do:

$context.currentClip.end();
$context.currentClip.endRepeat();

If the clip was serially repeating and you wanted the current repeat to finish, but not repeat any more after that, you would do:

$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");

  1. Script 2: Clear response from prior message
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();

  1. Script 3: Random Think Times

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.


  1. Script 4: Abort a script and consider it an error with custom error text

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), you could instead do:

$context.abortScript("Error text here");

  1. Script 3: Override a target’s use of HTTP/HTTPS for the instance of a clip

This 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");

  1. Script 4: Stop a composition via script

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();

  1. Script 5: Encoding Text

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);

  1. Script 6: 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() %%}

  1. Script 7: 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);
  }
}

  1. Script 8: Determine Dates 30 and 31 Days from Now

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.


  1. Script 9: Reading a Clip Property into a Test

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

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

 
  1. Script 10: Trim Spaces in a String

The brief script on the right 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+$/,"");
}

  1. Script 11: Conditional Logic using Chains and Random Numbers

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);
}

  1. Script 12: Replace Spaces with Plus (+) Signs

This simple 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,'+');

  1. Script 13: Dynamically Set Chain Repeats

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);