Hi,
I am using newman as library to run postman collections.
Looking to do a validation to compare expected json response saved in json file while the responses are recived using on(ârequestâ,(error,data) event.
below is my code where content is response from api and data is json response in file.
Iâm really following what youâre trying to do here? Whatâs the Collection doing if youâre also trying to use the Newman events to make assertions against?
Might be just me - Iâm just not really understanding the problem youâre trying to solve.
Collection is hitting all the apiâs and I am saving response in âactresponseâ variable.
Then I am comparing it with response saved in file and stored in variable âexpresponseâ.
Hey @shalaka-m I spotted some issues which are likely the cause of errors you are seeing
The following code will run asynchronously, so it is not guaranteed that the value inside expresponse would be available when you are setting the script using data.item.events.members[0].script.exec
To fix this, move that statement inside then().
And while setting global variables, you are passing strings, so newman would not be aware of what the variables content and data
...
"pm.globalVariables.set(\"actresponse\",JSON.parse(content));\r", // Where is content in the scope of the test script?
"pm.globalVariables.set(\"expresponse\",JSON.parse(data));\r", // same with data?
...
I havenât tested these, but the async file-read function may cause other problems too! The code may not return before the collectionâs âtestâ script gets executed for example.
You may want to revisit how you are structuring the file read to get expected responses. A better approach would be to read the responses and store inside a variable before executing newman.run(), but it would come at the cost of memory overhead.
Thanks @xk0der ,
I see this error using above solution âTypeError: Cannot read properties of undefined (reading âeventsâ)â .
Looks like postman script is unable to read variables that are set in node.