Content Extraction

Content Extraction refers to StormForgers ability to parse and extract content of the response data. This feature comes in handy for example if you need dynamically generated content for subsequent requests such as access tokens.

To extract content from a response you have to set the extraction option in the request options parameter. You can use more than one content extraction per request.

  1. specify the desired type (jsonpath, xpath, regexp, header)
  2. specify the names for referencing
  3. specify the expressions

For example:

session.get("/tokens", {
  extraction: {
    jsonpath: {
      "accessToken": "authorization.token",
      "checksum": "authorization.checksum"
    }
  }
});

To use the extracted content in subsequent request you can do so with the getVar function.

session.get("/ping?token=" + session.getVar("accessToken"));

NOTE: Since a launched client will execute only one session and they don't share anything, you cannot reuse extracted data from responses across sessions or clients.

JSONPath

JSONPath is used to find information in a JSON Object.

In your test case definition you can use JSONPath as follows.

Given this JSON response:

{
  "authorization": {
    "token": "s3cret-access-token"
  }
}

and this request options to extract the access token:

session.get("/tokens", {
  extraction: {
    jsonpath: {
      "accessToken": "authorization.token",
    }
  }
});

will make accessToken available as a dynamic data source to the current client within the same session:

session.get("/ping?token=" + session.getVar("accessToken"));

JSONPath Support

Note that we only support a subset of what JSONPath describes.

  • $. at the beginning to indicate the document root, is optional and implied.
  • We do only the dot-notation
  • We do not support script-expressions
  • We do support simple list filters in the form of $.something[?attribute==value].attribute
    • attribute may only be one attribute (no dot-notation path)
    • value does not support quotation
    • see below for examples
  • If the result of your JSONPath expression is not true, false, a string or number, the behavior is considered to be undefined.
  • If your expression does not match anything, getVar will be empty. You can use conditionals like this to act on empty matches: session.if(session.getVar("accessToken"), "=", "", function(context) { });

Here is another example for selecting elements from an array. Given this JSON document

{
  "products": [
    {
      "id": "8e7cd50a-ff44-4518-a02b-51d2dceaafb1",
      "type": "42",
      "name": "iPhone 6",
      "properties": {
        "color": "spacegray"
      }
    },
    {
      "id": "5e41653c-de17-4d6a-9616-5d80257d4b7e",
      "type": "23",
      "name": "iPhone 5",
      "properties": {
        "color": "white"
      }
    }
  ]
}
  • Accessing items by index
    • $.products[0].id will match 8e7cd50a-ff44-4518-a02b-51d2dceaafb1
    • $.products[1].id will match 5e41653c-de17-4d6a-9616-5d80257d4b7e
    • $.products[3].id will result in an empty string
  • Selecting by filter
    • products[?type==23].name will match iPhone 5
    • products[?type==42].properties.color will match spacegray

XPath

XPath (the XML Path language) is a language for finding information in an XML document.

In your test case definition you can use XPath as follows.

Given this response:

<?xml version="1.0" encoding="UTF-8"?>
<users>

  <user>
    <firstname>Giada</firstname>
    <lastname>De Laurentiis</lastname>
    <email>giadalaurentiis@example.com</email>
  </user>

</users>

and this request options:

session.get("/tokens", {
  extraction: {
    xpath: {
      "email": "/users/user[1]/email"
    }
  }
});

Will make email available as a dynamic data source within the same session:

session.put("/user?email=" + session.getVar("email"));

Regular Expression

Regular expressions are used to find a matching string.

In your test case definition you can use regexp as follows.

Given this response:

<p>Welcome john.doe@example.com!</p>
<p>You can confirm your account email through the link below:</p>
<p>
  <a href="http://test/users/confirmation?confirmation_token=noXuMgKei5pPP4wdv5Kq&amp;locale=en">
    Confirm my account
  </a>
</p>

and this request options:

session.get("/tokens", {
  extraction: {
    regexp: {
      "confirmationToken": "confirmation_token=[\w_-]*",
    }
  },
});

will make confirmationToken available as a dynamic data source within the same session:

session.get("/ping?token=" + session.getVar("confirmationToken"));

HTTP Response Header

HTTP Response Header contain meta information of the response message.

In your test case definition you can extract HTTP response header field values using the request option header as follows.

Given this header response:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 211
Connection: keep-alive
Status: 200 OK
Date: Fri, 12 Feb 2016 08:43:01 GMT
X-Powered-By: Phusion Passenger 5.0.23
Server: nginx/1.8.0 + Phusion Passenger 5.0.23

and this request options:

session.get("/tokens", {
  extraction: {
    header: {
      "serverHeader": "server"
    }
  },
});

Will make the HTTP response header Server with the value nginx/1.8.0 + Phusion Passenger 5.0.23 available as a dynamic data source named serverHeader:

session.get("/ping?server=" + session.getVar("serverHeader"));

Keep in mind that dynamic data sources are available in the same session only.

Get started icon

Get Started

New to the StormForger?
With these guides you’ll be up and running in no time!

FAQ icon

FAQ

Already took a look at our FAQs?

Support icon

Support

Are you stuck? Talk to us! We're humans.

We are using cookies to give you the best online experience. If you continue to use this site, you agree to our use of cookies. By declining we will disable all but strictly required cookies. Please see our privacy policy for more details.


Accept Decline