I am trying to to loop through a set of ids to change their title. {{id}}
and {{title}}
.
My request looks like this {{app}}/data/{{id}}
The body looks like this { "title": "{{title}}" }
Here is my current test code that doesn't work but more or less what I'm working towards.
var ids = [1, 2, 3]
var titles = ["title1", "title2", "title3"]
var i = titles.length
for(var j = 0; j < i; j++){
pm.environment.set("id",ids[j])
pm.environment.set("title",titles[j])
postman.setNextRequest("<request_name>")
}
My issue is that the title is only being updated the very last one title3 and ignoring the rest. Does anyone know how to fix this issue?
-
Did you run the request by runner or just sending the request?lucas-nguyen-17– lucas-nguyen-172021年09月01日 23:46:04 +00:00Commented Sep 1, 2021 at 23:46
-
@lucasnguyen17 I tried both, neither workedSteve– Steve2021年09月02日 14:27:56 +00:00Commented Sep 2, 2021 at 14:27
3 Answers 3
First, postman.setNextRequest
only works in runner.
Second, your task will be achieved using this code:
In tab Test of Request 1:
//Save 2 arrays
let ids = [1, 2, 3]
let titles = ["title1", "title2", "title3"]
pm.environment.set("ids", JSON.stringify(ids));
pm.environment.set("titles", JSON.stringify(titles));
In tab Pre-request of Request 2:
let ids = JSON.parse(pm.environment.get("ids"));
let titles = JSON.parse(pm.environment.get("titles"));
pm.environment.set("id", ids.shift())
pm.environment.set("title", titles.shift())
pm.environment.set("ids", JSON.stringify(ids));
pm.environment.set("titles", JSON.stringify(titles));
In tab Test of Request 2:
let ids = JSON.parse(pm.environment.get("ids"));
if (ids.length > 0){
postman.setNextRequest("New Request 69020616");
}
The result
Comments
Based off of @lucas-nguyen-17 answer I did the same thing but made an array of tests and only used one request by using pm.variables
instead of pm.environment
so the variables are only local to the test and not the whole environment.
Pre-request Script
let allTests = [
{ name: '1', statusCode: 200, data: {id: 1, title: 'title1'} },
{ name: '2', statusCode: 200, data: {id: 2, title: 'title2'} },
{ name: '3', statusCode: 200, data: {id: 3, title: 'title3'} },
{ name: "null id", statusCode: 400, data: {id: null} },
{ name: "happy", statusCode: 200, data: {} },
];
// Set test data on only first iteration in runner
if (pm.variables.get('testData') === undefined {
pm.variables.set('testData', JSON.stringify(allTests));
}
// Get the current test and remove it from the test still needing to run
let testData = JSON.parse(pm.variables.get('testData'));
let currentTestData = testData.shift()
pm.variables.set('testData', JSON.stringify(testData))
pm.variables.set('currentTestData', JSON.stringify(currentTestData))
// Combine current test data with a happy object so all parts aren't needed on every test
let happy = {id: '0', title: 'title'};
let test = {
...happy,
//overwrite happy with variables in test data
...currentTestData.data
};
// Set the variables for the test
pm.variables.set('id', test.id);
pm.variables.set('title', test.title);
Tests
let test = JSON.parse(pm.variables.get('currentTestData'));
pm.test(`Test(${test.name}) Status code is ${test.statusCode}`, function () {
pm.response.to.have.status(test.statusCode);
});
// Run the next test if any are left
let testData = JSON.parse(pm.variables.get('testData'));
if (testData.length > 0){
postman.setNextRequest(pm.info.requestName);
}
When not using the runner this will always just run the first test case but using the runner runs them all.
Comments
Create a json file like below:
[
{"ids": "1","title":"title1"},
{"ids": "2","title":"title2"},
{"ids": "3","title":"title2"}
]
Then run your PUT query from collection, click run, and select file (your json) file and then run new collection copy.
1 Comment
Explore related questions
See similar questions with these tags.