How to access environmental variables in test field - postman

I want to access environmental variables in tests. For ex. check if values are signed to the correct fields.
I've tried to get variable values different way and the only one working is to set a variable inside the test but then I won't be able to edit it in bulk to run tests with other presets.
pm.test("Check if caregiver information is correct", function () {
pm.expect(jsonData.caregivers[0].first_name).to.equal("{{caregiverName}}");
});
code above returns AssertionError: expected 'adam' to equal '{{caregiverName}}'
console.log(pm.variables.get("{{caregiverName}}"));
returns null
console.log("{{caregiverName}}");
returns {{caregiverName}}
I would expect the value of {{caregiverName}} to equal what I have set as in environmental variables.

As caregiverName is an environment variable and set before, you need to get using following syntax:
pm.environment.get("variable_key");
Refactor your code as follows,
pm.test("Check if caregiver information is correct", function () {
pm.expect(jsonData.caregivers[0].first_name).to.equal(pm.environment.get("caregiverName"));
});
Learn more about variables: Variables - Postman

Related

How to use system variable ##dataset_id in BigQuery view

BigQuery has session-bound (or script-bound) system variables, described here. Within a session, I can declare the value of one of those variables, e.g. with something like:
set ##dataset_id = 'my_dataset_id';
Now, I'd like to have a view (which I plan on running within a session) that includes something like:
create view foo
as
select ...
from ##dataset_id.my_table
... this doesn't work. Nor does any form of quoting around that variable. It appears use of that variable simply isn't allowed to help identify the namespace of my_table.
If that's true, I'm struggling to see the value of that variable at all. Does anyone know if I can use those variables as so, or how to prevent needing to namespace-bound all instances of my_table? I'd like to manage these query scripts outside of BQ itself, and ideally without templating in everywhere (e.g. {dataset_id}.my_table)
I have tried to replicate your concern,
Please try below:
set ##dataset_id = "SampleDataSetID";
EXECUTE IMMEDIATE format("""
CREATE VIEW sampleView1 as (
SELECT
*
FROM
%s.sampleTable
);
""", ##dataset_id);
I used EXECUTE IMMEDIATE to run the sql dynamically ( using the variable that we set to the session.)
Output:

How to make a Global Variable or Suite Variable as List in Robot Framework?

I am Trying to set a List variable that is accessible to all testcases in the suite and the value in the list should not be overwritten but new values should be appended each time.
So that I can use the List variable at the end of the execution of all the testcases for a clean up operation.
For Example the List variable will be holding value of the record created in the DB and at the end I can use this list to delete all the records created. Note each record will be created by different testcases under the same Suite.
Any help will be greatly appreciated.
Declare the variable in the *** Variables *** section and it will be accessible in all cases; if you don't give a value to it during this initialization, it will be an empty list:
*** Variables ***
#{my list}
Then just add any members you need in your cases:
Append To List ${my list} value
, and at the end - in the keyword set as Suite Teardown - you'll be able to iterate over it.
You can create this variable using keyword Set Suite Variable in each test your variable can be updated. If you want to update your variable on a global scale you can use Set Global Variable. You can have a look at the documentation for more information

Postman, Set on evraibale for "run session"

I know that we can set a variable in different scopes via a pre-request script, but can we set one for on "execution" or "run of test".
I have a folder that contains two requests to validate a scenario where the first one will create a resource with an unique id and the second one will fail by trying to create a resource with the same unique id.
I would like to generate that unique value each time the collection is run. At this time I use a collectionVariables to test and set when not present but that variable is kept between each "retry".
Can I create a variable that will be the same only for one execution of a collection ?
Thanks
I have similar cases, where I store the values in Environment variables and then unset them in the Pre-request script of the first request:
pm.environment.unset("myVariable");
So, my solution is the same as the one suggested by #so cal cheesehead.
I create a variable in either the folder pre-request or the first request script. And unset it after the last test in the last request.
The sad part is that the initialization and destruction of this variable is spread in different scripts.

How can I save value of dynamic variable in Postman?

Postman allows to generate random dummy data by using pre-defined variables, on example this one would be replaced by random company name:
{{$randomCompanyName}}
Using pre-defined variables multiple times return different values per request.
The question is how to save once generated value to the variable for further usage on example in tests, something like(it doesn't work):
pm.variables.set("company", {{$randomCompanyName}});
Thanks.
You can use the .replaceIn() function with that {{...}} syntax in the sandbox.
pm.globals.set("company", pm.variables.replaceIn('{{$randomCompanyName}}'));
I've used a global variable to store the value as you would want to use it again. You could also use either the environment or collectionVariables scope to do the same thing.

Using mapping variables in a post-session command

Workflow generates three files (header, detail, trailer) which I combine via post-session command. There are two variables which are set in my mapping, which I want to use in the post-session command like so:
cat header1.out detail1.out trailer1.out > OUTPUT_$(date +%Y%m%d)_$$VAR1_$$VAR2.dat
But this doesn't work and the values are empty, so I get OUTPUT_20151117__.dat.
I've tried creating workflow variables and assigning them via pre-session variable assignment, but this doesn't work either.
What am I missing? Or was this never going to work?
Can you see the values assigned to those variables on the session log or do they appear empty as well?
Creating workflow variables is what I'd try, but you need to assign the values with the post-session variable assignment.
Basically, you store values in a variable in your mapping and pass the values up to the workflow after the session succeeded. Here is how you can achieve that:
Define a Workflow variables $$VAR1 and $$VAR2
Define the variables in your mapping, but chose different names! So i.e. $$M_VAR1 and $$M_VAR2
In your mapping, assign the values to your mapping variables through the functions SetVariable(var as char, value as data type)
In your session, select Post-session on success variable assignment.
In step 4, the current value from $$M_VAR1 (mapping variable) is stored in your workflow variable $$VAR1 and can then be used in the workflow in command tasks like you asked.
A few notes:
I'm not 100% sure if the variable assignment is exectured before the post-session command. If the command is executed first, you could execute your command tasks in an external command task after your session.
Pre-Session variable assignment is used if you pass a value from a workflow variable down to a mapping variable. You can use this if your variables $$VAR1 or $$VAR2 are used inside another mapping and need to be initialized at the beginning.