Bird
Raised Fist0
Postmantesting~15 mins

Global variables in Postman - Build an Automation Script

Choose your learning style10 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

Practice

(1/5)
1. What is the main purpose of global variables in Postman?
easy
A. To store data accessible across all requests and tests
B. To store data only for the current request
C. To create temporary variables that disappear after one test
D. To define environment-specific settings

Solution

  1. Step 1: Understand global variable scope

    Global variables in Postman are accessible everywhere, across all requests and tests.
  2. Step 2: Compare with other variable types

    Unlike environment or local variables, global variables persist and are shared globally.
  3. Final Answer:

    To store data accessible across all requests and tests -> Option A
  4. Quick Check:

    Global variables = accessible everywhere [OK]
Hint: Global variables are shared everywhere in Postman [OK]
Common Mistakes:
  • Confusing global variables with environment variables
  • Thinking global variables are temporary
  • Assuming global variables are request-specific
2. Which of the following is the correct syntax to set a global variable named token with value abc123 in Postman?
easy
A. pm.globals.add('token', 'abc123');
B. pm.global.set('token', 'abc123');
C. pm.globals.set('token', 'abc123');
D. pm.globalVariables.set('token', 'abc123');

Solution

  1. Step 1: Recall correct Postman global variable syntax

    The correct method to set a global variable is pm.globals.set(key, value).
  2. Step 2: Identify the correct object and method

    Only pm.globals.set('token', 'abc123'); uses pm.globals.set correctly with proper quotes and parameters.
  3. Final Answer:

    pm.globals.set('token', 'abc123'); -> Option C
  4. Quick Check:

    Use pm.globals.set to set global variables [OK]
Hint: Use pm.globals.set('key', 'value') to set globals [OK]
Common Mistakes:
  • Using pm.global instead of pm.globals
  • Using .add instead of .set method
  • Incorrect object names like pm.globalVariables
3. Consider this Postman test script:
pm.globals.set('userId', '42');
const id = pm.globals.get('userId');
console.log(id);

What will be printed in the Postman console?
medium
A. undefined
B. 42
C. userId
D. null

Solution

  1. Step 1: Set global variable 'userId' to '42'

    The first line stores the string '42' in the global variable named 'userId'.
  2. Step 2: Retrieve and log the global variable

    The second line gets the value of 'userId' which is '42', then logs it.
  3. Final Answer:

    42 -> Option B
  4. Quick Check:

    pm.globals.get('userId') returns '42' [OK]
Hint: pm.globals.get returns the stored value [OK]
Common Mistakes:
  • Expecting variable name instead of value
  • Confusing undefined with null
  • Forgetting to set the variable before getting
4. You wrote this Postman script to set a global variable:
pm.globals.set(userId, '100');

But it throws an error. What is the problem?
medium
A. The variable name userId should be a string in quotes
B. pm.globals.set cannot set numeric values
C. pm.globals.set requires three arguments
D. Global variables cannot be set inside scripts

Solution

  1. Step 1: Check the syntax of pm.globals.set

    The first argument must be a string key, so it should be quoted like 'userId'.
  2. Step 2: Identify the error cause

    Without quotes, userId is treated as an undefined variable causing an error.
  3. Final Answer:

    The variable name userId should be a string in quotes -> Option A
  4. Quick Check:

    Keys must be strings in pm.globals.set [OK]
Hint: Always quote variable names in pm.globals.set [OK]
Common Mistakes:
  • Not quoting the key name
  • Thinking pm.globals.set needs more arguments
  • Believing global variables can't be set in scripts
5. You want to share a session token between multiple requests in Postman using global variables. Which sequence correctly sets and retrieves the token?
// Request 1
pm.globals.set('sessionToken', 'xyz789');

// Request 2
const token = pm.globals.get('sessionToken');
console.log(token);
hard
A. You must use environment variables instead of global variables
B. You need to pass the token as a parameter, not use globals
C. pm.globals.get cannot retrieve variables set in other requests
D. This sequence correctly shares the token globally

Solution

  1. Step 1: Set global variable in first request

    The first request sets 'sessionToken' globally using pm.globals.set.
  2. Step 2: Retrieve global variable in second request

    The second request gets the same 'sessionToken' using pm.globals.get and logs it.
  3. Final Answer:

    This sequence correctly shares the token globally -> Option D
  4. Quick Check:

    Global variables share data across requests [OK]
Hint: Set in one request, get in another using pm.globals [OK]
Common Mistakes:
  • Thinking globals can't share data between requests
  • Confusing environment and global variables
  • Trying to pass token manually instead of using variables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard