Need help with writing the following test script.
Verifying the count returned in the response body is equal to 33.
var jsonData = JSON.parse(responseBody);
var expectedCount = 33;
tests["Response count is: " + expectedCount] = jsonData.length === expectedCount;
The above works, but I want to set expectedCount
as a collection variable or environmental variable instead of providing the value in the script. How to do that?
Hey @balareddiar,
Welcome to the community! 
You can set a Collection level variable like this:
pm.collectionVariables.set("var_name", "var_value")
You could use that in a test script like this:
const jsonData = pm.response.json();
pm.collectionVariables.set("expectedCount", 33)
pm.test("Check the expected length", () => {
pm.expect(jsonData.length).to.equal(pm.collectionVariables.get("expectedCount"));
});
You’ll be able to see what Collection Variables have been set by selecting the ...
icon next to the Collection name in the sidebar, then select Edit
. This presents you with a popup where you will be able to switch to the Variables
tab to see the value set.
I’m not sure about your actual use-case so I can only offer a basic solution.
Thanks, but that did not work for my case. I am trying to count the number of records (say user records) that are shown in the response body after I perform the GET operation. I know the number of users records returned will be certain value, but don’t want to set it in the script, since the value keeps changing and user records number is used in other test scripts.
Like for example, my response body looks like below: 3 records have returned. My web shows 3 users too. So how to verify?
{
“id”: 3,
“username”: “mmm”,
“user_number”: 3,
}
{
“id”: 4,
“username”: “nnnn”,
“user_number”: 5,
}
{
“id”: 3,
“username”: “tttt”,
“user_number”: 6,
}
I was thinking of setting the number of user records value in Collection variable under Collection: Edit->Variable tab and then getting the value in the script. Is that possible?
I did mention that I have zero context about what you’re doing
- Your reply has given more information about what you’re trying to do that your original question. 
When asking for help or a solution, it’s vital to give the other people, enough detail in order for them to help or you just spend time going back and forth till you get the the actual usecase.
It’s still a little unclear what you trying to do, you mentioned a certain number of records returned and then comparing that to a web view or something like that? What does that look like?
For getting the Collection level variables - You can manually set that and then get the value using pm.collectionVariables.get("variable_name")
in the sandbox environment.