Extract a SessionID

Use the following script as an example of extracting a sessionID and placing that extracted value into a Custom Property. For example, from a DWR engine.js file.
Refer to Script to learn how to add a script to a test clip and test composition.
Note: You can get more information about DWR syntax at http://getahead.org/dwr/documentation.

You can Download and import the XML for the entire Extract A Session ID Composition or copy and paste the script code from here.

XML can be imported using the CloudTest > Repository toolbar > Import icon.

Refer to Script to learn how to add a script to a test clip and test composition.



  1. In lines 1-2, set a variable, that gets the object representing the item named "engine.js" in the clip.
var dwrEngineJsMessage = $context.currentClip.getChild('engine.js');
  1. In line 3, get the body of the HTTP response.
var dwrEngineJsBody = dwrEngineJsMessage.getResponse(Message.RESPONSE_HTTP_BODY);
  1. On line 9, set a variable, prefix .
var prefix = 'dwr.engine._origScriptSessionId = "';
  1. On line 10, set a variable, startIndex , to the location after the prefix in the message body.
var startIndex = dwrEngineJsBody.indexOf(prefix) + prefix.length;
  1. On line 11, set a variable, endIndex , to the location the first double-quote following the prefix.
var endIndex = dwrEngineJsBody.indexOf('"', startIndex + 1);
  1. Extract the session ID from the body of the HTTP response.
scriptSessionId = dwrEngineJsBody.substring(startIndex, endIndex);
  1. DWR expects the browser to generate a random suffix. So, generate a random number and append it to the session ID. This is copied directly from engine.js (see _getScriptSessionId() in the DWR documentation).
scriptSessionId = scriptSessionId + Math.floor(Math.random() * 1000);
  1. Write the result to the log for informational purposes.
$context.result.postMessage($context.result.LEVEL_INFO, "Extracted scriptSessionId " + scriptSessionId);
  1. Set the clip property.
$prop.set("MessageClip", "scriptSessionId", scriptSessionId);