Skip to main content

JSON responses

DrevOps\BehatSteps\Steps\Web\JsonTrait - Source, Example, Helpers

Assert JSON responses with path and schema checks.

  • Assert response is valid JSON format.
  • Assert values at JSONPath expressions.
  • Assert JSONPath existence, type, and element counts.
  • Validate the response against a JSON Schema.

JSONPath expressions use the standard $.path.to[0].value syntax.

The JSON path steps require the optional softcreatr/jsonpath package: composer require --dev softcreatr/jsonpath.

The JSON Schema steps require the optional justinrainbow/json-schema package: composer require --dev justinrainbow/json-schema.

Steps​

@Given the response JSON is loaded from the file :filename

Set the response JSON content from a fixture file

Given the response JSON is loaded from the file "json_valid.json"
@Given the response JSON is the following:

Set the response JSON content directly from a PyString

Given the response JSON is the following:
"""
{"name": "John Doe", "roles": ["admin", "editor"]}
"""
@When I print the last JSON response

Print the last JSON response

When I print the last JSON response
@Then the response should be in JSON format

Assert that a response is valid JSON

Then the response should be in JSON format

# Content set by a fixture step is validated instead of the page content.
Given the response JSON is loaded from the file "json_valid.json"
Then the response should be in JSON format
@Then the response should not be in JSON format

Assert that a response is not valid JSON

Then the response should not be in JSON format
@Then the JSON path :path should exist

Assert that a JSONPath expression matches at least 1 value

Then the JSON path "$.name" should exist
Then the JSON path "$.user.roles[0]" should exist
@Then the JSON path :path should not exist

Assert that a JSONPath expression matches no values

Then the JSON path "$.nonexistent" should not exist
@Then the JSON path :path should have the value :value

Assert that the value at a JSONPath equals the expected value

Then the JSON path "$.name" should have the value "John Doe"
Then the JSON path "$.age" should have the value "42"
@Then the JSON path :path should not have the value :value

Assert that the value at a JSONPath does not equal the expected value

Then the JSON path "$.name" should not have the value "Jane Doe"
@Then the JSON path :path should contain :value

Assert that the value at a JSONPath contains the expected text

Then the JSON path "$.name" should contain "John"
@Then the JSON path :path should not contain :value

Assert that the value at a JSONPath does not contain the expected text

Then the JSON path "$.name" should not contain "Jane"
@Then the JSON path :path should match :pattern

Assert that the value at a JSONPath matches a regular expression

Then the JSON path "$.email" should match "/^[^@]+@example\.com$/"
@Then the JSON path :path should not match :pattern

Assert that the value at a JSONPath does not match a regular expression

Then the JSON path "$.email" should not match "/^admin@/"
@Then the JSON path :path should be null

Assert that the value at a JSONPath is null

Then the JSON path "$.deleted_at" should be null
@Then the JSON path :path should be true

Assert that the value at a JSONPath is boolean true

Then the JSON path "$.active" should be true
@Then the JSON path :path should be false

Assert that the value at a JSONPath is boolean false

Then the JSON path "$.disabled" should be false
@Then the JSON path :path should have :count element(s)

Assert that the array or object at a JSONPath has a number of elements

Then the JSON path "$.items" should have "3" elements
Then the JSON path "$.user" should have "2" elements
@Then the response should match the following JSON schema:

Assert that the response validates against an inline JSON Schema

Then the response should match the following JSON schema:
"""
{"type": "object", "required": ["name"], "properties": {"name": {"type": "string"}}}
"""
@Then the response should match the JSON schema in the file :filename

Assert that the response validates against a JSON Schema from a file

Then the response should match the JSON schema in the file "json_schema.json"