0
0
Postmantesting~10 mins

Script execution order in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks the order in which Postman scripts run: Pre-request Script runs before the request, and Tests script runs after the response is received. It verifies that variables set in Pre-request Script are available in the request and that Tests script assertions pass.

Test Code - Postman
Postman
pm.environment.set('preRequestVar', 'hello');

// Pre-request Script
// This script runs before the request is sent

// Request URL uses the variable
GET {{baseUrl}}/api/data?greeting={{preRequestVar}}

// Tests Script
pm.test('Response status is 200', function () {
    pm.response.to.have.status(200);
});
pm.test('Response contains greeting', function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.greeting).to.eql(pm.environment.get('preRequestVar'));
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Pre-request Script runs and sets environment variable 'preRequestVar' to 'hello'Environment variable 'preRequestVar' is set to 'hello'-PASS
2Request is sent to URL with variable replaced: GET {{baseUrl}}/api/data?greeting=helloRequest URL is fully resolved with variable value-PASS
3Response is received with JSON containing greeting: 'hello'Response body: {"greeting": "hello"}-PASS
4Tests script runs: Assert response status is 200Response status code is 200pm.response.to.have.status(200)PASS
5Tests script runs: Assert response greeting matches environment variableResponse JSON greeting is 'hello', environment variable is 'hello'pm.expect(jsonData.greeting).to.eql(pm.environment.get('preRequestVar'))PASS
Failure Scenario
Failing Condition: Pre-request Script does not set the environment variable or variable is not replaced in request URL
Execution Trace Quiz - 3 Questions
Test your understanding
Which script runs first in Postman when sending a request?
APre-request Script
BTests Script
CRequest Body Script
DResponse Script
Key Result
Always use Pre-request Scripts to set or prepare variables before sending requests, ensuring the request uses correct data. Tests scripts should verify the response after the request completes.