Postman Scripting: Before and After the Request
Introduction
Scripting in Postman offers powerful capabilities to customize requests and responses. This guide delves into scripting both before and after requests, showcasing how to manipulate data, authenticate, and handle responses effectively. By mastering these techniques, you'll unlock new levels of flexibility and automation in your API testing and development workflows.
Pre-request scripts in Postman
What is pre-request scripts ?
You can use pre-request scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request Script tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data. You can also use pre-request scripts for debugging code, for example by logging output to the Postman Console.
Write Scripting before your request runs
To include code you want to execute before Postman sends a request, do the following:
Select Collections in the sidebar.
Open the request, then select the Pre-request Script tab.
Enter the JavaScript you need to process before the request runs, then select Save.
Select Send to send the request. The code will execute before Postman sends the request to the API.
Pre-request scripting example
An example usage of pre-request scripting could be as follows:
You have a series of requests in a collection and are running them in a sequence, such as when using the collection runner.
The second request is dependent on a value returned from the first request.
The value needs to be processed before you pass it to the second request.
The first request sets the data value from a response field to a variable in its Tests script.
The second request retrieves the value and processes it in its Pre-request Script, then sets the processed value to a variable (which is referenced in the second request, for example in its parameters).
Test scripts in Postman
What is Test Script in Postman
The Pre-request tab enables you to do any processing needed before sending a request, like setting variable values. Any code here runs before the request is sent. To learn more, see Write pre-request scripts.
The Tests tab allows for any post-processing after a request is sent and includes the ability to write tests for assessing response data. The Test tab sandbox has the Chai.js library built in, so you can use Chai's behavior-driven development (BDD) syntax to create readable test assertions.
Write Scripting after your request runs
You can add tests to individual requests, collections, and folders in a collection. Postman includes code snippets you add and then change to suit your test logic.
To add tests to a request, open the request and enter your code in the Tests tab. Tests will execute after the request runs. The output is in the response's Test Results tab.
If you need to execute code before a request runs, use Pre-request Scripts instead. See Intro to scripts for more on how your scripts execute when your requests run.
Test scripting example
Validate responses
To validate the data returned by a request, you can use the pm.response
object in a test. Define tests using the pm.test
function, providing a name and function that returns a boolean (true
or false
) value indicating if the test passed or failed. Use Chai.js BDD syntax and pm.expect
in your assertions to test the response detail.
The first parameter for the .test
function is a text string that will appear in the test result output. Use this to identify your tests, and communicate the purpose of a test to anyone viewing the results.
For example, enter the following in the Tests tab of a request to test if the response status code is 200
:
pm.test("Status test", function () {
pm.response.to.have.status(200);
});
Select Send to run your request and open Test Results in the response section. The tab header displays how many tests passed and how many ran in total. You can also view the number of Passed, Skipped, and Failed test results.
If the request returned a 200
status code, the test passes. To find out what happens with a different status code, change the expected status code in your test script and run the request again.
Conclusion
Having explored scripting both before and after requests in Postman, you now possess valuable tools to enhance your API testing and development processes. Whether it's preprocessing data, adding dynamic elements, or extracting insights from responses, scripting empowers you to customize every aspect of your requests. Keep experimenting with Postman's scripting capabilities to optimize your workflows further and achieve your testing objectives with precision.