How do I parse an HTML response for validation or to extract a value?

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

Step 1

Insert a Script following the Message whose response you wish to validate.

 

Step 2

Ensure that the Script doesn't play until the Message has completed.

If you are using a Sequenced Test Clip, this will always be the case and there is nothing special to do.

If you are using a Timed Test Clip, then you must either put the Message and the Script together into a Chain or put a Checkpoint between the Message and the Script. You could also insert a Delay between the Message and Script that you know will always be long enough, but this may not be consistently reliable if the response times to your Message vary.

 

Step 3

Your script should look something like the following:

// This Script looks at the response to the Message that
// precedes it (it assumes that a Message precedes it),
// and verifies that the HTML response to the Message
// has a title of "Page 1".If it does not, it aborts
// play of the current Test Composition.

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

// Extract the title from the HTML response. Note that
// the Xpath processor for HTML requires that the
// XPath be in all uppercase, and the search is not
// case sensitive.
var pageTitle = msg.getResponse(msg.RESPONSE_HTTP_BODY_AS_HTML,         "//HTML/HEAD/TITLE");

// Was the title found in the HTML, and if so is it the
// expected value?
if (pageTitle == null)
{
  // No title tag was found in the HTML.
  // Abort the Test Composition with an error.
  $context.composition.abort("Response for "
  + msg.name + ": HTML page has no title.");
}
else if (pageTitle != "Page 1")
{
  // The title is not what we are expecting.
  // Abort the Test Composition with an error.
  // Include the actual page title in the "details"
  // of the error.
  $context.composition.abort("Response for "
  + msg.name + ": HTML page title does not match.",
  pageTitle);
}
else
{
  // The title is what we're expecting.
  // Put an informational message into the Result.
  $context.result.postMessage($context.result.LEVEL_INFO,
  "Response for " + msg.name + " passed validation.");
}

Note that your Script could also put the extracted value into a property, insert it into another message, or do anything with that value that a Script can do.

 

Here is an example of the Script following the Message in a Chain:

Attachments

    Outcomes