Verify Postman script execution order
Preconditions (2)
✅ Expected Result: The 'order' variable value in the test result should be 'collection-folder-request', showing the scripts ran in the correct order
Jump into concepts and practice - no test required
// Collection Pre-request Script pm.variables.set('order', 'collection'); // Folder Pre-request Script let currentOrder = pm.variables.get('order'); pm.variables.set('order', currentOrder + '-folder'); // Request Pre-request Script let orderSoFar = pm.variables.get('order'); pm.variables.set('order', orderSoFar + '-request'); // Request Test Script let finalOrder = pm.variables.get('order'); pm.test('Script execution order is correct', function () { pm.expect(finalOrder).to.eql('collection-folder-request'); });
The collection pre-request script sets the initial variable 'order' to 'collection'.
The folder pre-request script appends '-folder' to the existing 'order' variable.
The request pre-request script appends '-request' to the 'order' variable.
Finally, the test script verifies that the 'order' variable equals 'collection-folder-request', confirming the scripts executed in the correct order.
This approach uses Postman Sandbox's pm.variables API to share variables across script scopes during the request lifecycle.
Now add a Post-request Script at the collection level that appends '-post' to the 'order' variable and verify the final value includes this suffix.
Collection Pre-request Script, Folder Pre-request Script, or Request Pre-request Script?Collection Pre-request Script: sets variable var1 = 'A'Folder Pre-request Script: sets var1 = 'B'Request Pre-request Script: sets var1 = 'C'var1 in the test script?token. A folder inside the collection has a pre-request script that updates token only if it is empty. The request inside the folder has a test script that checks if token is set. Which script execution order ensures the test script sees the updated token?