Bird
Raised Fist0
Postmantesting~20 mins

Why pre-request scripts prepare data in Postman - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Pre-request Script Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of Pre-request Scripts in Postman

Why do we use pre-request scripts in Postman before sending an API request?

ATo generate the final test report after all requests run
BTo validate the response data after the request is sent
CTo set or modify variables and prepare data needed for the request execution
DTo automatically retry failed requests without user input
Attempts:
2 left
💡 Hint

Think about what needs to happen before sending a request to ensure it has the right data.

Predict Output
intermediate
1:30remaining
Output of Pre-request Script Variable Setting

What will be the value of the variable authToken after running this pre-request script in Postman?

Postman
pm.environment.set('authToken', 'abc123');
pm.environment.set('authToken', pm.environment.get('authToken') + 'XYZ');
A"abc123XYZ"
B"abc123"
Cundefined
D"XYZ"
Attempts:
2 left
💡 Hint

Look at how the variable is updated by appending a string.

assertion
advanced
2:00remaining
Correct Assertion for Pre-request Script Variable

Which assertion correctly checks that the variable sessionId is set and not empty in a Postman test script?

Postman
const sessionId = pm.environment.get('sessionId');
Apm.expect(sessionId).to.be.false;
Bpm.expect(sessionId).to.equal(undefined);
Cpm.expect(sessionId).to.be.null;
Dpm.expect(sessionId).to.be.a('string').and.not.empty;
Attempts:
2 left
💡 Hint

Think about how to check a variable is a non-empty string.

🔧 Debug
advanced
1:30remaining
Debugging a Pre-request Script Error

What error will this pre-request script cause when run in Postman?

Postman
pm.environment.set('count', count + 1);
ATypeError: Cannot read property 'set' of undefined
BReferenceError: count is not defined
CSyntaxError: Unexpected token '+'
DNo error, sets count to 1
Attempts:
2 left
💡 Hint

Check if the variable count is defined before using it.

framework
expert
2:30remaining
Best Practice for Dynamic Data Preparation in Pre-request Scripts

Which approach best ensures dynamic data like timestamps or tokens are fresh and correctly used in Postman pre-request scripts?

AGenerate or fetch dynamic data inside the pre-request script and set it as environment variables before the request runs
BHardcode dynamic data values in the request headers and update manually before each run
CUse test scripts after the request to modify dynamic data for the next request
DStore dynamic data only in global variables without updating them in pre-request scripts
Attempts:
2 left
💡 Hint

Think about when and where dynamic data should be created to be used in the request.

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

  1. 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.
  2. Step 2: Differentiate from other script types

    Scripts that check response status or display results run after the request, not before.
  3. Final Answer:

    To prepare or update data needed for the request -> Option C
  4. 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

  1. 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.
  2. Step 2: Understand other methods

    pm.environment.get and pm.variables.get retrieve variables, not set them. pm.environment.delete removes variables.
  3. Final Answer:

    pm.variables.set('token', 'abc123'); -> Option A
  4. 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

  1. Step 1: Understand new Date().getTime()

    This returns the current time in milliseconds since January 1, 1970 (Unix epoch) as a number.
  2. Step 2: Check variable assignment

    The script sets the variable timestamp to this number using pm.variables.set, which is correct syntax.
  3. Final Answer:

    A number representing milliseconds since Jan 1, 1970 -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify recursion problem

    This causes infinite recursion because getToken() calls itself directly, leading to a stack overflow.
  3. Final Answer:

    The function getToken causes infinite recursion -> Option D
  4. 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

  1. 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.
  2. Step 2: Correctly set the environment variable

    pm.environment.set('userId', id); correctly stores the generated ID in the environment variable userId.
  3. Final Answer:

    const id = Math.floor(Math.random() * 1000); pm.environment.set('userId', id); -> Option B
  4. 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]
Common Mistakes:
  • Using .get() instead of .set() to store variables
  • Not rounding random numbers to integers
  • Deleting variables instead of setting them