Skip to content

Response

formatting...

Module contains step implementations that handles Request task responses.

Arguments

expression

Valid for:

Name Type Description Default
expected_matches int number of expected matches that expression should get, a value of -1 means Any number of matches 1
as_json bool always return matches as a JSON list, by default if there's a single match it will be returned as a string False

Allow status codes

Set allowed response status codes for the latest defined request in the scenario.

By default 200 is the only allowed respoonse status code. By prefixing a code with minus (-), it will be removed from the list of allowed response status codes.

Example

Then get request with name "test-get-1" from endpoint "/api/test"
And allow response status "200,302"

Then get request with name "test-failed-get-2" from endpoint "/api/non-existing"
And allow response status "-200,404"

Arguments

Name Type Description Default
status_list str

comma separated list of integers

required

Allow status codes table

Set allowed response status codes for the latest defined requests based on a data table.

Specifies a comma separeated list of allowed return codes for the latest requests in a data table.

By default 200 is the only allowed respoonse status code. By prefixing a code with minus (-), it will be removed from the list of allowed response status codes.

Number of rows in the table specifies which of the latest defined requests the allowed response status codes should map to.

The table must have the column header status.

Example

Then get request with name "test-get-1" from endpoint "/api/test"
Then get request with name "test-get-2" from endpoint "/api/test"
And allow response status
 | status   |
 | 200, 302 |
 | 200,404  |

Allowed response status codes for test-get-1 is now 200 and 302, and for test-get-2 is now 200 and 404.

Content type

Set the content type of a response, instead of guessing it.

This is applicable when there is a step_response_validate or step_response_save is included in the scenario, and is valid only for the latest defined request.

Example

And set response content type to "json"
And set response content type to "application/json"
And set response content type to "xml"
And set response content type to "application/xml"
And set response content type to "plain"
And set response content type to "text/plain"

Arguments

Name Type Description Default
content_type ContentType

expected content type of response

required

Save

Save metadata (header) or payload (body) value from a response in a variable.

This step expression is the same as Save matches if match_with is set to .*.

With this step it is possible to change variable values and as such use values from a response later on in the load test.

The Request task preceded by this step will fail if the specified expression has no or more than one match.

Example

Then save response metadata "$.Authentication" in variable "HEADER_AUTHENTICATION"

Then save response payload "$.Result.ShipmentId" in variable "ShipmentId"

Then save response payload "//measurement[0]/id/text()" in "xmlMeasurementId"

Arguments

Name Type Description Default
target ResponseTarget

metadata or payload, depending on which part of the response should be used

required
expression str

JSON path or XPath expression for finding the property

required
variable str

name of the already initialized variable to save the value in

required

Save matches

Save specified parts of a response, either from meta data (header) or payload (body), in a variable.

With this step it is possible to change variable values and as such use values from a response later on in the load test.

The Request task preceded by this step will fail if the specified expression has no or more than one match.

Example

# only token is matched and saved in TOKEN, by using regexp match groups
And value for variable "TOKEN" is "none"
Then save response metadata "$.Authentication" that matches "Bearer (.*)$" in variabel "TOKEN"

# the whole value is saved, as long as Authentication starts with "Bearer"
And value for variable "HEADER_AUTHENTICATION" is "none"
Then save response metadata "$.Authentication" that matches "^Bearer .*$" in variable "HEADER_AUTHENTICATION"

# only the numerical suffix is saved in the variable
And value for variable "AtomicIntegerIncrementer.measurermentId" is "1"
Then save response payload "$.measurement.id" that matches "^cpu([\d]+)$" in "measurementId"

# the whole value is saved, as long as the value starts with "cpu"
And value for variable "measurementId" is "0"
Then save response payload "$.measurement.id" that matches "^cpu[\d]+$" in "measurementId"

# xpath example
And value for variable "xmlMeasurementId" is "none"
Then save response payload "//measurement[0]/id/text() | content_type=xml" that matches "^cpu[\d]+$" in "xmlMeasurementId"

Arguments

Name Type Description Default
target ResponseTarget

metadata or payload, depending on which part of the response should be used

required
expression str

JSON path or XPath expression for finding the property

required
match_with str

static value or a regular expression

required
variable str

name of the already initialized variable to save the value in

required

Save matches optional

Save specified parts of a response, either from meta data (header) or payload (body), in a variable.

With this step it is possible to change variable values and as such use values from a response later on in the load test.

The Request task preceded by this step will not fail if the specified expression has no or more than one match, the value of variable will be set to default_value.

Example

# only token is matched and saved in TOKEN, by using regexp match groups
And value for variable "TOKEN" is "none"
Then save response metadata "$.Authentication" that matches "Bearer (.*)$" in variabel "TOKEN"

# the whole value is saved, as long as Authentication starts with "Bearer"
And value for variable "HEADER_AUTHENTICATION" is "none"
Then save response metadata "$.Authentication" that matches "^Bearer .*$" in variable "HEADER_AUTHENTICATION"

# only the numerical suffix is saved in the variable
And value for variable "AtomicIntegerIncrementer.measurermentId" is "1"
Then save response payload "$.measurement.id" that matches "^cpu([\d]+)$" in "measurementId"

# the whole value is saved, as long as the value starts with "cpu"
And value for variable "measurementId" is "0"
Then save response payload "$.measurement.id" that matches "^cpu[\d]+$" in "measurementId"

# xpath example
And value for variable "xmlMeasurementId" is "none"
Then save response payload "//measurement[0]/id/text() | content_type=xml" that matches "^cpu[\d]+$" in "xmlMeasurementId"

Arguments

Name Type Description Default
target ResponseTarget

metadata or payload, depending on which part of the response should be used

required
expression str

JSON path or XPath expression for finding the property

required
match_with str

static value or a regular expression

required
variable str

name of the already initialized variable to save the value in

required
default_value str

value to set if there is no match

required

Save optional

Save metadata (header) or payload (body) value from a response in a variable.

This step expression is the same as Save matches optional if match_with is set to .*.

With this step it is possible to change variable values and as such use values from a response later on in the load test.

The Request task preceded by this step will not fail if the specified expression has no or more than one match, the value of variable will be set to default_value.

Example

Then save optional response metadata "$.Authentication" in variable "HEADER_AUTHENTICATION" with default value "none"

Then save optional response payload "$.Result.ShipmentId" in variable "ShipmentId" with default value "SH1395865"

Then save optional response payload "//measurement[0]/id/text()" in "xmlMeasurementId" with default value "foobar"

Arguments

Name Type Description Default
target ResponseTarget

metadata or payload, depending on which part of the response should be used

required
expression str

JSON path or XPath expression for finding the property

required
variable str

name of the already initialized variable to save the value in

required
default_value str

value to set if there is no match

required

Validate

Fail a request based on the value of a response meta data (header) or payload (body).

Example

And restart scenario on failure
When response metadata "$.['content-type']" is not ".*application/json.*" fail request
When response metadata "$.['x-test-command']" is "abort" fail request
When response metadata "$.Authentication" is not "Bearer .*$" fail request

And stop user on failure
When response payload "$.measurement.id" is not "cpu[0-9]+" fail request
When response payload "$.success" is "false" fail request
When response payload "/root/measurement[@id="cpu"]/success/text()" is "'false'" fail request

Arguments

Name Type Description Default
target ResponseTarget

metadata or payload, depending on which part of the response should be used

required
expression str

JSON path or XPath expression for finding the property

required
condition enum

"is" or "is not" depending on negative or postive matching

required
match_with str

static value or a regular expression

required