Extraction Scripts

Document created by DPM Admin on Jul 21, 2017
Version 1Show Document
  • View in full screen mode

This topic presents a variety of SOASTA CloudTest extraction 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).

 

Extraction Scripts

  1. Script 1: Extract from Response Body (HTML)

The Extract from Response Body (HTML) script extracts an element from a page response and sets a clip property. Make sure to set the scope of the message is set to Public.

This is a versatile example because you can change the xpath to extract different elements. The xpath below extracts the soasta-password value from a file that returns a response like this:

<div id="soasta-password" style="display:none">rogxWFmmYR</div></div>

  1. The script itself is relatively simple. The first two lines determine the context and search for the id soasta-password:

var msg = $context.currentItem.previousItem;
var passwordValue = msg.getResponse(msg.RESPONSE_HTTP_BODY_AS_HTML, "//*[@id='soasta-password']")[0];

  1. Now that the passwordValue contains the string that you want -- you can put that into a clip property:

$prop.set("MessageClip", "password", passwordValue);

  1. A slight variation of this script that extracted an id from an HTML page:

var msg = $context.currentItem.previousItem.previousItem;
var semesterInputValue = msg.getResponse(msg.RESPONSE_HTTP_BODY_AS_HTML, "//*[@id='term_13']/@value")[0];
// semesterInputValue now contains the string that you want; put it in a clip property
$prop.set("MessageClip", "term", semesterInputValue);
$context.result.postMessage($context.result.LEVEL_INFO, "sem: " + semesterInputValue);

  1. The semesterInputValue now contains the string to put into a clip property:

$prop.set("MessageClip", "term", semesterInputValue);

Click here to view a table with Xpath samples and what they grab.

 
    1. Script 2: Extract from Response Body SubString

This example does multiple substrings of substrings. Line 1 establishes the context where the property will be set, while line 2 declares a variable to store the JSON element. Lines 3-8 retrieve the search string by position. Lines 10-11 set clip property to store the result and posts a message to results.

var msg = $context.currentItem.previousItem; 
var origText = msg.getResponse(msg.RESPONSE_HTTP_BODY); 
var searchString = "<ufid>"; 
var pos = origText.indexOf(searchString); 
var newText = origText.substring(pos + searchString.length);
var pos = newText.indexOf("</ufid>"); 
newText = newText.substring(0, pos); 

$prop.set("MessageClip", "ufid", newText);
$context.result.postMessage($context.result.LEVEL_INFO, "ufid: " + newText);

Click here to view a table with Xpath samples and what they grab.

 
    1. Script 3: Extract from Response Body (JSON)

The Extract from Response Body (JSON) script on the right extracts an element from a JSON page response and sets a clip property.

Line 1 establishes the context where the property will be set, which line 2 declares a variable to store the JSON element. Line 3 sets debug information to be posted into test results, and finally, line 4 sets a clip property using the variable from line 2.

var msg = $context.currentItem.previousItem;
var authid = msg.getResponse(msg.RESPONSE_HTTP_BODY_AS_JSON, "//authid")[0];
$context.result.postMessage($context.result.LEVEL_INFO, "authid: " + authid);
$prop.set("MessageClip", "authid", authid);

  1. Script 4: Extract from Response Header

This script extracts data from a header. This example extracts the orderId from the Location header. The orderId is in the middle of the Location header and needs to be extracted using a substring.


// This Script looks at the response to the Message that
// precedes it and Extracts the OrderID

// Get the Message that immediately precedes this Script.
var msg = $context.currentItem.previousItem;

var origText = msg.getResponse(msg.RESPONSE_HTTP_HEADER, "Location");

var pos = origText.indexOf("orderId=");
var newText = origText.substring(pos + 8);
var pos = newText.indexOf("&deliveryDate");
newText = newText.substring(0, pos);

$prop.set("MessageClip", "OrderID", newText);
$context.result.postMessage($context.result.LEVEL_INFO, "order ID: " + newText);

This script extracts data from a header as well. The difference in this case is that the header value (krypto) is the last item on the Location header.


// This Script looks at the response to the Message that precedes it 
// and extracts the krypto value. For use when nothing is after Krypto 
// on that header line.

// Get the Message that immediately precedes this Script.
var msg = $context.currentItem.previousItem;

var origText = msg.getResponse(msg.RESPONSE_HTTP_HEADER, "Location");

var pos = origText.indexOf("krypto=");
var newText = origText.substring(pos + 7);

