Hi,
I am getting response which contain large number of records.
I want to make sure doesn’t contain duplicate sessionIds. At the end of each 100 records i am getting next token and want to execute with that for next set of records.
May I know should should I write code for this ?
Can you post the initial part of the response, including the first two records?
Can you please post it using the preformatted text option in the editor (so it doesn’t all get aligned to the left). Redacted as appropriate. This is so that we can mock the response, and provide more accurate code.
The way I would do this is use the JavaScript map function to map all of the sessionID’s.
This will create an array of all of the session ID’s.
Then you can do something similar to the following.
let array1 = ['apple','orange','banana']
let array2 = ['apple','orange','apple']
const hasDuplicates = (arr) => arr.length !== new Set(arr).size;
pm.test("Array 1 has no duplicates", function () {
pm.expect(hasDuplicates(array1)).to.be.false;
});
pm.test("Array 2 has no duplicates", function () {
pm.expect(hasDuplicates(array2)).to.be.false;
});
For the second part of your question. You need to target the “next” token and then use an IF statement that uses setNextRequest() to keep running the same request while the next token is not null. Please note, setNextRequest() only works with the Collection Runner.
For example…
if (response.next != null) {
postman.setNextRequest("requestName");
}
Hi Mike,
Thank you for your reply.
Here is the initial part of the response
{
"sessions": [
{
"sessionId": "",
"sessionTitle": "",
"sessionDescription": "",
"facility": "",
"branchId": "",
"startInstant": "",
"durationSeconds": ,
"isWaitListAvailable": false,
"bookedAccountMembers": [],
"waitListedAccountMembers": [],
"primaryInstructor": {
"instructorId": "",
"instructorDisplayName": "",
"branchId": ""
},
},
This is what I have created after referring your sample.
const jsonData = pm.response.json();
const sId = _.map(jsonData.sessions.sessionId);
function hasDuplicates(array){
return array.length !== new Set(array).size;
}
pm.test("session search has no duplicate sessionIds",function() {
pm.expect(hasDuplicates(sId)).to.be.false;
});
You don’t quite have the format for the map correct so the array will always be blank.
The following uses the core JavaScript map function.
let sId = jsonData.sessions.map(sessions => sessions.sessionId);
And the following is using Lodash which you have in your sample code.
let sId = _.map(jsonData.sessions, 'sessionId');
You should always make your test fail to prove that you are not getting a false positive.
In this situation, you could potentially add an extra assertion to check that the array exists and is not empty.
pm.test("session search has no duplicate sessionIds",function() {
pm.expect(sId).to.be.an("array").that.is.not.empty;
pm.expect(hasDuplicates(sId)).to.be.false;
});
Please remember that console log is your friend. If you would have console logged the sId array, you would potentially have noticed that it was empty.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.