The Replace a String code

The Replace Version Number script code is used as an example of string replacement.

You can copy the code from this page and paste it into the SOASTA CloudTest > Script Editor, or use the following links to download and import the XML for the entire Replace Version Number Composition or just the Replace Version Number Script. SOASTA CloudTest XML cn be importated using the Import icon on the toolbar.

// The Messages in this Clip have URL parameters with a version number in them.  This
// script replaces the version number which will change with each build, with a constant version
// string, "version" which is used in development and always returns the requested resource.
var versionToReplace = "version20";
var version= "version";

var clipElements = $context.currentClip.children;
for (var i=0; i<clipElements.length; ++i)
{
  var clipElement = clipElements[i];
  if (clipElement.type == "Message")
  {
    var origMessage = clipElement.getMessage(clipElement.MESSAGE_HTTP_BODY);
    var newMessage = origMessage.replace(versionToReplace,version);
    if (origMessage != newMessage)
    {
     clipElement.setMessage(newMessage, clipElement.MESSAGE_HTTP_BODY);
     $context.result.postMessage($context.result.LEVEL_INFO, "Replaced version string in Message.", 
     "Original Message: " + origMessage + "\n\nNew Message: " + newMessage);
    }
  }
}