5/17/2023 0 Comments Postman script![]() #1) Create a new Postman collection with the above requests GET and POST. Test: As test or post request validation, we will check whether the response returned is having HTTP Status code 200 or not.Pre request: Add a new header named “Content-Type” and set the value of the header to “application/json”.We will add the following collection level scripts for these requests. POST with request body as JSON with 2 fields i.e.We will use a Postman collection with 2 requests with the following test endpoints. Let’s look at an illustration of collection-level pre-request scripts and tests, and see the scripts getting into action for each of the requests inside the collection. Similar to pre-request scripts, tests or post-request scripts can also be applied to a collection. Those scripts would apply to all the requests that are available in the collection. Using Pre-Request & Post-Request Scripts With CollectionsĪs discussed above, pre-request scripts can also be applied at the collection level. For example, request header expecting a session ID that needs to be randomly generated and needs some other conversions like base 64 encoding or processing in general. #2) Pre request scripts are also useful when you want certain header related operations. ![]() You can try hitting the request multiple times and see the request getting hit for different values of userIds that got generated through the random variable pre-script. Use the environment variable as a part of the request body.In the above code snippet, you can see that we have generated a random value between 1 and 10 and stored it in a local variable named ‘randomVal’. Store the generated random number in an environment or local variable.Var random = Math.floor(Math.random() * 10) Generate a random number between 1 t0 10. Add logic in the pre-request script tab.We will be using the same GET API endpoint to illustrate is a random integer between 1 to 10 that would be calculated in the pre-request script. Let’s see an example of how to use the pre-request script in this case. Pre-request scripts are the perfect place to execute such logic and then use the same with request execution. #1) For example, suppose your request expects a security token that needs to be retrieved from a third party server and as this value changes with every request execution, it could not be persisted with the environment/global variables as well. Pre request scripts are generally useful when pre-processing is required before a request is executed. Please refer to the below figure to see the Request flow when the collection level pre-request script and tests are there. It’s important to note here that, pre-request scripts can also be applied at a collection level which indirectly means that a pre-request script will apply to all the requests that are part of that collection. It allows for adding dynamic behavior to request execution. Pre-request scripts are logic or piece of code that are guaranteed to execute before the request execution begins. If there is any script/logic added as a part of the pre-request script that gets executed first following which the actual request execution takes place and once the response is received, the tests or the post request scripts get executed. The pre-request script is the entry point for request execution in Postman. Refer to the below figure to understand Postman Request Flow. The scripts are nothing but Javascript code that the Postman sandbox understands and executes as desired. Postman has a powerful runtime which is Node JS based that allows adding scripting capabilities before and after request execution. Using Pre-Request & Post-Request Scripts With Collectionsįirst, let’s try to understand how Postman enables or allows the pre and test scripts to get executed in the context of request execution.
0 Comments
Leave a Reply. |