Thank you for providing more information on the script and process you have, it’s difficult to know what’s happening when one 1 person can see that
What are the values of that variable pm.collectionVariables.get(“testData”)? Why are parsing that?
If you’re reading those from an external data file you would need to use pm.iterationData.get("the_var_name")
@danny-dainton Thank you for that. But I need to read the data of all JSON File and loop through to validate against the response body sets as in image below. Please assist.
The code snippet that you copied from the other post is for a different context and the scenario is not the same. That was taking different parts of the datafile and check that against the corresponding parts of the response data. You’re also using the references like element.ID in your scripts which isn’t defined in your datafile because it’s id.
You also removed the code comment which tells you how to use a datafile. In Step 2, it’s using a Collection variable to mimic a datafile, unless you have created a testData variable at the Collection level which contains the same data as your JSON file, it wouldn’t work.
// Step 1 - parse response
const response = pm.response.json();
// Step 2 - define data file.
let testData = JSON.parse(pm.collectionVariables.get("testData"));
// You would change this to read from the data file.
// You can access the iteration data from the special "data" variable
// let testData = data
// Step 3 - Iterate over the test data\input file
testData.forEach(element => {
// Step 4 - Test each record in the input file
// Using string literals to create a custom test case name
// So its easier to know which record failed
pm.test(`response keys match for ID ${element.ID}`, function () {
// Step 5
// Search the response using the ID from the testData
let search = (response.find(obj => { return obj.id === element.ID }));
// Step 6
// You don't need to test the ID is correct
// You can just check that the search is not undefined
pm.expect(search).to.not.be.undefined;
// console.log(search);
// Step 7
// You can now assert on the other fields
// Example using bracket notation, just in case of special characters
pm.expect(search.address.city).to.eql(element["address-city"]);
// Example using dot notation
pm.expect(search.workCenterCode).to.eql(element.workCenterCode);
});
})
If you wanted to check the whole object in the datafile against the whole response object, the structure of the datafile and the script would need to change in many places.
Thank you for that. I need to check the whole object in the datafile against the whole response object, please share the input on how to achieve that. Thank you.
Each object will be an iteration that is run through the collection runner.
Each iteration will only have the current iterations data.
It won’t have the whole object\data (which I suspect is what the problem is here).
Therefore, if you want to read in the whole dataset, you will need to change the structure of your JSON data file, so that it has an array with a single object that contains the rest of the data. You would then parse that top level array to a variable to be compared against your response.