I have the following XML output that has two packages in the items block. I want to write a test to check that the response item block has package count 2.
Wrote below test using length function, it works fine when executing the request as an individual. But if I run this test in the postman runner or newman it gets failed saying length is undefined.
Response :
<PackageCollection xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://api.blackbay.com/v2">
<TotalCount>2</TotalCount>
<PreviousPage i:nil="true" />
<NextPage i:nil="true" />
<Items>
<Package>
<Id>18665bd4-2ed9-4261-acd0-fb36746943ba</Id>
<Barcode>2611508 5016400487671202</Barcode>
<SpecialInstructions i:nil="true" />
<Size i:nil="true" />
<Rules />
</Package>
<Package>
<Id>137bd10e-ee15-4d40-8267-fe098b6a9db7</Id>
<Barcode>2611508 5016400487671201</Barcode>
<SpecialInstructions i:nil="true" />
<Size i:nil="true" />
<Rules />
</Package>
</Items>
</PackageCollection>
Test script:
pm.test("Check response code", function () {
if (pm.response.to.have.status(200)) {
pm.test("get Package count", function () {
const responseJson = xml2Json(pm.response.text());
const length = responseJson.PackageCollection.Items.Package.length;
console.log(length);
pm.expect(length).to.eql(2);
});
}
});
This is responseJson :
Appreciate your help.
Thank you.