$prop.set("MessageClip", "kryptoA", newText);
$context.result.postMessage($context.result.LEVEL_INFO, "kryptoA " + newText);

This script extracts the JSESSIONID from a “Set-Cookie” header.


// This Script looks at the response to the Message that
// precedes it and Extracts the jsessionId cookie

// Get the Message that immediately precedes this Script.
var msg = $context.currentItem.previousItem;

var origText = msg.getResponse(msg.RESPONSE_HTTP_HEADER, "Set-Cookie");

var pos = origText.indexOf("JSESSIONID=");
var newText = origText.substring(pos + 11);
var pos = newText.indexOf(";");
newText = newText.substring(0, pos);

$prop.set("MessageClip", "jsessionId", newText);
$context.result.postMessage($context.result.LEVEL_INFO, "JSESSIONID: " + newText);

  1. Script 5: Extracting Data from a Message TWO Prior

The following brief script extracts information from TWO MESSAGES prior (see multiple previousItems) using xpath and then puts that information into a property. Line 1 establishes the clip element TWO items before the script as the context, while Line 3 declares the variable, semesterInputValue and specifies the xpath ID to find, and then line 5 places the extracted data into a clip property.

var msg = $context.currentItem.previousItem.previousItem;

var semesterInputValue = msg.getResponse(msg.RESPONSE_HTTP_BODY_AS_HTML, "//*[@id='term_13']/@value")[0];

$prop.set("MessageClip", "term", semesterInputValue);

  1. Script 6: Extracting and Encoding a VIEWSTATE and EVENTVALIDATION

This script extracts VIEWSTATE and EVENTVALIDATION values. This is typically a hidden field in an HTML file and frequently used in .NET applications.

Use this script to extract and encode a viewstate value. This is typically a hidden field in an HTML file and frequently used in .NET applications.


  1. Script 7: Extracting and encoding a VIEWSTATE

This script extracts a VIEWSTATE value. This is typically a hidden field in an HTML file and frequently used in .NET applications. This script is sometimes needed (in addition to the prior script) with .NET applications because not every POST in a .NET application has an EVENTVALIDATION parameter—although almost all have VIEWSTATE parameters.

Use this script to extract and encode a viewstate value. This is typically a hidden field in an HTML file and frequently used in .NET applications.


  1. Script 8: Verify Something Exists in a Response before the Extraction Is Attempted

In this script, first verify data before extracting data for use elsewhere, such as in a clipProperty that stores an authID to be used in the nextItem in a test clip. Use searchIndex to find a string in the response.


  1. Script 9: Extract HTTP Status Code from Response

An HTTP status code can be extracted from a response header using the following one-line script:

var StatusCode = msg.getResponse(msg.RESPONSE_HTTP_STATUSCODE);


var StatusCode = msg.getResponse(msg.RESPONSE_HTTP_STATUSCODE);

  1. Script 10: InfoPath Forms Services 2007/MOSS variable extraction

Here is general information how variables work in InfoPath Forms Services (IPFS):

  • IPFS has two main transactions when interacting with a form: Invocation and Postback
  • Editing Session Id, Solution Id, and Canary are dynamic. They will need to be extracted from responses.
  • Editing Session Id, Solution Id, and Canary are used in the postback request. Their values will need to be substituted in the postback body.
  • Editing Session Id and Solution Id do not change between postbacks. They will only need to be extracted for the invocation request.
  • Canary changes after every postback. It needs to be extracted for every request.
 

Here is the initial script that extracts all 3 variables after the initial form load:


Here is the second script that extracts just the CanaryValue after each form postback:


  1. Script 11: Find all JPG/PNG ‘IMG SRC’ Links in an HTML Response

The Find All IMG SRC Links in an HTML Response script takes an HTML response, parses through it and extracts just the PNG and JPG ‘img src’ links. The final result is an array that contains each of the links. An important part of this script is that it removes duplicates from the array so you are left with just the unique links on the page.


Lines 1-3 declare variables for the message, source images (as parsed via XPath), and the message response.

var msg = $context.currentItem.previousItem;

var srcs = msg.getResponse(msg.RESPONSE_TEXT_AS_HTML, "//@src");

var response = msg.getResponse();

