Setting Checkpoints for an API Script
Suggestions
You are advised to set checkpoints. For API requests, you need to provide checkpoints for determining response codes.
If the checkpoints are left empty, the result is success regardless of the response code of the API.
Checkpoint Description
A test checkpoint is also called assertion. It determines whether the system meets the expectation based on the API response.
On the Checkpoints tab page of the test step in the test case details of API automation, you can define test checkpoints.
Checkpoint parameters include Property, Comparison Operator, and Target Value.
Parameter |
Description |
---|---|
Retries |
Number of times that the test step is re-executed if the checkpoint fails. The value is an integer ranging from 0 to 5. |
Retry Interval |
Interval for retrying a checkpoint if the checkpoint fails. The unit is ms. The value is an integer ranging from 0 to 10,000. |
Source |
Source of the detected field, such as the response body (JSON), response header, response code, and variable. |
Property |
Enter $ to invoke global variables, local variables, and built-in functions.
|
Advanced Extraction Type |
(Optional) Use the advanced extraction type to assist in extracting checkpoint information. If N/A is selected, no additional matching mode is used. Currently, there are two modes:
For the advanced extraction type, the string extraction function is preferred. If the function cannot meet the requirements, you can use the regular expression. |
Type Value |
Parameter required in the advanced extraction type. |
Comparison Operator |
Comparison operator, such as numbers, strings, JSON objects, and types, are supported. For details, see Comparison Operator Description. |
Target Value |
Expected value of a checkpoint. Built-in parameters can be used for target values. For details, see Built-in Parameters. |
Parameter |
Value |
---|---|
Source |
Response body (JSON) |
Property |
item.name |
Advanced Extraction Type |
Character string extraction |
Type Value |
0–5 |
Comparison Operator |
Equals (string) |
Target Value |
petty |
Character String Extraction Description
When setting checkpoints or response extraction, if the expressions in the property setting bar cannot meet the requirements, you can set Advanced Extraction Type to Character string extraction.
There are two Type Value text boxes for string truncation:
- The first box indicates the start index. The first digit is 0. The start index is contained in the truncated string.
- The second box indicates the end index. The end index is not contained in the truncated string.
The following response body is used as an example.
- To extract the fifth to ninth digits of code in the first element of the result array from the response body and compare them with the target value, configure the Checkpoints tab page as follows.
- To extract the fifth to ninth digits of code in the first element of the result array in the response body and assign the value to the variable code, configure the Extract Response tab page as follows.
Regular Expression Description
When setting checkpoints or response extraction, if the expressions in the property setting bar or the character string extraction function cannot meet the requirements, you can set Advanced Extraction Type to Regular expression.
The Java regular expression engine can be used.
The following response body is used as an example.
- If the checkpoint verifies remoteUrl of the first element in the result array in the response body, obtain the IP value through regular expression matching and compare the value with the target value. The configuration is as follows. Set Type Value to ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?).
- If remoteUrl of the first element in the result array in the response body is extracted, obtain the IP value through regular expression matching and assign the value to the variable remoteIp. The configuration is as follows. Set Type Value to ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?).
Comparison Operator Description
CodeArts TestPlan supports the following comparison types.
Comparison Type |
Comparison Operator |
Value Mandatory or Not |
Example |
---|---|---|---|
Digit |
|
Yes |
|
String |
|
Yes |
|
Regular expression |
Regular expression |
Yes |
|
General |
|
No |
|
JSON array |
|
No |
|
JSON array size |
Yes |
|
|
Type |
|
No |
|
JSON object |
JSON value |
Yes |
|
Response Code Check
Compare the response code with the target value. For example, check whether the response code is 200.
Response Header Check
Compare the value of a field in the response header with the target value. For example, check whether the value of content-type in the response header is equal to text/plain;charset=UTF-8.
Response Body (JSON) Check
- Check the value of the object field in the response body (JSON). For example:
The response body structure is as follows.
Check the value of the field whose name is status in the response body object. The checkpoint configuration is as follows.
- Check the field value of an array object in the response body (JSON). (The array condition uses the subscript starting from 0 to determine the object.) For example:
The response body structure is as follows.
Check the value of userId in the first element object field of the result array in the response body. The checkpoint configuration is as follows.
- Check the field value of an array object in the response body (JSON). (The array condition uses the fuzzy match function to determine the object.) For example:
The response body structure is as follows.
- Check all objects whose name is beer in the result array of the response body. Obtain the values of id after the first object. The checkpoint configuration is as follows.
If there is only one object in the obtained array, [0] can be omitted. The expression in the example can be written as result[name==beer].id.
- Check the ID of the object whose name in the result array of the response body is beer, and of which the a property under obj is 2. The checkpoint configuration is as follows.
- Check all objects whose name is beer in the result array of the response body. Obtain the values of id after the first object. The checkpoint configuration is as follows.
Variables Check
Check global variables, local variables, and variable values after response extraction, and compare them with target values. For example:
- Check whether the value of the global variable hostIp is 127.0.0.1.
- Check whether the value of the local variable local is text.
- Check whether the value of the variable name after response extraction is Liquor.
Example: Obtaining a String from the Response Body Based on the Specified key:value
You can enter an array whose subscript is in the key:value format for the Property field of the check body to obtain the JSON string that meets the condition from the response body based on the specified key:value.
The following connectors are supported between key:value.
Connector |
Description |
Example |
---|---|---|
== |
Equals (string or digit) |
name==John age==20 |
!= |
Does not equal (string or digit) |
name!=John age!=20 |
> |
Greater than (digit) |
age>20 |
>= |
Greater than or equal to (digit) |
age>=20 |
< |
Less than (digit) |
age<20 |
<= |
Less than or equal to (digit) |
age<=20 |
The array subscript supports multiple groups of key:value conditions connected by &&, indicating that the extracted JSON string must meet all key:value conditions. For example, student[age>20&gender=female] indicates that the extraction conditions are that the age must be greater than 20 and the gender must be female.
The search criteria of an array support nested arrays. In this case, the sub-condition can be met only when all objects in the nested condition meet the conditions.
- If the value is an empty object, use $null, that is, key==$null.
- If the value is a null string ("null"), use null, that is, key==null.
- If the value is an empty string (""), use key==.
- If the property value of a checkpoint contains special characters, use single quotation marks or double quotation marks as boundary characters.
The following response body (JSON) is used as an example:
{ "status": "success", "result": [ { "name": "Beer", "quantity": "20" "address": "Shelf 10 in repo A3", "obj": { "a": 1, "b": "test", "c": "test" }, "array": [ { "id": 1, "name": "aaa" }, { "id": 2, "name": "bbb" } ] }, { "name": "Beer", "quantity": "10", "address": "Shelf 10 in repo A3", "obj": { "a": 1, "b": "test", "c": "test" }, "array": [ { "id": 1, "name": "aaa" }, { "id": 2, "name": "bbb" } ] }, { "name": "Liquor", "quantity": "20" "address": "Shelf 10 in repo A3", "obj": { "a": 1, "b": "test", "c": "test" }, "array": [ { "id": 1, "name": "aaa" }, { "id": 2, "name": "bbb" } ] }, { "name": "Liquor", "quantity": "30" "address": "Shelf 10 in repo A3", "obj": { "a": 1, "b": "test", "c": "test" }, "array": [ { "id": 3, "name": "aaa" }, { "id": 4, "name": "bbb" } ] }, { "name": null, "quantity": "10", "address": "Shelf 10 in repo A3", "obj": { "a": 2, "b": "test", "c": "test" }, "array": [ { "id": 5, "name": "aaa" }, { "id": 6, "name": "bbb" } ] }, { "name": "", "quantity": "10", "address": "Shelf 10 in repo A3", "obj": { "a": 2, "b": "test", "c": "test" }, "array": [ { "id": 5, "name": "aaa" }, { "id": 6, "name": "bbb" } ] } ], "condition": [ { "name": "Beer", "quantity": "120", "address": "Shelf 15 in warehouse A1", } ], "reason": null }
The following table lists the expressions for obtaining data from the response body.
Extraction Condition |
Expression |
---|---|
Obtain the data whose name is Liquor from the result array. |
result[name==Liquor] |
Obtain the data whose name is not Beer and quantity is greater than 20 from the result array. |
result[name!=Beer&&quantity>20] |
Obtain the data whose name is a null object and id is less than or equal to 2 from the result array. |
result[name==$null&&array[id<=2]] |
Obtain the data whose name is a null string and property a under obj is 2 from the result array. |
result[name==null&&obj.a==2] |
Obtain the data whose name is an empty string ("") from the result array. |
result[name==] |
If you need to check whether the data (JSON) whose name is Liquor and quantity is greater than 20 is [{"name": "Liquor", "quantity": "30", " address": "Shelf 10 in warehouse A3", "obj": {"a": 1, "b": "test", "c": " test"}, "array": [{"id": 3, "name": "aaa"}, {"id": 4, " name": "bbb"}]], refer to the following table for the configuration of each field.
Parameter |
Value |
---|---|
Source |
Response body (JSON) |
Property |
result[name!=Beer&&quantity>20] |
Comparison Operator |
Equals (string) |
Target Value |
[{"name": "Liquor", "quantity": "30"," address": "Shelf 10 in warehouse A3", "obj": {"a": 1, "b": "test", "c": " test"}, "array": [{"id": 3, "name": "aaa"},{"id": 4," name": "bbb"}]] |
Example: Obtaining Data from the Response Body Based on JSONPath
For details about JSONPath, visit the official website.
The following response body (JSON) is used as an example:
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 }
The JSONPath expression can contain dots (.) or brackets ([]). For example:
- $.store.book[0].title
- $['store']['book'][0]['title']
The following table describes the JSONPath expressions and their meanings.
JSONPath Expression |
Description |
---|---|
$.store.book[*].author |
The authors of all books. |
$..author |
All authors. |
$.store.* |
All things, both books and bicycles. |
$.store..price |
The price of everything. |
$..book[2] |
The third book. |
$..book[-2] |
The second to last book. |
$..book[0,1] |
The first two books. |
$..book[:2] |
All books from index 0 (inclusive) until index 2 (exclusive). |
$..book[1:2] |
All books from index 1 (inclusive) until index 2 (exclusive). |
$..book[-2:] |
Last two books. |
$..book[2:] |
Book number two from tail. |
$..book[?(@.isbn)] |
All books with an ISBN number. |
$.store.book[?(@.price < 10)] |
All books in store cheaper than 10. |
$..book[?(@.price <= $['expensive'])] |
All books in store that are not "expensive". |
$..book[?(@.author =~ /.*REES/i)] |
All books matching regex (ignore case). |
$..* |
Give me everything. |
If the length or size function is used, do not use deep scanning (that is, the .. symbol) when calling the function for multiple times. For example, if $..book.length() returns the number of books, you need to determine the path and change it to $.store.book.length().
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot