Postman Managing Variable Values Automatically
A variable is a symbolic name for - or a reference to - information. The variable’s name represents what information the variable contains. Variables allow you to store and reuse values in your requests and scripts. The type of variables in Postman might be confusing as there are quite a few of them, such as environment variables, collection variables, data variables etc. Similar to working with variables in other programming languages, in Postman, all variables conform to a specific hierarchy.
Scope is nothing but the accessibility of a variable. Using variables within specific scopes allows you to reuse values efficiently. All variables defined in the current iteration take precedence over the variables defined in the current environment, which overrides the ones defined in the global scope.
Local variables are temporary, and only accessible in your request scripts. Local variable values are scoped to a single request or collection run, and are no longer available when the run is complete.
Data variables come from external CSV and JSON files to define data sets you can use when running collections via Newman or the Collection Runner.
Manage Variable Values automatically before/after every API request
Pre-request Script: Now, if you want to do something like removing the variable value every time before the API request is sent. You can write the code for that by going to the Pre-request Script section. Like this:
Post-request Script: For performing any operation after the API gives a response like setting the token value into the variable directly. We can write the code for that by going to the Tests section. Like this:
We can access the API response by using this statement:
var jsonData = pm.response.json();
And, We can access different types of variables whether they are environment variables, collection variables etc. Using these statements:
pm.globals.get("global_varriable"); pm.collectionVariables.get("collection_varriable"); pm.environment.get("enviorment_varriable"); pm.variables.get("local_varriable");
We can get or set a variable value using .get and .set methods:
We can also check if the value is set successfully in the variable or not by printing the result on the console (Console is given at the bottom in the postman application. You can output or print anything on the consoles by writing console.log("Anything");)
Testing: We can also write some small test cases which would tell us that the API has given the expected data or not. Like this:
Checking that the API has given a 200 HTTP Status Code or not:
For viewing the Test results you need to navigate to the Test Results section
Setting the Variable value only when API give a 200 HTTP Status Code:
Checking the Response Time of an API:
Postman variables allow you to store and reuse values in your requests and scripts in an effective way. Which variables you use and how you use them can benefit your testing by knowing which scope to use and when to provide a more readable and logical way of accessing data, And by writing a script to manage variables automatically based on the response the API give, Development and testing on postman becomes much more easier and smoother than before.