Output parameters

更新时间:
复制 MD 格式

In actual stress testing scenarios, you can extract output parameters from the response of a request and use the extracted information as the input of the next request. You can define multiple output parameters in a single API. This topic describes how to extract output parameters.

Configure output parameters

imageThe following table describes the elements in an output parameter.

Element

Description

Output Parameter Name

The name of the output parameter. The name can contain letters, digits, underscores (_), and hyphens (-). The name must start with a letter.

Source

The parsing method of the response. You can select one of the following parsing methods:

  • Body : JSON: Parses the response body in JSON format.

  • Body : TEXT: Parses the response body as plain text.

  • Header : K/V: Parses the response header as a key-value pair.

    Note

    If you select Header : K/V, the parsing expression is the key of the desired header.

  • Cookie : K/V: Parses the cookie as a key-value pair.

  • HTTP Status Code: Extracts the status code from the response.

Parsing Expression

The parsing expression that is used to extract the content that you want to use as the output parameter from the response.

Nth match

This parameter applies only when Source is set to Body : TEXT. If the parsing expression finds multiple matches, use this parameter to select one. Indexing starts at 0. A negative number, such as -n, selects the nth match from the end. The value must be in the range [-99, 99]. To select a random match, enter random.

Body : JSON parsing method

The Body : JSON source type supports responses with Content-Type headers of application/json and text/json.

PTS supports both the new and old versions of the JSONPath syntax. To highlight the differences between the new and old versions of JSONPath expressions, the following sections provide the sample JSONPath structure and the syntax and descriptions of JSONPath expressions for both versions.

We recommend that you use the new version of the JSONPath syntax.

JSONPath expressions (new version)

The following sample code provides an example of the new version of the JSONPath structure. The following table describes the syntax and descriptions of the new version of JSONPath expressions.

Sample JSONPath structure

{
    "menu":{
        "header":"SVG Viewer",
        "items":[
            {
                "id":0
            },
            {
                "id":1,
                "label":"Open New"
            },
            null,
            {
                "id":2,
                "label":"Zoom In"
            },
            {
                "id":3,
                "label":"Zoom Out"
            },
            {
                "id":4,
                "label":"Original View"
            },
            null,
            {
                "id":5
            },
            {
                "id":6
            },
            {
                "id":7
            },
            null,
            {
                "id":8,
                "label":"Find..."
            },
            {
                "id":9,
                "label":"Find Again"
            },
            {
                "id":10
            },
            {
                "id":11,
                "label":"Copy Again"
            },
            {
                "id":12,
                "label":"Copy SVG"
            },
            {
                "id":13,
                "label":"View SVG"
            },
            {
                "id":14,
                "label":"View Source"
            },
            {
                "id":15,
                "label":"Save As"
            },
            null,
            {
                "id":16
            },
            {
                "id":17,
                "label":"About Adobe CVG Viewer..."
            }
        ]
    }
}

JSONPath syntax

Description

$

The root object. For example, if you specify the $.menu.header expression, SVG Viewer is returned.

[num]

Retrieval of an element in an array. The num variable specifies a number. For example, if you specify the $.menu.items[0] expression, the {"id":0} value is returned.

[num0,num1,num2...]

Retrieval of multiple elements in an array. The num variable specifies a number. In this case, multiple elements in the array are returned. For example, if you specify the $.menu.items[0,3] expression, an array that consists of two elements is returned and the element specified by 3 is {"id":2,"label":"Zoom In"}.

[start:end]

Retrieval of a range of elements defined by a start element and an end element in an array. For example, if you specify the $.menu.items[0:3] expression, an array that consists of four elements is returned. The element specified by 1 is {"id":1,"label":"Open New"} and the element specified by 2 is null.

[?(@.key)]

Retrieval of non-empty object attributes. For example, if you specify the $.menu.items[?(@.label)] expression, an array that consists of 12 elements is returned. The 12 elements have non-empty labels and the first element is {"id":1,"label":"Open New"}.

[?(@.key > 123)]

Retrieval of numeric-type object attributes by a comparison operation. The supported comparison operators include equal to (=), not equal to (!=), greater than (>), greater than or equal to (>=), less than (<), and less than and equal to (<=). For example, if you specify the $.menu.items[?(@.id > 5)] expression, an array that consists of 12 elements whose IDs are greater than 5 is returned and the first element is {"id":6}.

[?(@.key = '123')]

Retrieval of string-type object attributes by a comparison operation. The supported comparison operators include equal to (=), not equal to (!=), greater than (>), greater than or equal to (>=), less than (<), and less than and equal to (<=). For example, if you specify the $.menu.items[?(@.label = 'Copy Again')] expression, an array that consists of one element is returned and the returned element is {"id":11,"label":"Copy Again"}.

[?(@.key like 'aa%')]

Retrieval of string-type object attributes by a like clause. You can use only a percentage sign (%) as the wildcard. The not like clauses are supported. For example, if you specify the $.menu.items[?(@.label like 'Copy%')] expression, an array that consists of two elements is returned. The two elements have labels that contain the Copy string, and the first element is {"id":11,"label":"Copy Again"}. If you specify the $.menu.items[?(@.label not like 'Copy%')] expression, an array that consists of 10 elements is returned. The 10 elements have labels that does not contain the Copy string, and the first element is {"id":1,"label":"Open New"}.

[?(@.key rlike 'regexpr')]

Retrieval of string-type object attributes by a regular expression. The Java Development Kit (JDK) syntax is used and the not rlike clauses are supported. For example, if you specify the $.menu.items[?(@.label rlike 'Copy ([A-Z]+)')] expression, an array that consists of one element whose label contains the Copy string and uppercase letters is returned. The returned element is {"id":12,"label":"Copy SVG"}. If you specify the $.menu.items[?(@.label not rlike 'Copy ([A-Z]+)')] expression, an array that consists of 11 elements is returned and the first element is {"id":1,"label":"Open New"}.

[?(@.key in ('v0', 'v1'))]

Retrieval of object attributes by an in clause. String- and numeric-type objects are supported. The not in clauses are supported. For example, if you specify the $.menu.items[?(@.id in (1, 2))] expression, an array that consists of two elements whose IDs are 1 and 2 is returned and the first element is {"id":1,"label":"Open New"}. If you specify the $.menu.items[?(@.id not in (1, 2))] expression, an array that consists of 16 elements whose IDs are not 1 or 2 is returned and the first element is {"id":0}.

[?(@.key between 234 and 456)]

Retrieval of object attributes by a between clause. Numeric-type objects are supported. The not between clauses are supported. For example, if you specify the :$.menu.items[?(@.id between 0 and 3)] expression, an array that consists of four elements whose IDs are in the range of 0 to 3 is returned and the first element is {"id":0}.

length() or size()

Retrieval of the number of elements in an array. For example, if you specify the $.menu.items.size() or $.menu.items.length() expression, 22 is returned.

..

Retrieval of a specific attribute. For example, if you specify the $.menu.items..id expression, an array that consists of 18 elements is returned. Each element corresponds to an ID value.

*

Retrieval of all attributes in an object. For example, if you specify the $.menu.items.* expression, all data in the items array is returned.

randomIndex()

Retrieval of a random element in an array. For example, if you specify the $.menu.items[randomIndex()] expression, a random element of the items array is returned.

['key']

Retrieval of an attribute. For example, if you specify the $['menu']['items'] expression, the value of the items array is returned.

['key0','key1']

Retrieval of multiple attributes. For example, if you specify the $['menu']['items'][3]['id', 'label'] expression, an array that consists of two elements is returned. The returned ID is 2 and the returned label is Zoom In.

Note

The semantics of the $.store.book[0].title and $['store']['book'][0]['title'] expressions are the same.

JSONPath expressions (old version)

The following sample code provides an example of the old version of the JSONPath structure. The following table describes the syntax and descriptions of the old version of JSONPath expressions.

{
    "info": "success",
    "message": "Succeeded.",
    "data": {
        "id":13509, "code":0,
        "items": [
            {"name": "name1", "value": "1234"},
            {"name": "name2", "value": "8448"},
            {"name": "name3", "value": "1298"},
            {"name": "name4", "value": "3049"},
            {"name": "name5", "value": "7648"}
        ]
    }
}
            

Output parameter value

Old version of the parsing expression

New version of the parsing expression (for comparison)

The value of the info object.

info

$.info

The ID value in the data array.

data.id

$.data.id

The value of the first element in the items array. Relative position is supported.

data.items[0].value

$.data.items[0].value

The value of the second to last element in the items array. Relative position is supported.

data.items[-2].value

Not supported

The entire items array.

