In this fast-paced world, we have designed many tools, platforms, and frameworks to simplify our tasks, improve accuracy, and speed up our work. Over time, programming languages have developed to introduce features that improve reusability, optimize memory management, and minimize redundant work. One essential concept that incorporates these principles is the use of variables.
What are "Variables" ?
Variables are the building blocks that enable us to understand and implement reusability in programming. Variables are used to store some data or create dataset dynamically.
Why to use "Variables"?
In Postman, variables allow us to store values and reuse them across different APIs. We can easily reference these values in collections, environments, or individual requests, making it more convenient to manage and access stored information. Using variables in your Postman requests eliminates the need to duplicate requests, which can save a lot of time!
This concept of variables is used beautifully in POSTMAN .
Where to use "Variables"?
In Postman, variables can be defined at different scope levels: Global, Environment, Collection, and Local variables. Let's dive deeper into the scope of variables and how they work.
Global Variables: Global variables are accessible throughout the workspace.
Workspaces enable you to organize your Postman work and collaborate with teammates. Workspace can contain different collections folders and requests.
Steps to create Global variable
Step 1: Create a Global Variable
Open Postman and click on the top-right corner to open the “Environment quick look" dialog.
2.Go to the "Globals" tab.
3. In the "Key" field, enter url_global.
4.In the "Value" field, enter your API's base URL, for example, http://localhost:3000/.
5. Click the "Save" button.
Now, you've created a global variable named url_global.
This variable can be accessed by all the collections present in that workspace.
So, to summarize:
A global variable is defined and stored in Postman.
The global variable is accessible by any collection within Postman.
The global variable is not limited to a specific environment and is available across all environments.
The global variable can be accessed by any request within Postman, regardless of which collection or environment it belongs to.
Collection Variables:Collection variables can be used in any request within the same collection, making them accessible across all the requests in that collection.
In a real-life example, suppose you have an UserId that will be used across multiple requests. Instead of hardcoding the UserId in every request, you set it as a Collection Variable.
1. Open the collection .
2. Select Variable tab.
3. In the "Key" field, enter UserId.
4. In the "Value" field, enter value of UserId.
5. Click the “ save” button.
This variable can be accessed by all the requests present in that Collection.
So, to summarize:
A collection variable is defined and stored in Postman.
The collection variable is accessible by any request created under that Collection.
The collection variable is not limited to a specific environment and is available across all environments.
The collection variable can be accessed by any request within that collection, regardless of which environment it belongs to. This means that collection variables are available throughout all requests in a collection and do not change based on the selected environment.
Environment Variables: In Postman, you can manage different environments such as QA, Dev, and Prod, each with its own set of variables or values. For example, each environment may have different URLs. To access a specific URL, you need to set it at the environment level. This allows you to easily switch between environments and ensure that the correct URL and other environment-specific variables are used in your requests.
Steps to create Environment Variable
1. Open Postman and select the environment.
2. Click on the top-right corner to open the “Environment quick look" dialog.
3.Go to the "Environment" tab.
4. In the "Key" field, enter url_Env.
5.In the "Value" field, enter your API's base URL, for example, http://localhost:3000/.
6. Click the "Save" button.
Now, you've created a global variable named url_Env.
This variable can be accessed by all the collections present in that Environment.
So, to summarize:
A environment variable is defined and stored in Environment.
The environment variable is accessible by any collection within that Environment.
An environment variable set in the QA environment can be used by all the collections within that environment. This means that once you define a variable, such as a URL or API key, in the QA environment, it is accessible across any requests or collections that are using the QA environment. This helps ensure consistency and avoids the need to redefine variables for each collection or request individually.
Local Variables: Local variables in Postman can be created at the request level within the pre-request script. These variables are defined before the request is sent and are used within the request URL. They are created and assigned values at runtime, just before the request is executed.
We have to use Postman's pm library
pm.variables.set("url_local","http://localhost:3000/")
Steps to create Local Variable
1. Open Postman and select any request of a collection.
2. Click on the Script tab and select pre-request.
3. Define the variable .
4. Click the "Save" button.
Now, you've created a global variable named Books_URL.
Variables exist in different scopes, such as "Global," "Environment," "Collection," and "Local" as represented in the illustration below:
Scope hierarchy
Different script syntaxes exist to set or get variables for each scope in the Postman.
Syntax
The syntax to set and get variables based on each scope is as follows:
Global
set: pm.global.set('variable_key', 'variable_value')
get: pm.global.get('variable_key', 'variable_value')
Environment
set: pm.environment.set('variable_key', 'variable_value')
get: pm.environment.get('variable_key', 'variable_value')
Collection
set: pm.collectionVariables.set('variable_key', 'variable_value')
get: pm.collectionVariables.get('variable_key', 'variable_value')
Local
set: pm.variables.set('variable_key', 'variable_value')
get: pm.variables.get('variable_key', 'variable_value')
Conclusion
Variables in Postman make API testing more flexible and efficient. They help you run dynamic tests, link requests, and manage different environments. Understanding and using these features can improve your testing process and make it more effective.
コメント