How to extract response body elements full branch and verify same data displays in another api (GET)

Thanks for that, did try that but its not seeming to catch what the err is

1 Like

Hi @shahbaz.shiekhqa,

So you just need to use the proper attribute for err. First, I would try console logging the whole error, that way, you can then see what attributes it has.

So now, if you want to print something else out, (say hostname), you can do:

console.log("Error is " + err.hostname);

Best,
Orest

1 Like

Thanks, i noticed when i changed that url into a text, and added it to the variable, when i print it to the console i am seeing undefined , and when i am using that same variable for my pm.sendrequest i am seeing that its not getting the url instead its just writing the variable as a url.

1 Like

Hi @shahbaz.shiekhqa,

So in order to use a postman variable, you need to first obtain a reference to the variable. You can do that by using the following syntax in your script:

var TracerswithAMPurl = pm.environment.get("TracerswithAMPurl");

Then you can use the variable as you normally would with javascript.

pm.sendRequest(TracerswithAMPrul, function (err, response) {
    if (err) {
         console.log(err);
    }
   console.log("Response is " + pm.response.text());
   pm.expect.pass("Pass test")
});
1 Like

Hi Thanks for that,

I do see the variable has turned into the url ā€¦ added
this
var TracerswithAMPurl = pm.environment.get(ā€œTracerswithAMPurlā€);

and then
pm.sendRequest(TracerswithAMPurl, function (err,response){

if (err){

    console.log(err);

    // pm.expect.fail()

}else{

console.log(response.json());

// pm.expect.pass("passed test")

}

});

but am seeing the same unexpected token issue ā€¦
Json error ā€¦
the url is a text so is that why its sending this error ā€¦? do you we need to convert that text into json ?

1 Like

Hi @shahbaz.shiekhqa,

Thanks for providing the information up front.

From the looks of it, you error is coming from response.json. This is because your response actually isnt json, its just text. The snippet above was taken from something else that has a json response.

Instead, use pm.response.text() and you shouldnt have an error anymore. Iā€™ve also updated the example above.

Thanks,
Orest

1 Like

Thank you very much for thatā€¦ instead of using the console.log , how can i show these results through the test results? i tried pm.expect.pass or pm.expect.fail but pass is not a function

1 Like

Found the answer

thank you very much for all your help,

is it fine if i ask more questions on this forum ā€¦?

pm.test("Tracers with AMP Url hits the server successfully", function () {
    pm.sendRequest(TracerswithAMPurl, function (err, response) {
        pm.expect(err).to.not.be.ok;
        pm.expect(response).to.have.property('code', 200);
        pm.expect(response).to.have.property('status', 'OK');
    });
});
2 Likes

Hi @shahbaz.shiekhqa,

Youā€™re welcome! Glad we can help and that you can figure it out.

Youā€™re script looks pretty good.

As for asking more questions, I would say if you have new unrelated questions to the original issue for this post, I would make a new post around it, for organizational purposes.

Best,
Orest

1 Like

Hi I did post another question over hereā€¦ this is a bit complex ā€¦

1 Like

Hi would you happen to know the answer to this

How to verify full body response within a Pre requisite script and compare with response body?

1 Like

Hi Allen,

I did the same test for another API, and i am getting it to pass, but not showing in the test results is there something wrong i am doing?

API 1:

API2:
const apiOne = JSON.parse(pm.collectionVariables.get(ā€˜AlertBanner_Messageā€™));

const apiTwo = pm.response.json();

for(let i = 0; i < apiOne.length; i++) {

const alertbanner = apiOne[i];    

const apiTwo = apiTwo.find(portalannouncement => portalannouncement.announcementId === alertbanner.announcementId && portalannouncement.header === alertbanner.header &&

 portalannouncement.body === alertbanner.body);

pm.test(Has matching header for ${alertbanner.header}, function (){

pm.expect(apiTwo).to.not.be.undefined;

});

Its not showing the test results of ā€œHas matching headerā€ Test
just these two are showing:
image

1 Like

Can you show us the entire test script either in a screenshot or copy/paste the source please?

1 Like

Sure,

this is the entire test script:

pm.test(ā€œif 200 - Pass else Fail - 404 errorā€, function(){

pm.response.to.have.status(200);

});

pm.test(ā€œResponse Time is less than 600msā€, function(){

pm.expect(pm.response.responseTime).to.be.below(600);

});

const apiOne = JSON.parse(pm.collectionVariables.get(ā€˜AlertBanner_Messageā€™));

const apiTwo = pm.response.json();

for(let i = 0; i < apiOne.length; i++) {

const alertbanner = apiOne[i];    

const apiTwo = apiTwo.find(portalannouncement => portalannouncement.announcementId === alertbanner.announcementId && portalannouncement.header === alertbanner.header &&

 portalannouncement.body === alertbanner.body);

pm.test(Has matching header for ${alertbanner.header}, function (){

pm.expect(apiTwo).to.not.be.undefined;

});

}

1 Like

Iā€™m going to reformat your test script for better readability.

pm.test(ā€œif 200 - Pass else Fail - 404 errorā€, function(){
  pm.response.to.have.status(200);
});

pm.test(ā€œResponse Time is less than 600msā€, function(){
  pm.expect(pm.response.responseTime).to.be.below(600);
});

const apiOne = JSON.parse(pm.collectionVariables.get(ā€˜AlertBanner_Messageā€™));
const apiTwo = pm.response.json();

for(let i = 0; i < apiOne.length; i++) {
  const alertbanner = apiOne[i];    
  const apiTwo = apiTwo.find(portalannouncement => portalannouncement.announcementId === alertbanner.announcementId && portalannouncement.header === alertbanner.header &&
 portalannouncement.body === alertbanner.body);

  pm.test(Has matching header for ${alertbanner.header}, function (){
    pm.expect(apiTwo).to.not.be.undefined;
  });
}

Since you arenā€™t seeing any matching header tests, that means that you donā€™t have any objects in your apiOne array and your for loop has nothing to iterate over.

1 Like

Hi

so for the first API i have
the header displaying as customer technical support hours for test

and the second api I have the same headerā€¦
but everytime i run the code it should see that 3 tests were run but is only seeing two tests runā€¦

1 Like

It looks like your first request returns an object, not an array.

So your for loop has nothing to iterate over, meaning it wonā€™t create that third test. you could try this:

pm.test(ā€œif 200 - Pass else Fail - 404 errorā€, function(){
  pm.response.to.have.status(200);
});

pm.test(ā€œResponse Time is less than 600msā€, function(){
  pm.expect(pm.response.responseTime).to.be.below(600);
});

const apiOne = JSON.parse(pm.collectionVariables.get(ā€˜AlertBanner_Messageā€™));
const apiTwo = pm.response.json();
    
const match = apiTwo.find(portalannouncement => portalannouncement.announcementId === apiOne.announcementId && portalannouncement.header === apiOne.header &&
 portalannouncement.body === apiOne.body);

pm.test(Has matching header for ${apiOne.header}, function (){
  pm.expect(match).to.not.be.undefined;
});

1 Like

Allen, sir you are awesome, that worked perfectly
so all we had to do was get rid of the loop ā€¦ but than how do we know if its an object or array ?

Also have another question, man so much testing to do ā€“ with APIs

2 Likes

Iā€™m very happy to hear that worked for you!

To tell if something is an object or an array, look at the enclosing characters

{} //object
[] //array
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.