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

Hi , I have an issue where i have two APIs,

API1 -> https://eprodservices-dev.devjcrinc.com/portal/api/Sites/GetSites?userId={{userID}}

body shows:
[

{

    "siteId": 7911,

    "siteName": "Abrazo Scottsdale Campus",

    "hcoid": 9497,

    "defaultSite": false

},

{

    "siteId": 7771,

    "siteName": "Baptist Health System",

    "hcoid": 9209,

    "defaultSite": false

},

{

    "siteId": 7202,

    "siteName": "BBH - PBMC, LLC",

    "hcoid": 7934,

    "defaultSite": false

},

{

    "siteId": 7194,

    "siteName": "BBH - SBMC, LLC",

    "hcoid": 7924,

    "defaultSite": false

},

{

    "siteId": 7205,

    "siteName": "BBH BMC, LLC",

    "hcoid": 7938,

    "defaultSite": false

},

{

    "siteId": 7235,

    "siteName": "BBH WBMC, LLC",

    "hcoid": 7985,

    "defaultSite": false

},

{

API2:{{domain}}/GetCommonInfo/GetUserSites?userId={{userID}}

Body shows -->
[

{

    "SiteID": 685,

    "HCOID": 2659,

    "SiteName": "Saint Francis Hospital-Memphis",

    "RoleID": 1,

    "SiteFullName": "2659 Saint Francis Hospital-Memphis",

    "IsAMPAccess": 1,

    "IsTracersAccess": 1,

    "SortOrder": 1

},

{

    "SiteID": 697,

    "HCOID": 2719,

    "SiteName": "Doctors Medical Center of Modesto, Inc.",

    "RoleID": 9,

    "SiteFullName": "2719 Doctors Medical Center of Modesto, Inc.",

    "IsAMPAccess": 1,

    "IsTracersAccess": 1,

    "SortOrder": 1

},

{

    "SiteID": 760,

    "HCOID": 2915,

    "SiteName": "HDMC Holdings, L. L. C.",

    "RoleID": 1,

    "SiteFullName": "2915 HDMC Holdings, L. L. C.",

    "IsAMPAccess": 1,

    "IsTracersAccess": 1,

    "SortOrder": 1

},

{

    "SiteID": 1236,

    "HCOID": 4831,

    "SiteName": "TH Healthcare LTD",

    "RoleID": 9,

    "SiteFullName": "4831 TH Healthcare LTD",

    "IsAMPAccess": 1,

    "IsTracersAccess": 1,

    "SortOrder": 1

},

{

    "SiteID": 1255,

    "HCOID": 4860,

    "SiteName": "Twin Cities Community Hospital",

    "RoleID": 9,

    "SiteFullName": "4860 Twin Cities Community Hospital",

    "IsAMPAccess": 1,

    "IsTracersAccess": 1,

    "SortOrder": 1

},

{

    "SiteID": 1267,

    "HCOID": 4877,

    "SiteName": "Doctors Medical Center of Modesto",

    "RoleID": 9,

    "SiteFullName": "4877 Doctors Medical Center of Modesto",

    "IsAMPAccess": 1,

    "IsTracersAccess": 1,

    "SortOrder": 1

},

I see the values in the bodies and format are different except for:
HCOID, SiteID, and SiteName ā€¦

I basically want to extract the three elements from all the branches within the response body for API1 and I want to verify that API2 contains these same names and elements. (its not going to be in the same order)
I want this to be dynamic and not use hard coded valuesā€¦ since i want to add different userIDs through ā€¦ this same design for testing two site drop downs with multiple elements.

How can i go about doing this? ,

I did try this

pm.environment.set(ā€˜api1ā€™, pm.response.text());
it grabed all the data ā€¦
and

in API 2 i did this :
pm.test(ā€˜should have identical responses siteIdā€™, () =>{

pm.response.to.have.body(JSON.parse(pm.environment.get('siteId')));

});

pm.test(ā€˜should have identical responses siteNameā€™, () =>{

pm.response.to.have.body(JSON.parse(pm.environment.get('siteName')));

});

pm.test(ā€˜should have identical responses hcoidā€™, () =>{

pm.response.to.have.body(JSON.parse(pm.environment.get('hcoid')));

});

pm.test(ā€˜should have identical responses defaultSiteā€™, () =>{

pm.response.to.have.body(JSON.parse(pm.environment.get('defaultSite')));

});

the API1 - grabs everything, i dont want that,
if i have 6 branches and 6 site IDs i want it to grab that, (also 6 hcoid, 6 sitenames, 6,default sites)
and then verify that those 6 display on API2ā€¦

Please help,

Thanks

1 Like

This is going to be a two step process.

Step 1
In the tests of your first request, you should just save off the response as stringified json.

pm.collectionVariables.set('test-apiOneResponse', JSON.stringify(pm.response.json()));

NOTE - I realize JSON.stringify and casting the response as json might be redundant, but iā€™m not sure of the inner workings of the pm object, so better to be safe than sorry

Step 2
Then in the tests tab of the second request you can run the following:

const apiOneSites = JSON.parse(pm.collectionVariables.get('test-apiOneResponse'));
const apiTwoSites = pm.response.json();

for(let i = 0; i < apiOneSites.length; i++) {
    const site = apiOneSites[index];    
    const apiTwoSite = apiTwoSites.find(otherSite => otherSite.SiteID === site.siteId && otherSite.HCOID === site.hcoid && otherSite.SiteName === site.siteName);
    pm.test(`Has matching site for ${site.siteName}`, function () {
        pm.expect(apiTwoSite).to.not.be.undefined;
    }
}

I personally like collection variables more than environment variables because adding random variables into the environment can seem messy. These variables really are scoped to the collection, so they belong there.

1 Like

Hi,
Thank you so much for that code, I did try it but am getting an error at the last line. Could be due to some bracketā€¦ i did analyze could not find itā€¦

1 Like

looks like I forgot to add a closing parenthesis and semicolon

for(let i = 0; i < apiOneSites.length; i++) {
    const site = apiOneSites[index];    
    const apiTwoSite = apiTwoSites.find(otherSite => otherSite.SiteID === site.siteId && otherSite.HCOID === site.hcoid && otherSite.SiteName === site.siteName);
    pm.test(`Has matching site for ${site.siteName}`, function () {
        pm.expect(apiTwoSite).to.not.be.undefined;
    });
}
1 Like

HI Thanks,

I am getting an error saying Index is not defined:

1 Like

I really should have run this code before I posted it :slight_smile:

replace index with ā€˜iā€™, that is the variable we created in the for loop

for(let i = 0; i < apiOneSites.length; i++) {
    const site = apiOneSites[i];    
    const apiTwoSite = apiTwoSites.find(otherSite => otherSite.SiteID === site.siteId && otherSite.HCOID === site.hcoid && otherSite.SiteName === site.siteName);
    pm.test(`Has matching site for ${site.siteName}`, function () {
        pm.expect(apiTwoSite).to.not.be.undefined;
    });
}

If youā€™re going to use Postman as a part of your daily work, I would highly recommend learning javascript at least at a basic level. It will help tremendously.

1 Like

Thank you so much this really helped, just wondering how do i print to the console the tests that have passed and failed. Yeah i agree just started with Postman I know Java and dart i should really get on Javascript .

1 Like

If you click on the Test Results tab of the response window, you can see which tests passed/failed/were skipped

image

1 Like

To add to @allenheltondevā€™s response regarding the Test Results tab, you can also try running Newman in your Terminal, if youā€™d like to know which Tests are passing/failing as well.

You can find some info here:
https://learning.postman.com/docs/running-collections/using-newman-cli/command-line-integration-with-newman/#example-collection-with-failing-tests

Hope this helps.

2 Likes

Thank you very much ā€¦
I am running into a dilemma when it comes to dynamic ness and executing test cases
Turns out the old api is not used anymore

What is the best practice for a userid that has 6 sites and I want to ensure the body response that is getting returned is In fact coming from the database ā€¦ how can I validate this without hard coding values ? Meaning I want to put many userids through but if I test for all the sites of that one user that would be hard coding values ā€¦ guess want to see the best testing approach for validating that the sql database data is the correct data that is in the apiā€¦?

1 Like

If youā€™re doing data tests through Postman, you have to use an API.

From what I can understand of your use case you could do the following:

  1. Get the user and save an array of all the sites. You would do this via javascript in the test tab
  2. Iterate through the site list, hitting an API to make sure the site exists. This would involve a pre-request script and test script to do the management and tracking of the values in your site array.

Do you have an API that can validate the sites?

1 Like

Hi Thanks again, yeah i do have one API that validates all the sites, thats the one within the example, i guess one question is ā€¦ how can i validate my sites API - with the database data ā€¦ thats what my manager wants most is to ensure the API that we are testing is returning back the correct data from the database ā€¦ is there a way to add a javascript snippet to validate thatā€¦ that would be the best dynamic test and will have expected and actuals to compare to.

1 Like

The way I would run a test like this would be as follows:

  1. Create a POST request to add data to your database
  2. Create a GET request to load the data and verify it matches the data from your POST

Iā€™m not sure I really follow what youā€™re trying to solve.

You have a known set of ids and you want to make sure values in the GET match them?

1 Like

Actually the data is already there in the database i just want to validate that the response body data from the API displays the same data that is currently in the SQL database. Is there a way Postman can connect to the SQL database to verify that the API data matches the database data ?

1 Like

No, you cannot do that through Postman.

Postman is an API testing tool, not a database testing tool.

1 Like

Thank you so much Allen for your help, to get more of your assistance should i ask another question here, or should i create a new question?

I have a response with just a URL and i want to ensure i am able to verify that url is hitting the server if its clicked on , i was trying to extract it and store it in a variable but i keep getting an issue

1 Like

Hi @shahbaz.shiekhqa,

Looks like you are only getting a url in your response from the server (as you have noted).

Given that, your script will not work, because do done have any json to serialize. What you want to do is the following:

pm.environment.set("url", pm.response.text());

If you want more code snippet examples, refer to the links below.

https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/
https://learning.postman.com/docs/writing-scripts/script-references/test-examples/

Hope this helps!

Orest

1 Like

Thank you very much that worked! Now how would i ensure this url is hitting the server? would there be test snippet or would it be another request ?

1 Like

Hi @shahbaz.shiekhqa,

As for ensuring the url is hitting the server, receiving a response will not be enough to invoke an action to hit hit that url. Now if you want, you can do a pm.sendRequest() using that url, but with that, I am not sure what you are trying to accomplish.

That url wonā€™t have a request automatically made to it, unless you do some scripting. If you are trying to simulate a user clicking on the url, that is something you will have to do outside of postman, and use UI automation libraries, like selenium.

But if you just want to see that if a request made against that url goes and hits your server, then I would recommend the pm.sendRequest as noted above. Here is an example snippet I pulled from one of the links I sent earlier.

pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});

Hope this helps!

Orest

1 Like

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

1 Like