BotiumScript offers various assertion types to validate chatbot responses, ensuring that they meet expected criteria. This article will cover two essential JSON asserters: JSON_PATH and JSON_PATH_COUNT. Each section will provide a detailed explanation, usage examples, and configuration options.
Generic JSONPATH
This JSON_PATH
asserter allows you to validate the presence and
value of elements within a JSON structure returned by the chatbot. It is
particularly useful for checking if specific data is included in the bot's
response.
Imagine an eCommerce chatbot where the response contains the shopping cart in session variables. The following BotiumScript asserts that the cart is available in the session, and the ordered item is in the cart:
#me
add to cart 5 bananas
#bot
JSON_PATH $.session.cart
JSON_PATH $.session.cart.item[0].name | banana
Explanation- User Action: The user sends a command to the chatbot to add 5 bananas to the cart.
- Bot Response: The chatbot should update the session with the cart details.
- First Argument: The JSONPath expression to query.
- Second Argument (Optional): If provided, the value is compared to the outcome of the JSONPath expression. If the expression results in multiple values, it is compared to all of them. If not provided, only the existence of the element is asserted.
sourceData
field of the botMsg
, not on the
botMsg
as a whole.Generic JSONPATH COUNT
This asserter will validate the number of JSONPath results. You can use number comparisons or specify a number for equality:
JSON_PATH_COUNT $.session.cart.item|2
JSON_PATH_COUNT $.session.cart.item|=2
JSON_PATH_COUNT $.session.cart.item|>2
JSON_PATH_COUNT $.session.cart.item|<=3
Extending JSONPath Asserters
The JSONPath Asserter can optionally be configured with global arguments in
botium.json
. Arguments from the convo file are handed over and
used as specified.
- argCount: Number of arguments to expect in the convo file.
- path: Predefined JSONPath expression.
- pathTemplate: Mustache template for predefined JSONPath expression (based on args).
- assertTemplate: Mustache template for assertion value (based on args).
- matchingMode (since 1.11.6): Matching mode to use for assertions (default is to use the global matching mode).
Example 1 - WATSONV1_HAS_CONTEX
{
"botium": {
"Capabilities": {
"ASSERTERS": [
{
"ref": "WATSONV1_HAS_CONTEXT",
"src": "JsonPathAsserter",
"args": {
"argCount": 1,
"pathTemplate": "$.context['{{args.0}}']",
"matchingMode": "equalsIgnoreCase"
}
}
]
}
}
}
Usage:#bot
WATSONV1_HAS_CONTEXT my-context-variable
Example 2 - WATSONV1_CONTEXT
{
"botium": {
"Capabilities": {
"ASSERTERS": [
{
"ref": "WATSONV1_CONTEXT",
"src": "JsonPathAsserter",
"args": {
"argCount": 2,
"pathTemplate": "$.context['{{args.0}}']",
"assertTemplate": "{{args.1}}"
}
}
]
}
}
}
Usage:#bot
WATSONV1_CONTEXT my-context-variable|expected-value