如何在 Katalon studio 中遍历 API 响应中的每个 属性 元素?
How to iterate through each property element in API response in Katalon studio?
我正在katalon studio中编写测试脚本来验证API.My响应主体的响应主体格式:
{
"status": "Success",
"correlationCode": "1234-5678",
"type": {
"id": 51247,
"name": "Student",
},
"data": {
"name": "Sara Nieves",
"gender": "Female",
"dob": "1995-08-06",
"libraryCard": {
"id": "11178",
"type": "Seniors"
},
"qualifications": [
{
"id": "45650986546",
"name": "Graduate Certificate in Environmental Engineering Management"
}
]
}
}
我想验证 none 元素的 return 'null' 值。由于 API 响应的元素 return 不是静态的(意思是姓名、性别等可能不会每次都被 returned)因此,我不能使用类似 "data.name" 来验证它是否有空值。所以,我想要一种通用的方法来遍历每个属性 returned 并检查它的值是否 returned 为空。
任何帮助将不胜感激。谢谢!
此解决方案不如@dagget 建议的解决方案精确,但它是一种快速检查:
def response = '''
{
"status": "Success",
"correlationCode": "1234-5678",
"type": {
"id": 51247,
"name": "Student",
},
"data": {
"name": "Sara Nieves",
"gender": "femmina",
"dob": "1995-08-06",
"libraryCard": {
"id": "11178",
"type": "Seniors"
},
"qualifications": [
{
"id": "45650986546",
"name": "Graduate Certificate in Environmental Engineering Management"
}
]
}
}
'''
assert !response.contains("null")
您收到错误消息:
groovy.lang.MissingMethodException: No signature of method: WSVerification1569811424284$_run_closure1.doCall() is applicable for argument types: (com.kms.katalon.core.testobject.ResponseObject) values: [200 1 KB] 22572.groovy:21)
我假设你的响应对象类型:com.kms.katalon.core.testobject.ResponseObject
将响应解析为 json 并验证它的代码:
import groovy.json.JsonSlurper
/**
* the recursive method to validate that json object does not have null values
* @param obj - the parsed json object (sequence of maps and lists)
* @param path - a variable to know where the error occurred in json data.
*/
void assertNoNullValue(Object obj, String path='ROOT'){
//the main assertion
assert obj!=null : "value in json could not be null: $path"
if(obj instanceof Map){
//iterate each key-value in map and validate the value recursively
obj.each{k,v-> assertNoNullValue(v,path+".$k") }
} else if(obj instanceof List){
//iterate each value in list and validate the value recursively
obj.eachWithIndex{v,i-> assertNoNullValue(v,path+"[$i]") }
}
}
def response = ...
assert response.isJsonContentType()
def responseText = response.getResponseText()
//parse body
def data = new JsonSlurper().parseText(responseText)
assertNoNullValue(data)
我正在katalon studio中编写测试脚本来验证API.My响应主体的响应主体格式:
{
"status": "Success",
"correlationCode": "1234-5678",
"type": {
"id": 51247,
"name": "Student",
},
"data": {
"name": "Sara Nieves",
"gender": "Female",
"dob": "1995-08-06",
"libraryCard": {
"id": "11178",
"type": "Seniors"
},
"qualifications": [
{
"id": "45650986546",
"name": "Graduate Certificate in Environmental Engineering Management"
}
]
}
}
我想验证 none 元素的 return 'null' 值。由于 API 响应的元素 return 不是静态的(意思是姓名、性别等可能不会每次都被 returned)因此,我不能使用类似 "data.name" 来验证它是否有空值。所以,我想要一种通用的方法来遍历每个属性 returned 并检查它的值是否 returned 为空。
任何帮助将不胜感激。谢谢!
此解决方案不如@dagget 建议的解决方案精确,但它是一种快速检查:
def response = '''
{
"status": "Success",
"correlationCode": "1234-5678",
"type": {
"id": 51247,
"name": "Student",
},
"data": {
"name": "Sara Nieves",
"gender": "femmina",
"dob": "1995-08-06",
"libraryCard": {
"id": "11178",
"type": "Seniors"
},
"qualifications": [
{
"id": "45650986546",
"name": "Graduate Certificate in Environmental Engineering Management"
}
]
}
}
'''
assert !response.contains("null")
您收到错误消息:
groovy.lang.MissingMethodException: No signature of method: WSVerification1569811424284$_run_closure1.doCall() is applicable for argument types: (com.kms.katalon.core.testobject.ResponseObject) values: [200 1 KB] 22572.groovy:21)
我假设你的响应对象类型:com.kms.katalon.core.testobject.ResponseObject
将响应解析为 json 并验证它的代码:
import groovy.json.JsonSlurper
/**
* the recursive method to validate that json object does not have null values
* @param obj - the parsed json object (sequence of maps and lists)
* @param path - a variable to know where the error occurred in json data.
*/
void assertNoNullValue(Object obj, String path='ROOT'){
//the main assertion
assert obj!=null : "value in json could not be null: $path"
if(obj instanceof Map){
//iterate each key-value in map and validate the value recursively
obj.each{k,v-> assertNoNullValue(v,path+".$k") }
} else if(obj instanceof List){
//iterate each value in list and validate the value recursively
obj.eachWithIndex{v,i-> assertNoNullValue(v,path+"[$i]") }
}
}
def response = ...
assert response.isJsonContentType()
def responseText = response.getResponseText()
//parse body
def data = new JsonSlurper().parseText(responseText)
assertNoNullValue(data)