4

I need to print one specific response element from the response body of an API in the test results section of Postman.

1
  • There was an error in evaluating the test script: TypeError: Cannot read properties of undefined (reading '0')
    – user54162
    Sep 15, 2022 at 9:49

2 Answers 2

6

Assuming that you want to print a data from the response value

var jsonData = pm.response.json();

var testmessage=jsonData._data[0].UserName //Dev_demo_82
pm.test(testmessage, function () {
   //testscript

});

Response value of Property UserName will be stored in variable testmessage now your test name will be displayed as shown in image below

enter image description here

4

Mohamed's step will allow you to do what you wanted, but if you are trying to debug something then postman already have the postman console for this purpose.

so just use console.log() in the prerequisite or test script session and open view>show postman console in the postman menu bar.

Now run the request and you can see that the value is printed in the console.

Opening:

enter image description here

Viewing:

enter image description here

Note: if you click on the request it will show all details like the request header, body, response etc

in newman htmlextra report you can get these details in the report using the --reporter-htmlextra-logs flag

6
  • 1
    Just for an added bonus, in htmlextra, those log statements are now with each request rather than in a separate section. 😁 May 3, 2020 at 14:04
  • 1
    @DannyDainton you are a life saver Danny . That's an awesome change thank you for that . Hope you will come up with consolidating different Newman reports together as in mocha report
    – PDHide
    May 3, 2020 at 14:06
  • 1
    Kinda out of my control as Newman is a one collection at a time type thing...not heard any internal chats for changing that at the moment. 😔 Keep up the great effort in here answering all the questions 🏆 May 3, 2020 at 14:10
  • 1
    @DannyDainton Thanks for the awesome report and will look forward for more aawesome features.
    – PDHide
    May 3, 2020 at 14:15
  • 1
    If we're not connected outside of here - let's make that happen. Would love to hear more of your thoughts. Apologies for hijacking the thread. May 3, 2020 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.