Hello,
I have this test case: I need to search response for ID (dynamic value) and store it in variable for reusing it in next request. To find this ID, I need to go through array objects, find specific string/value in element “source”, and then grab value of another element (“args”) of the same object.
My response looks something like this:
{
"id": "d2392e98-bac8-4385-9343-d1360427a7cd",
"status": "IngestionBatchUpdated",
"createdAt": "2023-03-27T13:46:35.2994054Z",
"updatedAt": "2023-03-27T13:46:35.2994476Z",
"auditEvents": [
{
"timestamp": "2023-03-27T13:47:46.4708557Z",
"event_type": "Information",
"source": "AuditorService/ChangeStatus",
"message": "Changing status from MetadataTransformed to MappingMetadata",
"args": [
"MetadataTransformed",
"MappingMetadata"
]
},
{
"timestamp": "2023-03-27T13:47:47.1998269Z",
"event_type": "Information",
"source": "AuditorService/ChangeStatus",
"message": "Changing status from MappingMetadata to MetadataMapped",
"args": [
"MappingMetadata",
"MetadataMapped"
]
},
{
"timestamp": "2023-03-27T13:47:48.9671202Z",
"event_type": "Information",
"source": "AuditorService/ChangeStatus",
"message": "Changing status from MetadataMapped to CreatingAsset",
"args": [
"MetadataMapped",
"CreatingAsset"
]
},
{
"timestamp": "2023-03-27T13:48:09.3269921Z",
"event_type": "Information",
"source": "BrandStoreServiceFacade/CreateAsset",
"message": "Asset '52121300' created in TBS.",
"args": [
"52121300"
]
},
{
"timestamp": "2023-03-27T13:48:09.3813078Z",
"event_type": "Information",
"source": "AuditorService/ChangeStatus",
"message": "Changing status from CreatingAsset to AssetCreated",
"args": [
"CreatingAsset",
"AssetCreated"
]
},
{
"timestamp": "2023-03-27T13:48:15.2381473Z",
"event_type": "Information",
"source": "BrandStoreServiceFacade/CheckPermissionsForAsset",
"message": "User has all needed permissions.",
"args": null
}
]
}
So I need to find:
{
"timestamp": "2023-03-27T13:48:09.3269921Z",
"event_type": "Information",
"source": "BrandStoreServiceFacade/CreateAsset", // I' looking for this string: "BrandStoreServiceFacade/CreateAsset"
"message": "Asset '52121300' created in TBS.",
"args": [
"52121300" // I need to store this in variable.
]
},
Based on other topics I tried this code (and different variations of it) but without success:
pm.test(`Audit log should contains 'asset_id' and its value cannot be 'undefined' or 'null'.`, () => {
_.each(pm.response.json(), (responseBody) => {
if (responseBody.auditEvents.source.include("BrandStoreServiceFacade/CreateAsset")) {
pm.environment.set("asset_id", responseBody.auditEvents.args)
}
})
pm.expect(pm.environment.get('asset_id')).to.not.be.oneOf([undefined, null]);
I think my problem is I provide wrong path to element (or element value) of an array. Could someone please help me with this?