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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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