Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the main purpose of a pre-request script in Postman?
A pre-request script runs before the API request to prepare or modify data needed for the request, like setting variables or generating tokens.
Click to reveal answer
beginner
How do pre-request scripts help in dynamic data handling?
They allow you to create or update data like timestamps, random values, or authentication tokens right before sending the request, making tests flexible and realistic.
Click to reveal answer
intermediate
Why is preparing data in pre-request scripts better than hardcoding it in the request?
Because it avoids stale or repeated data, reduces manual updates, and supports automated, repeatable tests with fresh data every time.
Click to reveal answer
beginner
Give an example of data commonly prepared in a pre-request script.
Generating a current timestamp or creating a unique user ID to use in the request body or headers.
Click to reveal answer
intermediate
How do pre-request scripts improve test reliability?
By ensuring each request uses valid, up-to-date data, they reduce failures caused by outdated or missing information.
Click to reveal answer
What does a pre-request script in Postman do?
ASends the API request
BRuns after the request to check response
CRuns before the request to prepare data
DGenerates the test report
✗ Incorrect
Pre-request scripts run before the API request to prepare or modify data.
Why use pre-request scripts instead of hardcoding data?
ATo make tests slower
BTo skip authentication
CTo ignore errors
DTo avoid stale or repeated data
✗ Incorrect
Pre-request scripts help avoid stale or repeated data by generating fresh data each time.
Which of these is a common use of pre-request scripts?
AGenerating a timestamp
BValidating response status
CCreating test reports
DRunning UI tests
✗ Incorrect
Generating timestamps is a common task done in pre-request scripts.
When do pre-request scripts run in Postman?
ABefore sending the request
BOnly when tests fail
CDuring test report generation
DAfter the response is received
✗ Incorrect
Pre-request scripts run before sending the API request.
How do pre-request scripts improve test automation?
ABy manually updating data
BBy preparing fresh data automatically
CBy disabling tests
DBy ignoring API responses
✗ Incorrect
They prepare fresh data automatically, making tests repeatable and reliable.
Explain why pre-request scripts are important for preparing data in Postman.
Think about what happens before sending an API request.
You got /4 concepts.
Describe a real-life example where pre-request scripts help in API testing.
Imagine you need fresh data every time you test.
You got /4 concepts.
Practice
(1/5)
1. Why do pre-request scripts run before sending a request in Postman?
easy
A. To display the test results
B. To check the response status code
C. To prepare or update data needed for the request
D. To log the response time
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 C
Quick Check:
Pre-request scripts prepare data = C [OK]
Hint: Pre-request scripts run before requests to set data [OK]
Common Mistakes:
Confusing pre-request scripts with test scripts
Thinking pre-request scripts check responses
Assuming pre-request scripts run after requests
2. Which of the following is the correct way to set a variable named token in a pre-request script?
easy
A. pm.variables.set('token', 'abc123');
B. pm.environment.get('token', 'abc123');
C. pm.variables.get('token', 'abc123');
D. pm.environment.delete('token', 'abc123');
Solution
Step 1: Identify the correct method to set a variable
In Postman scripts, pm.variables.set or pm.environment.set is used to set variables. Here, pm.variables.set('token', 'abc123'); correctly sets the variable.
Step 2: Understand other methods
pm.environment.get and pm.variables.get retrieve variables, not set them. pm.environment.delete removes variables.
Final Answer:
pm.variables.set('token', 'abc123'); -> Option A
Quick Check:
Set variable uses .set() method = B [OK]
Hint: Use .set() to assign variables in pre-request scripts [OK]
Common Mistakes:
Using .get() instead of .set() to assign variables
Confusing environment and variables methods
Trying to delete variables when setting them
3. What will be the value of the variable timestamp after running this pre-request script?
const now = new Date().getTime();
pm.variables.set('timestamp', now);
medium
A. A number representing milliseconds since Jan 1, 1970
B. An error because pm.variables.set is incorrect
C. Undefined because now is not set
D. A string representing the current date
Solution
Step 1: Understand new Date().getTime()
This returns the current time in milliseconds since January 1, 1970 (Unix epoch) as a number.
Step 2: Check variable assignment
The script sets the variable timestamp to this number using pm.variables.set, which is correct syntax.
Final Answer:
A number representing milliseconds since Jan 1, 1970 -> Option A
Quick Check:
getTime() returns milliseconds number = D [OK]
Hint: getTime() returns milliseconds number, not string [OK]
Common Mistakes:
Thinking getTime() returns a date string
Assuming variable is undefined
Confusing set() method usage
4. Identify the error in this pre-request script:
pm.environment.set('authToken', getToken());
function getToken() {
return getToken();
}
medium
A. The function getToken is missing parentheses
B. pm.environment.set is not a valid method
C. Variables cannot be set in pre-request scripts
D. The function getToken causes infinite recursion
Solution
Step 1: Analyze the function call
The script calls getToken() inside pm.environment.set('authToken', getToken()). The function getToken() returns getToken(), which calls itself again.
Step 2: Identify recursion problem
This causes infinite recursion because getToken() calls itself directly, leading to a stack overflow.
Final Answer:
The function getToken causes infinite recursion -> Option D
Quick Check:
Function calls itself recursively = infinite recursion [OK]
Hint: Avoid recursive functions in pre-request scripts [OK]
Common Mistakes:
Thinking pm.environment.set is invalid
Missing that getToken calls itself recursively
Assuming variables can't be set in pre-request scripts
5. You want to generate a random user ID before each request and store it in an environment variable userId. Which pre-request script correctly achieves this?
hard
A. const id = Math.random() * 1000;
pm.environment.get('userId', id);
B. const id = Math.floor(Math.random() * 1000);
pm.environment.set('userId', id);
C. pm.environment.set('userId', Math.random());
D. const id = Math.floor(Math.random() * 1000);
pm.environment.delete('userId');
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 variable userId.
Final Answer:
const id = Math.floor(Math.random() * 1000);
pm.environment.set('userId', id); -> Option B
Quick Check:
Use Math.floor and .set() to store random ID = A [OK]
Hint: Use Math.floor and pm.environment.set to save random IDs [OK]