Showing posts with label Variables. Show all posts
Showing posts with label Variables. Show all posts

Dec 3, 2020

API Testing 'A Beginners View': Environment and Variables In POSTMAN

In this post let’s try to have a better understanding of the Environment and Variables in POSTMAN


Let’s start with Variables: A variable is a bucket that holds values that can be changed. Suppose you are developing a mobile application. There are some values that will be used throughout the application or some values might be unique to a functionality. Hardcoding of values everywhere in the design is not a good practice in such situations variables can be used. Define a variable as per your need i.e. locally or globally and use its referenced name wherever needed. If you are required to update the value of a variable, just update the value where you have declared. Everywhere updated values will be used.


The same concept applies to Postman. We have many usages of variables in Postman. Let's check out a few of them:

  1. In our day-to-day work life, we might need to test systems in different environments like QA, Dev, Stage, etc which would have a different set of domains. Here variables make our job easier. We can store the domain name as per environment in a variable and can call that variable here.
  2. We can use variables at several places to make it parameterized such as Query- parameters, headers, authorization, request body.
  3. Variables can be used to chain requests in the collection. Extracting data from response, storing, and using it for another request as input are possible by using variables.
  4. Variables can also be used in Pre-request and Tests Scripts. 

Next, Let’s Understand Environment: Environment in POSTMAN is a set of variables that can be assigned a value and used during your work. The environment is tied to the current operating environment: each operating environment has its own POSTMAN environment. To visualize this, I have created an “API Testing 'A Beginners View'” environment with some variables.


There are two columns for the variable's value. The only difference is that if you share your environment with your team, then the value from the first column will be shared.


Let's check out some usage of Environments:

  1. We can create, share, duplicate, export, and delete an environment. 
  2. You can download it as JSON.
  3. Variables defined in an Environment are Local variables. It means you cannot use variables of an environment in another environment. To solve this we have another type of variable called Global variables. Global variables do not belong to specific environments. It is available in all scopes. You can not select multiple environments at the same time. In that case, Global variables will be helpful.

Oct 12, 2020

API Testing 'A Beginners View': Dynamic Variable Usage

Postman uses the pseudo library to generate dummy data. You can generate random names, addresses, email addresses, and much more. You can use these pre-defined variables multiple times to return different values per request. Also, you can use these variables like any other variable defined in Postman. Their values are generated at the time of execution and their names start with a $ symbol e.g. $randomPhoneNumber, $randomCity, $randomDateFuture, etc.

To use dynamic variables in pre-request or test scripts, you need to use:
  • pm.variables.replaceIn()
  • e.g. pm.variables.replaceIn('{{$randomFirstName}}').
Recently in my daily routine work, got to use this feature, so here is how I made use of the dynamic variables 😇

My Requirement: Fetch $randomeFirstName and set the real value of $randomFirstName at the environmental level so I can access it in the test script.

Solution: 

1. In Pre-Request Script: 
    • Define a function to return the randomly generated value
    • Set an environment variable to hold the randomly generated value that returns from the function defined.
    • Snippet: 
    pm.environment.set("RandomFirstName"getRandomFirstName());
    function getRandomFirstName() {
        const RandomFirstName = pm.variables.replaceIn("{{$randomFirstName}}");
        return RandomFirstName;
    }
2. In Body: Call the variable defined at that environment level
{
"FirstName": [{
            "value": "{{RandomFirstName}}"
        }]
}

3. Example: