1

From below Response, I want to fetch the value of "responseCode" and store temporarily. If a value is 1 then on Console, I want to write "Test PASS". Can anyone share code for this test?

{
   "data":{
      "transactionId":"$1"
   },
   "responseMessage":"Transaction successfully done. Transaction Id : txn_15594028419901124218",
   "responseCode":1
}

I tried to use the following code to set the variable:

var jsonData = JSON.parse(responseBody); 
pm.globals.set("responseCode",jsonData.data.responseCode); 
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80

1 Answers1

0

This basic test would check that value in the response, store the variable and also write Test PASS to the console

pm.test("Check the Response Code is 1", () => {
    pm.expect(pm.response.json().responseCode).to.eql(1);
    pm.globals.set("responseCode", pm.response.json().responseCode)
    console.log("Test PASS")
});

This doesn't account for the test failing and writing Test FAIL to the console, you kind of get that anyway in the Postman UI.

If you didn't want to wrap this in a test, you could just do something like:

if(pm.response.json().responseCode === 1){
    pm.globals.set("responseCode", pm.response.json().responseCode)
    console.log("Test PASS")
}
else {
    console.log("Test FAIL")
}
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80