How To Use Variable In Postman
Leveraging Variables in Postman for Enhanced API Testing
Postman’s dynamic variables empower you to create efficient and reusable API tests. This guide demonstrates how to define, utilize, and manage variables within Postman for optimized testing workflows.
1. Defining Variables
Variables act as placeholders for data that can change across requests. To define a variable:
- Navigate to the ‘Variables’ tab in the Postman UI.
- Click on ‘Add Variable’.
- Enter a name for your variable (e.g.,
baseUrl
). - Provide the initial value (e.g.,
https://api.example.com
). - Click ‘Add’ to store your variable.
2. Utilizing Variables in Requests
Once defined, variables can be seamlessly incorporated into your API requests:
- In Request URLs:
- Replace hardcoded values with variable names enclosed in double curly braces (
{{ }}
). - For example, instead of
https://api.example.com/users
, use{{baseUrl}}/users
.
- Replace hardcoded values with variable names enclosed in double curly braces (
Example:
{ "url": "{{baseUrl}}/users"}
- In Request Headers:
- Add headers with variable names in double curly braces.
- For example, use
Authorization: Bearer {{token}}
to incorporate an authentication token.
Example:
{ "headers": { "Authorization": "Bearer {{token}}" }}
- In Request Bodies:
- Embed variables within the request body data.
- For instance, use
{"username": "{{username}}", "password": "{{password}}"}
to send user credentials dynamically.
Example:
{ "body": { "mode": "application/json", "raw": "{\n \"username\": \"{{username}}\",\n \"password\": \"{{password}}\"\n}" }}
3. Data-Driven Testing with Collections
Collections enable you to group related requests together, allowing for more streamlined testing workflows.
- Global Variables: Variables defined within a collection are accessible to all requests within that collection.
Example:
-
Navigate to a collection and click ‘Variables’ on the left panel to access collection variables.
-
Define global variables like
baseUrl
andtoken
for use across multiple API requests within the collection. -
Environment Variables: These variables are shared across multiple collections and can be modified based on testing environment.
Example:
- Create an environment in the
Environments
section by clicking the “Manage Environments” button in the top right corner. - Add environment variables such as
baseUrl
andtoken
to suit different environments (e.g., development, staging, production).
4. Dynamic Variable Generation
Postman supports advanced variable generation techniques:
- Pre-request Scripts: These scripts execute before each request, allowing you to dynamically modify variables based on previous responses or other factors.
Example:
pm.test("Login successful", function () { pm.response.to.have.status(200); pm.environment.set("token", pm.response.json().token); // Set token variable based on response});
- Test Scripts: These scripts execute after each request, enabling analysis of response data and updating variables accordingly.
Example:
pm.test("Verify response body", function () { pm.response.to.have.body("success"); pm.variables.set("status", pm.response.json().status); // Update status variable based on response});
5. Accessing Variables in Tests
You can easily access variable values within your test scripts:
- Environment Variables:
- Use
pm.environment.get("variableName")
to retrieve an environment variable value.
- Use
Example:
pm.test("Verify API endpoint", function () { pm.expect(pm.environment.get("baseUrl")).to.equal("https://api.example.com");});
- Global Variables:
- Use
pm.collectionVariables.get("variableName")
to fetch a global variable value.
- Use
Example:
pm.test("Verify API key", function () { pm.expect(pm.collectionVariables.get("apiKey")).to.equal("your-api-key");});
- Local Variables:
- Use
pm.variables.get("variableName")
to retrieve a local variable value.
- Use
Example:
pm.test("Verify username", function () { pm.expect(pm.variables.get("username")).to.equal("testuser");});
6. Best Practices for Variable Management
- Clear Naming: Choose descriptive variable names that reflect their purpose (e.g.,
apiToken
instead oft
). - Data Types: Ensure variables are assigned values of appropriate data types (e.g., strings, numbers, booleans).
- Scope Awareness: Understand the scope of variables (global, collection, environment) to avoid conflicts.
- Documentation: Comment your variables for clarity and easy maintenance.
By mastering variable usage in Postman, you elevate your API testing capabilities, achieving greater flexibility, reusability, and efficiency in your workflows.