最简单的方法是将响应处理为 JSON,并简单地检查您的层次结构:
在测试部分,你可以添加一些测试:
pm.test("Response should be OK", function () {
pm.response.to.be.ok;
});
// You can actually check for explicit response code if a 200 series is not enough.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Optionally test the returned Content-Type is correct.
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
pm.test("Response should be okay to process", function () {
pm.response.to.be.ok;
pm.response.to.not.be.error;
});
pm.test("Body contains JSON data result with Twilio as the viewable name", function () {
const JsonData = pm.response.json();
pm.expect(JsonData.data.twilio.viewable.name).to.equal('Twilio');
});
为了安全起见,您可以先检查每个父元素是否存在(验证数据在 JsonData 中),以避免在负载发生变化或与您期望的不匹配时出现错误。
编辑:您的 JSON 结构:
{
"success": true,
"timestamp": "2019-08-12T12:31:33+00:00",
"response_code": 200,
"data": {
"twilio": {
"name": "Twilio",
"slug": "twilio",
"image": "https://s3-eu-west-1.amazonaws.com/connector-assets/images/twilio.png",
"description": "Twilio is a cloud communications platform as a service. Integrate your Twilio account to send outgoing SMS from Purple.",
"category": "communication",
"connectedCount": 1,
"allowMultipleAdd": false,
"editable": [],
"viewable": {
"cf-5d51503868af3": {
"name": "Twilio"
}
},
"id": "cf-5d515c2527579",
"overrideAllowed": false
},
"salesforce-mc": {
"name": "Salesforce marketing cloud",
"slug": "salesforce-mc",
"image": "https://s3-eu-west-1.amazonaws.com/connector-assets/images/sf-mc.png",
"description": "Salesforce Marketing Cloud is a provider of digital marketing automation and analytics software and services.",
"category": "marketing-automation",
"connectedCount": 0,
"allowMultipleAdd": true,
"editable": [],
"viewable": [],
"id": "cf-5d515c252bc31",
"overrideAllowed": true
}
}
}
基于此,您正在查找的文本是存在的,但在 ID 下,不能如您所指示的那样直接可用。可以进行两种检查:
1.使用主块中的名称
pm.test("Body contains JSON data result with Twilio as the viewable name", function () {
const JsonData = pm.response.json();
pm.expect(JsonData.data.twilio.name).to.equal('Twilio');
});
- 另一种方法是提取可视值 ('cf-5d51503868af3') 并使用该值作为键来进行测试。不确定这是否可以从您的用户 ID 或其他数据源获得。如果没有,那么您需要从
JsonData.data.twilio.viwable 对象中提取它。还有一些更有用的例子here in this answer.