Why do we use pre-request scripts in Postman before sending an API request?
Think about what needs to happen before sending a request to ensure it has the right data.
Pre-request scripts run before the API call. They prepare or set data like tokens, timestamps, or variables so the request can use them. This ensures the request is ready and valid.
What will be the value of the variable authToken after running this pre-request script in Postman?
pm.environment.set('authToken', 'abc123'); pm.environment.set('authToken', pm.environment.get('authToken') + 'XYZ');
Look at how the variable is updated by appending a string.
The script first sets authToken to "abc123". Then it gets that value and adds "XYZ" to it, updating authToken to "abc123XYZ".
Which assertion correctly checks that the variable sessionId is set and not empty in a Postman test script?
const sessionId = pm.environment.get('sessionId');Think about how to check a variable is a non-empty string.
Option D checks that sessionId is a string and not empty, which confirms it is set properly. Other options check for wrong or empty values.
What error will this pre-request script cause when run in Postman?
pm.environment.set('count', count + 1);
Check if the variable count is defined before using it.
The script tries to use count without defining or getting it first, causing a ReferenceError.
Which approach best ensures dynamic data like timestamps or tokens are fresh and correctly used in Postman pre-request scripts?
Think about when and where dynamic data should be created to be used in the request.
Generating or fetching dynamic data in the pre-request script ensures the request uses fresh and valid data every time it runs. Hardcoding or updating after the request can cause stale or invalid data.