Set and use environment and global variables in Postman scripts
Preconditions (3)
Step 1: Open the Postman request
Step 2: Go to the Pre-request Script tab
Step 3: Set an environment variable named 'userId' with value '12345' using pm.environment.set()
Step 4: Set a global variable named 'authToken' with value 'abcdef' using pm.globals.set()
Step 5: Send the request
Step 6: Go to the Tests tab
Step 7: Write a script to retrieve 'userId' and 'authToken' variables using pm.environment.get() and pm.globals.get()
Step 8: Use pm.test() to assert that 'userId' equals '12345'
Step 9: Use pm.test() to assert that 'authToken' equals 'abcdef'
✅ Expected Result: The environment variable 'userId' and global variable 'authToken' are set before the request. After the request, the tests pass confirming the variables have the expected values.