Why pre-request scripts prepare data in Postman - Automation Benefits in Action
pm.test('Pre-request script sets authToken', function () { pm.expect(pm.environment.get('authToken')).to.be.a('string').and.not.empty; }); pm.test('Authorization header contains authToken', function () { const authHeader = pm.request.headers.get('Authorization'); const token = pm.environment.get('authToken'); pm.expect(authHeader).to.equal(`Bearer ${token}`); }); pm.test('Response status is 200', function () { pm.response.to.have.status(200); });
The first test checks that the pre-request script has set the environment variable authToken to a non-empty string before the request is sent.
The second test verifies that the Authorization header in the request includes the token prepared by the pre-request script, ensuring the API receives the correct data.
The third test confirms the API responded with status code 200, indicating success.
Using pm.environment.set() in the pre-request script stores data for use in the request. The tests use pm.test() and pm.expect() for clear, readable assertions.
Now add data-driven testing by preparing different tokens for three different user roles and verify the API response for each.
Practice
Solution
Step 1: Understand the role of pre-request scripts
Pre-request scripts run before the request is sent to prepare or update any data needed for the request.Step 2: Differentiate from other script types
Scripts that check response status or display results run after the request, not before.Final Answer:
To prepare or update data needed for the request -> Option CQuick Check:
Pre-request scripts prepare data = C [OK]
- Confusing pre-request scripts with test scripts
- Thinking pre-request scripts check responses
- Assuming pre-request scripts run after requests
token in a pre-request script?Solution
Step 1: Identify the correct method to set a variable
In Postman scripts,pm.variables.setorpm.environment.setis used to set variables. Here,pm.variables.set('token', 'abc123');correctly sets the variable.Step 2: Understand other methods
pm.environment.getandpm.variables.getretrieve variables, not set them.pm.environment.deleteremoves variables.Final Answer:
pm.variables.set('token', 'abc123'); -> Option AQuick Check:
Set variable uses .set() method = B [OK]
- Using .get() instead of .set() to assign variables
- Confusing environment and variables methods
- Trying to delete variables when setting them
timestamp after running this pre-request script?const now = new Date().getTime();
pm.variables.set('timestamp', now);Solution
Step 1: Understand
This returns the current time in milliseconds since January 1, 1970 (Unix epoch) as a number.new Date().getTime()Step 2: Check variable assignment
The script sets the variabletimestampto this number usingpm.variables.set, which is correct syntax.Final Answer:
A number representing milliseconds since Jan 1, 1970 -> Option AQuick Check:
getTime() returns milliseconds number = D [OK]
- Thinking getTime() returns a date string
- Assuming variable is undefined
- Confusing set() method usage
pm.environment.set('authToken', getToken());
function getToken() {
return getToken();
}Solution
Step 1: Analyze the function call
The script callsgetToken()insidepm.environment.set('authToken', getToken()). The functiongetToken()returnsgetToken(), which calls itself again.Step 2: Identify recursion problem
This causes infinite recursion becausegetToken()calls itself directly, leading to a stack overflow.Final Answer:
The function getToken causes infinite recursion -> Option DQuick Check:
Function calls itself recursively = infinite recursion [OK]
- Thinking pm.environment.set is invalid
- Missing that getToken calls itself recursively
- Assuming variables can't be set in pre-request scripts
userId. Which pre-request script correctly achieves this?Solution
Step 1: Generate a random integer ID
Math.floor(Math.random() * 1000)generates a random whole number between 0 and 999, suitable for a user ID.Step 2: Correctly set the environment variable
pm.environment.set('userId', id);correctly stores the generated ID in the environment variableuserId.Final Answer:
const id = Math.floor(Math.random() * 1000); pm.environment.set('userId', id); -> Option BQuick Check:
Use Math.floor and .set() to store random ID = A [OK]
- Using .get() instead of .set() to store variables
- Not rounding random numbers to integers
- Deleting variables instead of setting them
Ready to prove it?
Pass the test and this pattern moves unknown
