Postman collection variables in a collection run - postman

I have a line like pm.collectionVariables.set("blah", jsonData.someField) in my Tests tab for a request.
It sets the collection variable fine if I run the request in the workspace, but not if I run a Collection Run.
That is, when I run the collection as a "Collection Run", the collection variable is not changed. (!)
Is this right? Do I need to change the variable to be an environment variable?

Related

Postman to define variables list once at the begining of collection

I need to define set of variables and they shuldn't be mixed with environment variables. At the begining of collection set I define them
pm.globals.set('a', 1);
and clear it athe the end ot tests set
pm.globals.clear();
It works if I have consecutive set of tests. I need something like postman "collection pre-request script", but it is executed before every test. How to set some variables once before collection or folder execution?
If you'd like to set variables at the collection level then you can define those through the collection settings:
https://learning.getpostman.com/docs/postman/environments_and_globals/variables/#defining-collection-variables
If you want to run the same pre-request script for every request in a collection (or folder), that can be done from the collection/folder settings too:
https://learning.getpostman.com/docs/postman/scripts/pre_request_scripts/#adding-a-pre-request-script-to-a-collection-or-folder

Can I use postman collection variable inside newman?

Using variables from scope: collection inside Postman works fine.
But when I export collection and use it inside Newman it does not work as I expected.
1) Variabes are inside collection json, in the end of file - ok.
2) I use this code:
var obj = {};
obj.categories = pm.variables.get("category_id");
obj.packages = pm.variables.get("package_id");
obj.type = "add";
pm.globals.set("switch_json", JSON.stringify(obj));
console.log("request body: " + pm.globals.get("switch_json"));
in pre-request script to get value of 2 collection variables (category_id, package_id).
3) Inside Postman all works fine, console.log return:
request body: {"categories":"14","packages":"2","type":"add"}
4) Inside Newman console.log return only:
'request body: {"type":"add"}'
Does it mean Newman do not support collection variables?
Collection Variables are stored in the variables tab in collections under Edit. Initial values are shared when you export a collection and not the current values. Newman will access these(initial Values) values.
you shall save your environment (ie. my_environment.json) then, in your newman command use the -e option to use it.
have a look here for newman options
hope this helps
Alexandre
I looked around and around for a way to set a collection variable on the command line, without using an environment file, without finding any answers so I just tested using --env-var and it worked.
In the case of the original question it would look like this:
newman run xxxxxxx --env-var package_id=14
I face the similar issue, but because I need to override the collection variable from command line using --env-var(there is no --collection-var in options) but seems not working, I think because collection variable is narrow than the environment so won't work.
I think your case works because you have different variable name, is it?

How can I use Postman Collection Variables?

When I add environment variables I can use them in my post body with {{varName}}. But this does not work for collection variables (Collection > edit > Variables tab)
With the settings as shown above, if I add {{firstName}} to my body it does not work. How can I access these collection variables in my posts?
Currently if I try to post postman will just hang for a while then give this error
Error: Script execution timed out.↵ at
ContextifyScript.Script.runInContext (vm.js:53:29)
If I use an environment variable or just type in a value it works fine.
Also, you need to make sure to save the request to the belonging collection before you can use it!
It turns out {{varName}} does work. The problem was in my pre-request script. The API I was connecting to requires a checksum on the body so it pre-processes the variables in the body, but it was not setup to handle collection variables. This was causing postman to fail. User error.

What are the "local" variables in Postman?

Postman's documentation leaves a lot to be desired. In their Variables page they say:
The following scopes are available to you:
Global
Environment
Local
Data
There's information about the Global and Environment scopes, and I believe the "Data" scope is the data from a collection run. But what are the "local" variables?
Because I'd love to have a variable that is calculated on the fly, used for the request, and then discarded. Both global and environment variables are persistent.
According to the Postman Quick Reference Guide local variables are only available within the request (or collection run) that has set them. So they are used for the request or collection run and then discarded.
When to use:
passing data from the pre-request script to the request or tests or between requests.
The behavior is a bit different in Postman vs Collection Runner / Newman, so make sure you understand how they work before using!
Setting
pm.variables.set('myVariable', MY_VALUE);
Getting
pm.variables.get('myVariable', MY_VALUE);
Removing
Local variables are automatically removed once the tests have been executed / collection run finished.
Local variables are the one you use in your Tests part.
You may even use the 'let' declaration as it is coded in javascript ...
ie:
let jsonData;
jsonData = JSON.parse(responseBody);
or use var for declaration.
var jsonData = JSON.parse(responseBody);
Though, you can erase globals on the fly using
pm.environment/global.unset(<variable>)
see here for details

initiate a variable dynamically to work in all session on workflow

I am developing an Informaticajob with multiple sessions in one workflow. I need to assign a variable ##AAR with following code
IIF(get_date_part(sysdate,'mm') <= 7, get_date_part(add_to_date(sysdate,'YY',-2),'YY'), get_date_part(add_to_date(sysdate,'YY',-1),'YY')
)
I am not sure how to get about it, I was thinking on creating a session that assigns the variable, then passes it to the workflow.
This session should be the first session to run in the workflow. but I don't know how to create a session that is not a mapping.
What could I do to get this done?
First, you define the variables in your workflow (Workflows -> Edit -> Variables) so the workflow knows about the variables.
Then, as first Task in your workflow, you take an "Assignment" instead of a session. That's the icon that looks like a calculator.
In the assignment, you can assign values to your variables.
Please note that the variables need to be names "$$..." not "##..."