admin管理员组文章数量:1316336
I am trying to write a test in Postman to check if JSON keys are present in the response I received from the server.
The response:
{
"Result": 0,
"ResponseStatus": {
"ErrorCode": null,
"Message": null,
"StackTrace": null,
"Errors": null
},
"ResponseHeader": {
"Succeeded": true,
"Errors": null
},
"SessionId": "XXX-XXX-XXX"
}
I want to check "Results, Errorcode, Message,Succeeded" etc..
Thanks!
I am trying to write a test in Postman to check if JSON keys are present in the response I received from the server.
The response:
{
"Result": 0,
"ResponseStatus": {
"ErrorCode": null,
"Message": null,
"StackTrace": null,
"Errors": null
},
"ResponseHeader": {
"Succeeded": true,
"Errors": null
},
"SessionId": "XXX-XXX-XXX"
}
I want to check "Results, Errorcode, Message,Succeeded" etc..
Thanks!
Share Improve this question edited Oct 5, 2016 at 14:15 StephenTG 2,6576 gold badges28 silver badges37 bronze badges asked Oct 5, 2016 at 14:09 abovebeyond15abovebeyond15 1051 gold badge1 silver badge10 bronze badges 4- 1 What's your question? Where are you having trouble? – WillardSolutions Commented Oct 5, 2016 at 14:12
- share your backend-api – Zahidur Rahman Commented Oct 5, 2016 at 14:12
- @ZahidRahman — Why do you need to know details of the backend API in order to answer a question about writing a test against JSON that has already been successfully retrieved? – Quentin Commented Oct 5, 2016 at 14:18
- 2 @Quentin - the "JSON value check" is checking for json values. e.x : '"Succeeded": true' - it will check if true is presented. i want to check if Succeeded is presented. i want to achieve a backend api test (so i will know when the schema has changed) – abovebeyond15 Commented Oct 6, 2016 at 7:44
2 Answers
Reset to default 4You could check the response scheme using:
var jsonData = JSON.parse(responseBody);
tests['response json contain Results'] = _.has(jsonData, 'Results');
According to your response body that you get, You can write simple test script for request under test section. You need to parse your json response first. The script will look like:
var jsonData = JSON.parse(responseBody);
tests["Succeeded with value true"] = jsonData.ResponseHeader.Succeeded === true;
similarly you can write tests for other checks.For sessionId I would suggest you to check it with sessionId where it gets generated(store it in environment and check with it in this request)
本文标签: javascriptCheck if JSON object exist (Postman)Stack Overflow
版权声明:本文标题:javascript - Check if JSON object exist (Postman) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742003964a2411582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论