data.items[ALL]

$.data.items[*]

A random element in the items array.

data.items[RANDOM]

$.data.items[randomIndex()]

Body : TEXT parsing method

The Body : TEXT source type supports any text format and uses a regular expression to extract data. If the regular expression finds multiple matches, you can specify which one to use by its index. The default index is 0, which selects the first match.

The following sample code provides an example API response:

<input name="id" value="34729XXXX">
<input name="token" value="acdfo4dfopasdf44dXXXX">
...
<script>
    var planId=4587;
    var planId=5689;
    var planId=8906;
</script>
            

Desired value

Parsing Expression

Nth match

The value whose name is id.

<input name="id" value="([0-9]*)">

0

The value whose name is token.

name="token" value="([A-Za-z0-9]*)"

0

The value of the third PlanID element.

var planId=([0-9]*);

2

The value of a random PlanID element.

var planId=([0-9]*);

random

Format conventions:

  • The scan starts from the beginning of the response body. The first value that matches the expression is used, and the scan stops. Therefore, ensure your parsing expression is specific enough to match only the desired value, unless any of the multiple matches is acceptable.

  • The matching text cannot contain special characters, such as braces ({}) and parentheses (()).

  • When you use a regular expression to extract information from a JSON response, you do not need to add a space after the colon between the key and value in a key-value pair. You can add a space after the colon between the key and value in each key-value pair to optimize the visual effects of JSON data.

Cookie : K/V and Header : K/V parsing methods

The Cookie : K/V and Header : K/V parsing methods are used to extract the Cookie field and Header field, respectively. You can specify the keys that you want to extract in the parsing expression. For example, if the Cookie field contains token=1234;path=/ and you want to extract the value of the token, enter token in the parsing expression.

Status Code parsing method

You can use the Status Code parsing method to extract the response status codes of requests. In most cases, use this parsing method for check points or conditional jumps. Different page information is returned for different status codes.

Output parameters of JDBC nodes

To extract an output parameter from SQL query results, select the Body : JSON source. The results are structured as the following JSON object:

{
    "data": [
        
         {
             "Column 1": "value",
             "Column 2": "value"
           },
         
           {
              "Column 1": "value",
              "Column 2": "value"
           }
           ...
  ]
}

For example, the following table describes the SQL query results. Assume that you want to parse the value (name1) in the name column of the first row as the output parameter.

id

name

1

name1

2

name2

Use the following parsing expression:

$.data[0].name

Debug output parameters

If you cannot determine whether a parsing expression is correct, you can debug the parsing expression and the associated output parameter in scenario debugging. Perform the following steps:

  • On the PTS Scenario page, click Debug at the bottom. In the Debug Scenario dialog box that appears, click the scenario name. In the details pane on the right, select an API and click Click to test the regular expression for the output parameter.

  • In the dialog box that appears, select the source type, enter the parsing expression, specify the nth match, and enter an output parameter name. Click Test Expression. Check the matching result against the response details to verify that the extracted content is correct.

  • To save the configuration, click Sync Output Parameter to add this expression to the API's list of output parameters.

    Note

    After scenario debugging is complete, if you synchronized any output parameters, return to the Scenario settings tab of the stress test. On the Output Parameter Definition tab for that API, you must set a name for the synchronized output parameter.

Example: Configure output parameters

In the financial management business, you want to recommend appropriate services based on the consumption capability of customers. In this case, you must extract the consumption capability level of each customer as the output parameter and use the output parameter to recommend services.

Perform the following steps:

  1. Log on to the PTS console, choose Performance Test > Create Scenario, and then click PTS.

  2. On the PTS Scenario page, add APIs for retrieving spending power and recommending products. For example, name them Spending Power (with output parameter) and Product Recommendation, and enter their respective URLs.

  3. For the Spending Power (with output parameter) API, configure an output parameter to extract the spending power information from the response. Enter an output parameter name (for example, output) and the parsing expression (for example, data.items[0].value).

  4. In the lower-left corner of the PTS Scenario page, click Parameter list. On the Custom parameters tab, you can view the output parameter that you created. Click the parameter name (for example, output) or the copy icon 复制图标 to copy the parameter reference to your clipboard.

  5. In the configuration for the Product Recommendation API, go to the Body Definition tab. Paste the copied parameter reference into the body.image

    You can also edit the body content, such as combining strings, parameters, or functions.