0
0
Postmantesting~15 mins

Global variables in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify setting and using global variables in Postman
Preconditions (2)
Step 1: Open Postman and create a new request
Step 2: Set the request method to GET and the URL to https://postman-echo.com/get
Step 3: In the Pre-request Script tab, add code to set a global variable named 'token' with value '12345abcde'
Step 4: Send the request
Step 5: In the Tests tab, add code to verify that the global variable 'token' exists and its value is '12345abcde'
Step 6: Send the request again to execute the test script
Step 7: Open the Globals section in Postman to verify the variable 'token' is set with the correct value
✅ Expected Result: The global variable 'token' is set to '12345abcde' before the request, the test script confirms the variable exists with the correct value, and the Globals section shows the variable with the expected value.
Automation Requirements - Postman test scripts (JavaScript)
Assertions Needed:
Assert global variable 'token' exists
Assert global variable 'token' value equals '12345abcde'
Best Practices:
Use pm.globals.set() to set global variables
Use pm.globals.get() to retrieve global variables
Use pm.test() and pm.expect() for assertions
Keep scripts clear and maintainable
Automated Solution
Postman
/* Pre-request Script */
pm.globals.set('token', '12345abcde');

/* Tests Script */
pm.test('Global variable token is set', () => {
    const token = pm.globals.get('token');
    pm.expect(token).to.not.be.undefined;
    pm.expect(token).to.eql('12345abcde');
});

In the Pre-request Script, pm.globals.set('token', '12345abcde') sets the global variable before the request runs.

In the Tests tab, pm.test defines a test named 'Global variable token is set'. Inside, pm.globals.get('token') retrieves the variable. The assertions check that the variable exists and its value matches '12345abcde'.

This approach ensures the variable is set before the request and verified after the response, following Postman's scripting best practices.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using environment variables instead of global variables when global scope is needed', 'why_bad': "Environment variables are limited to a specific environment and won't be accessible globally across all collections or requests.", 'correct_approach': 'Use pm.globals.set() and pm.globals.get() to manage variables that need to be accessible everywhere.'}
{'mistake': 'Setting global variables in the Tests script instead of Pre-request Script', 'why_bad': "Tests run after the request, so the variable won't be set before the request executes, causing dependent requests to fail.", 'correct_approach': 'Set global variables in the Pre-request Script to ensure they exist before the request runs.'}
Not checking if the global variable exists before using it
Bonus Challenge

Now add data-driven testing by setting the global variable 'token' with three different values and verify each in separate requests.

Show Hint