All the requests in a postman collection do not run - postman

I am new to postman. I have 6 requests collection and I am using variables that pass from these requests.
In request 3 I am using the If else statement and postman.setNextRequest to ensure that the request goes in loop until I get the required parameters, once I get the parameters I should run the next request which is mentioned in the else statement.
The request in the else statement does not seem to run when I run the collection. In the collection runner I do not see any error either. After request 3 runs, request 4, 5 and 6 should run.
When I run requests 1, 2, 3, 4, 5, 6 individually, all of them work as expected. When I run them as a collection It executes until request 3 which is in loop and the request 4, 5, 6 do not run.
Please help me understand how I can fix this issue. Please check the code below -
Request 3: i.e "Get Dataflow Execution Time 2"
bodyData = JSON.parse(responseBody);
if (responseCode.code === 200 && bodyData.dataflowJobs[0].status !== "Success" && bodyData.dataflowJobs[0].label === "MyOpps_Data_Dataflow") {
// Request 3 which is in loop until I get the status === "success"
postman.setNextRequest("Get Dataflow Execution Time 2");
} else {
postman.setNextRequest("Schedule Recipe"); // Request 4
var current_timestamp = new Date();
console.log(current_timestamp.toISOString())
}

Related

Postman tests send multiple requests

I want to make a postman request that returns a list of IDs.
The returned list can contain 1 or multiple IDs.
In tests section i want to use these returned IDs to make new requests.
Is it possible to send requests from tests section in for loop and check their returned data BEFORE sending another request?
I tried using simple for loop and pm.sendRequest but due to async this did not work.
Is this possible using Postman?
You can achieve that by using postman.setNextRequest("request_name")
For example:
Request 1: Tab Tests Get the ids and save ids to environment
pm.environment.set("ids", JSON.stringify(pm.response.json().ids));
Request 2:
Tab Pre-req
let ids = JSON.parse(pm.environment.get("ids"));
if(ids.length > 0){
let id = ids.shift();
pm.environment.set("ids", JSON.stringify(ids));
pm.variables.set("id", id);
}
Tab Tests
let ids = JSON.parse(pm.environment.get("ids"));
if(ids.length > 0){
postman.setNextRequest("Req2");
}
Result:

Postman - Newman running from NodeJS

In order to save the response from postman API calls, I am executing Postman collection using newman run.
The collection runs when running using
newman run C:\TasteBud\NewMan\JSON-Order_collection.json --env-var unique_order_id=OD-06-07-I2Q5-JYRY5ARPN --environment C:\TasteBud\NewMan\EnvironmentJSON.json
However when I run the same collection as part of javascript or nodejs script.
node writeToFile.js
it throws error as node "1⠄ JSONError in test-script " refer attached image. I need to pass the auth token generated by login request to subsequent request. So I have variable assignment in the "test".
let response = pm.response.json();
pm.environment.set("auth_token", response.data.auth_token);
console.log(pm.response.json().data.auth_token);
Why cant I have "test" ? if no then how can I pass/set these environment variable for subsequent API call ?
Code inside writeToFile.js is here. writeToFile.js
Always use status code validation in your token generation. because every request is based on the token, if we receive negative status code, terminate the build.
postman.setNextRequest(null);
var jsonData = JSON.parse(responseBody);
if(responseCode.code === 200){
tests["Status code is 200"] = responseCode.code === 200;
postman.setEnvironmentVariable("AT", jsonData.oauth2.accessToken);
console.log("AccessToken : " +jsonData.oauth2.accessToken);
}
else{
postman.setNextRequest(null);
console.log("Error generating user token");
console.log("Status code : "+responseCode.code);
console.log("Status description : "+jsonData.statusDescr);
}

How get response from setnextrequest:

Folder
Request 1
Request 2
How can I get response from request 1, that I call in Request 2 -> prerequest script with this function postman.setNextRequest("Request 1")? I need info from response Request 1 use as varible in Request 2.
Thank you for you advice!
Not sure you can reuse the whole response.
But you can access the response in the Tests of request 1 by pm.response, get the data you need and store them eg in an environment variable pm.environment.set("myVariable", "some value" and than access that variable after the request 1 has been executed pm.environment.get("myVariable")

Flask can handle several background tasks without using Celery

As I understand, using Flask without Celery will block the server availability when a user starts a long operation.
My web server is actually not exposed to the internet and the maximum amount of users that will be connected at a time will be 3 (its of internal use for invoking automation scripts).
I have created a test environment in order to check how Flask is handling several calls at a time.
I have created 'task1' and 'task2' and run a loop with print statement + sleep in order to block the main thread for several seconds.
It seems like its not really blocking the main thead!!!
I can run 'task1' start to see the output for every loop and then run 'task2' and see the output of 'task2' together with task one.
I checked the limit and it seems like I can run 7 tasks without blocking.
How is that possible? according to the above, I dont need to use Celery in my organization since I will have no scenario that 2 users will run more then 2 tasks at a time.
Can someone explain why 'task1' is not blocking the starting of 'task2'?
#app.route('/task1', methods=['POST'])
def task1():
for i in range(4):
print('task 1 - ' + str(i))
time.sleep(1)
return 'message'
#app.route('/task2', methods=['POST'])
def task2():
for i in range(5):
print('task 2 - ' + str(i))
time.sleep(1)
return 'message'
<script >
function runTask(){
document.getElementById('task').value = "this is a value"
let req = $.ajax({
url : '/task1',
type : 'POST', // post request
data : { }
});
req.done(function (data) {
});
}
function runLongerTask(){
document.getElementById('longer_task').value = "this is longer value"
let req = $.ajax({
url : '/task2',
type : 'POST', // post request
data : { }
});
req.done(function (data) {
});
}
</script>
I expected 'task1' to start only when 'task2' will finish but it seems like the two tasks is running in threads (without actually configuring a thread)
Here is the results that I got:
task 2 - 0
task 1 - 0
task 2 - 1
task 1 - 1
task 2 - 2
task 1 - 2
task 2 - 3
task 1 - 3
task 2 - 4
As I understand, using Flask without Celery will block the server availability when a user starts a long operation.
This is not precisely correct, although it's a good rule of thumb to keep heavy workloads out of your webserver for lots of reasons.
You haven't described how you are running flask - with a WSGI container, or the run options. I'd look there to understand how concurrency is configured.

How to make multiple postman requests with cycle-generated variables?

Task: I've got some id's on a server and I need to remove some of them while it's only possible to delete one per request.
Result example: requests https://ids.com/1 and https://ids.com/2
Actual result: just one request https://ids.com/2
Request:
https://ids.com/{{id}}
Environment (empty var):
id:
Pre-request Code:
const ids = [1, 2]
while (ids.length) {
pm.environment.set("id", ids.shift());
}
Test:
const id = pm.environment.get("id");
if (id)
postman.setNextRequest("request_name");
else
postman.setNextRequest(null);