0
0
Postmantesting~10 mins

Global variables in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test verifies that a global variable in Postman is set correctly and can be accessed in a subsequent request. It checks that the variable value matches the expected string.

Test Code - Postman Sandbox
Postman
pm.test("Set and verify global variable", function () {
    pm.globals.set("userToken", "abc123token");
    const token = pm.globals.get("userToken");
    pm.expect(token).to.eql("abc123token");
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test script starts execution in Postman SandboxPostman test environment ready to run script-PASS
2Set global variable 'userToken' to 'abc123token' using pm.globals.setGlobal variables now include userToken = 'abc123token'-PASS
3Retrieve global variable 'userToken' using pm.globals.getVariable 'token' assigned value 'abc123token'-PASS
4Assert that retrieved token equals 'abc123token' using pm.expectAssertion compares 'abc123token' to 'abc123token'pm.expect(token).to.eql('abc123token')PASS
5Test script completes successfullyTest result recorded as pass-PASS
Failure Scenario
Failing Condition: Global variable 'userToken' is not set or has a different value
Execution Trace Quiz - 3 Questions
Test your understanding
What does pm.globals.set("userToken", "abc123token") do in the test?
ARetrieves the global variable 'userToken'
BStores the value 'abc123token' as a global variable named 'userToken'
CDeletes the global variable 'userToken'
DRuns an HTTP request with 'userToken' as a header
Key Result
Always verify that global variables are set before accessing them to avoid undefined values causing test failures.