Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ from the community.
| [FileDownloadTrait](STEPS.md#filedownloadtrait) | Test file download functionality with content verification. |
| [IframeTrait](STEPS.md#iframetrait) | Switch between iframes and the root document. |
| [JavascriptTrait](STEPS.md#javascripttrait) | Automatically detect JavaScript errors during test execution. |
| [JsonTrait](STEPS.md#jsontrait) | Assert JSON responses with path and schema checks. |
| [KeyboardTrait](STEPS.md#keyboardtrait) | Simulate keyboard interactions in Drupal browser testing. |
| [LinkTrait](STEPS.md#linktrait) | Verify link elements with attribute and content assertions. |
| [MetatagTrait](STEPS.md#metatagtrait) | Assert `<meta>` tags in page markup. |
Expand Down
296 changes: 296 additions & 0 deletions STEPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
| [FileDownloadTrait](#filedownloadtrait) | Test file download functionality with content verification. |
| [IframeTrait](#iframetrait) | Switch between iframes and the root document. |
| [JavascriptTrait](#javascripttrait) | Automatically detect JavaScript errors during test execution. |
| [JsonTrait](#jsontrait) | Assert JSON responses with path and schema checks. |
| [KeyboardTrait](#keyboardtrait) | Simulate keyboard interactions in Drupal browser testing. |
| [LinkTrait](#linktrait) | Verify link elements with attribute and content assertions. |
| [MetatagTrait](#metatagtrait) | Assert `<meta>` tags in page markup. |
Expand Down Expand Up @@ -1383,6 +1384,301 @@ When I switch to the root document
> ```


## JsonTrait

[Source](src/JsonTrait.php), [Example](tests/behat/features/json.feature)

> 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.
> <br/><br/>
> The JSON Schema steps require the optional `justinrainbow/json-schema`
> package: `composer require --dev justinrainbow/json-schema`.


<details>
<summary><code>@Given the response JSON from the file :filename</code></summary>

<br/>
Set the response JSON content from a fixture file
<br/><br/>

```gherkin
Given the response JSON from the file "json_valid.json"

```

</details>

<details>
<summary><code>@Given the response JSON content is the following:</code></summary>

<br/>
Set the response JSON content directly from a PyString
<br/><br/>

```gherkin
Given the response JSON content is the following:
"""
{"name": "John Doe", "roles": ["admin", "editor"]}
"""

```

</details>

<details>
<summary><code>@When I print last JSON response</code></summary>

<br/>
Print the last JSON response
<br/><br/>

```gherkin
When I print last JSON response

```

</details>

<details>
<summary><code>@Then the response should be in JSON format</code></summary>

<br/>
Assert that a response is valid JSON
<br/><br/>

```gherkin
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 from the file "json_valid.json"
Then the response should be in JSON format

```

</details>

<details>
<summary><code>@Then the response should not be in JSON format</code></summary>

<br/>
Assert that a response is not valid JSON
<br/><br/>

```gherkin
Then the response should not be in JSON format

```

</details>

<details>
<summary><code>@Then the JSON path :path should exist</code></summary>

<br/>
Assert that a JSONPath expression matches at least one value
<br/><br/>

```gherkin
Then the JSON path "$.name" should exist
Then the JSON path "$.user.roles[0]" should exist

```

</details>

<details>
<summary><code>@Then the JSON path :path should not exist</code></summary>

<br/>
Assert that a JSONPath expression matches no values
<br/><br/>

```gherkin
Then the JSON path "$.nonexistent" should not exist

```

</details>

<details>
<summary><code>@Then the JSON path :path should be equal to :value</code></summary>

<br/>
Assert that the value at a JSONPath equals the expected value
<br/><br/>

```gherkin
Then the JSON path "$.name" should be equal to "John Doe"
Then the JSON path "$.age" should be equal to "42"

```

</details>

<details>
<summary><code>@Then the JSON path :path should not be equal to :value</code></summary>

<br/>
Assert that the value at a JSONPath does not equal the expected value
<br/><br/>

```gherkin
Then the JSON path "$.name" should not be equal to "Jane Doe"

```

</details>

<details>
<summary><code>@Then the JSON path :path should contain :value</code></summary>

<br/>
Assert that the value at a JSONPath contains the expected text
<br/><br/>

```gherkin
Then the JSON path "$.name" should contain "John"

```

</details>

<details>
<summary><code>@Then the JSON path :path should not contain :value</code></summary>

<br/>
Assert that the value at a JSONPath does not contain the expected text
<br/><br/>

```gherkin
Then the JSON path "$.name" should not contain "Jane"

```

</details>

<details>
<summary><code>@Then the JSON path :path should match :pattern</code></summary>

<br/>
Assert that the value at a JSONPath matches a regular expression
<br/><br/>

```gherkin
Then the JSON path "$.email" should match "/^[^@]+@example\.com$/"

```

</details>

<details>
<summary><code>@Then the JSON path :path should not match :pattern</code></summary>

<br/>
Assert that the value at a JSONPath does not match a regular expression
<br/><br/>

```gherkin
Then the JSON path "$.email" should not match "/^admin@/"

```

</details>

<details>
<summary><code>@Then the JSON path :path should be null</code></summary>

<br/>
Assert that the value at a JSONPath is null
<br/><br/>

```gherkin
Then the JSON path "$.deleted_at" should be null

```

</details>

<details>
<summary><code>@Then the JSON path :path should be true</code></summary>

<br/>
Assert that the value at a JSONPath is boolean true
<br/><br/>

```gherkin
Then the JSON path "$.active" should be true

```

</details>

<details>
<summary><code>@Then the JSON path :path should be false</code></summary>

<br/>
Assert that the value at a JSONPath is boolean false
<br/><br/>

```gherkin
Then the JSON path "$.disabled" should be false

```

</details>

<details>
<summary><code>@Then the JSON path :path should have :count element(s)</code></summary>

<br/>
Assert that the array or object at a JSONPath has a number of elements
<br/><br/>

```gherkin
Then the JSON path "$.items" should have "3" elements
Then the JSON path "$.user" should have "2" elements

```

</details>

<details>
<summary><code>@Then the response should match the following JSON schema:</code></summary>

<br/>
Assert that the response validates against an inline JSON Schema
<br/><br/>

```gherkin
Then the response should match the following JSON schema:
"""
{"type": "object", "required": ["name"], "properties": {"name": {"type": "string"}}}
"""

```

</details>

<details>
<summary><code>@Then the response should match the JSON schema in the file :filename</code></summary>

<br/>
Assert that the response validates against a JSON Schema from a file
<br/><br/>

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

```

</details>

## KeyboardTrait

[Source](src/KeyboardTrait.php), [Example](tests/behat/features/keyboard.feature)
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"behat/behat": "^3.14",
"behat/mink": ">=1.11",
"drupal/drupal-extension": "^6.0",
"lullabot/mink-selenium2-driver": "^1.7.4"
"lullabot/mink-selenium2-driver": "^1.7.4",
"softcreatr/jsonpath": "^0.10 || ^1.0"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
},
"require-dev": {
"alexskrypnyk/phpunit-helpers": "^0.15.0",
Expand All @@ -34,6 +35,7 @@
"drupal/coder": "^8.3.28",
"dvdoug/behat-code-coverage": "^5.3.2.1",
"ergebnis/composer-normalize": "^2.47",
"justinrainbow/json-schema": "^6.0",
"mglaman/phpstan-drupal": "^2.0.0",
"php-parallel-lint/php-parallel-lint": "^1.4",
"phpcompatibility/php-compatibility": "^9.3.5",
Expand All @@ -46,6 +48,9 @@
"conflict": {
"drupal/drupal-extension": "<6"
},
"suggest": {
"justinrainbow/json-schema": "Required by JsonTrait for JSON Schema validation steps ('the response should match the JSON schema ...')."
},
"autoload": {
"psr-4": {
"DrevOps\\BehatSteps\\": "src/"
Expand Down
Loading