// $context.result.postMessage($context.result.LEVEL_INFO, "response: " +response);
// count the number of ‘<img src=’ matches are in the response
var count_matches = response.split("<img src=">.length-1;
$context.result.postMessage($context.result.LEVEL_INFO, ("<img src matches: " +count_matches));
// split the array each time '<img src="' is found
var array_of_images = response.split("<img src=\"");

Lines 8-17 declare a variable to count the matches and creates an array of images.

 

var count_matches = response.split("<img src=").length-1;
$context.result.postMessage($context.result.LEVEL_INFO, ("<img src matches: " +count_matches));

// split the array each time '<img src="' is found
var array_of_images = response.split("<img src=\"");

//since the first element of the array 
// (prior to the first img src) 
// is not interesting, delete it from the array
// array_name.splice(indexNumber,numberOfElementsToRemove
array_of_images.splice(0, 1);

The remaining lines create an array_of_images, and then defines logic to distinguish between PNG and JPG images, (as well as to present a list of IMG SRC links in the file). Pertinent Set debug information for results.

var array_of_images_cleaned = new Array();

for (x in array_of_images)
{

var stringToFind = ".png";

// Determine if the value exists in the response.
var searchIndex = array_of_images[x].indexOf(stringToFind);

if (searchIndex == -1)
{
// $context.result.postMessage($context.result.LEVEL_INFO, ".png not found ");
// string not found, so look for a .jpg

// string to find; hard coded
var stringToFind = ".jpg";

// Determine if the value exists in the response.
var searchIndex = array_of_images[x].indexOf(stringToFind);

if (searchIndex == -1)
{
// string not found

$context.result.postMessage($context.result.LEVEL_INFO, ".jpg not found either ");
}

// If the value is found, output a confirmation 
else {
final_url = array_of_images[x].substring(0, searchIndex+4);
array_of_images_cleaned.push(final_url); 
}

}

// If the value is found, output a confirmation
else {
final_url = array_of_images[x].substring(0, searchIndex+4);
array_of_images_cleaned.push(final_url); 
}
}

$context.result.postMessage($context.result.LEVEL_INFO, "length of array_of_images_cleaned: "+array_of_images_cleaned.length);

// remove duplicates in array by creating a new array
var r = new Array();
o:for(var i = 0, n = array_of_images_cleaned.length; i < n; i++) {
for(var x = i + 1 ; x < n; x++)
{
if(array_of_images_cleaned[x]==array_of_images_cleaned[i]) continue o;
}
r[r.length] = array_of_images_cleaned[i];
}


var count_matches = r.length;
$context.result.postMessage($context.result.LEVEL_INFO, "<img src matches: " +count_matches);

for (x in r)
{
$context.result.postMessage($context.result.LEVEL_INFO, "img src" + x+ ": " +r[x]);
}

  1. Script 12: Extract a Substring Using Regex in JavaScript

The Extract a Substring Using Regular Expression script creates some complex Substrings using regular expression. The value to extract comes from the “ZK - Direct RIA” Ajax library. The relevant value to extract is shown in BOLD in the following sample code: 

 

 

div id="zk-comp-154" class="panels first-set"><div id="zk-comp-155" class="es-tab-panel"><div id="zk-comp-156" class="es-scrollable-tall"><div id="zk-comp-157"

Since the example above contains more than one dynamic value everything with "zk-comp" is extracted in the subsequent script below.


Line 2 establishes a variable, searchString, from the regular expression to the right of the equal sign.

var searchString = new RegExp("div id\=\"zk\-comp\-[0-9]+\" class\=\"panels first\-set\"\>\<div id\=\"zk\-comp\-[0-9]+\" class\=\"es\-tab\-panel\"\>\<div id\=\"zk\-comp\-[0-9]+\" class\=\"es\-scrollable\-tall\"\>");

Lines 5-6 set leftIndex and rightIndex to delimit the search.

var leftIndex = "div id=\"";
var rightIndex = "\"";

  

Lines 8-24 perform the extraction using origText and the indices already extracted. Results are posted for each filter.

var zk157 = null;

if(false != searchString.test(origText))
{

var result = searchString.exec(origText);

zk157= new String(result);

$context.result.postMessage($context.result.LEVEL_INFO, "First Filter: "+ zk157);

zk157=zk157.substring(zk157.indexOf(leftIndex) + leftIndex.length);

$context.result.postMessage($context.result.LEVEL_INFO, "Second Filter: "+ zk157);

zk157=zk157.substring(0, zk157.indexOf(rightIndex));

$context.result.postMessage($context.result.LEVEL_INFO, "zk157= " +zk157);
}
$context.result.postMessage($context.result.LEVEL_INFO, "zk157= " +zk157); 
}

Attachments

    Outcomes