Scripts in POSTMAN

Scripts in POSTMAN are lines of code which are used to automate an API test. A script can be used to set or get variables and also several functions are applied in scripts in POSTMAN. There are code snippets that are available in POSTMAN, which are small ready-made scripts and can be used directly for different purposes.

There are two different types of scripts in POSTMAN. One is the pre-scripts and the other is the test scripts. Pre-scripts gets executed before the request is made to the server. On the other hand, the test script gets executed after the request is sent into the server and the response is received.

Pre-request Script

Pre-request scripts are snippets of code, for collection request that gets executed before the request is sent to the server. For example, we may require to send some random variables into the URL parameter, or generating any key in URL, etc.

Also, using the concept of pre-script, we can get function output such as date, time, or even timestamp, etc. For example, to include a timestamp in the request headers, an environment variable can be set with the value returned from an inbuilt function of POSTMAN.

Suppose, if we write: pm.environment.set("TimeStampHeader",new Date()); in the pre-script editor. Now the variable TimeStampHeader can be used which will automatically contain the Date.

Adding Pre-request Script in folder level

When a request is pre-request is added in the folder level, then the same pre-scripts get executed for all the requests which are inside that folder. And when a pre-request is added in the collection level, then the pre-script(s) gets executed for all the request which are inside the collection.

Test Script

Tests Scripts are scripts that get executed after the response is received from the server. There are different tests that are available inside the POSTMAN Snippet. Not a single test but multiple tests can be run on a request.

Setting an Environment Variable :

pm.environment.set("variable_key", "variable_value");

Getting an Environment Variable :

var value = pm.environment.get("variable_key");

Clear an Environment Variable :

pm.environment.unset("variable_key");

Setting a Global Variable :

pm.globals.set("variable_key", "variable_value");

Getting a Global Variable :

pm.globals.get("variable_key");

Clear a Global Variable :

pm.globals.unset("variable_key");

variables- a function

variables is a function that is used to search variables across the globals and the active environment.

var value = pm.variables.get("variable_key");

Response Handling

Response Handling means testing and analyzing the response that is received from the server. There are test scripts that are written or code snippets can be used to analyze the response. Some examples of response handling are:

  • Checking for the HTTP status code.
  • Checking for a JSON value.
  • Checking if the body of the response contains a string, etc.

The POSTMAN has some in-built functionality through which some of the parameters such as HTTP status code, the size of the response is automatically checked and displayed. But, if you require some customized checking on the Response, then you must start with writing test scripts.

Check if the response body contains a string

Go to the test tab of the request you want, and then edit the following code into the editor:

pm.test("Body contains the string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

Suppose I have a response body and want to search whether name, for example, Edwards is contained in the Response.

postman-response-part

So, I shall write the below test in the test tab of the particular request:

postman-string-search-test

And after hitting the Send button, and after getting the response back from the server, Check the Result tab below to see the output of the test.

postman-string-test-result

The Test Result tab has a (1/1) which means that 1 test out of 1 test has passed. And the string we used Body matches string is displayed as the output.

Check if the response body is equal to a string

To check whether the response body is equal to the string, edit the following code into the Test editor of the POSTMAN request.

pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
Check for a JSON Value

To check the JSON Value of the response, just edit the below code into the Test editor of the POSTMAN request:

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
Check whether Content-Type Header is present in the response

To check whether the Content-Type header is present in the response, edit the below code into the Test editor of the POSTMAN request:

pm.test("Content-Type header is present", function () {
    pm.response.to.have.header("Content-Type");
})

postman-test-script

And after that, click into the Send button, to send the request and wait for the response from the server.

postman-script-result

As shown in the image, the Test Results tab shows PASS. And displayed that the Content-Type Header is present in the response body.

Check whether the response time is less than 1000 ms

Response time is the time taken by the server to send the server to your computer. Or simply, is the time required to get/load the response fully into the computer. Usually, all the response time is calculated in mili-seconds, and also takes some seconds at maximum.

In this example, we shall check whether the response time is less than 200 ms or not. Say, if yes, we shall consider it as a good website or API.

Edit the following code into the Editor :

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

postman-test-script-for-response-time

After that, send the request and wait for the response from the server.

postman-check-response-time

As shown in the above image, Checking for the response time to be below 1000ms (1 Second) has given a PASS output.

Check whether the HTTP Status code is 200

The HTTP status code signifies the status of the page or the response that is received from the server. For example, a status code 404 means, that the page requested is not there in the server or the page is not found for different reasons.

On the other hand, a status code 200 means a OK, which signifies that the page is found and a valid response is received from the server.

To check whether is the HTTP status code is 200, edit the below code into the editor.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

After editing the above code into the Test editor hit the Send button and after getting the response check the Result tab in the Editor.

postman-status-code-check

As shown in the above image, the Status code received it 200, therefore it shows a PASS.

Check whether the Code Name contains a String

To check whether the code name contains a given string, edit the following code into the Test editor:

pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});
Check whether the POST Request is successful

To check whether the POST Request is successful or not edit the below code into the editor:

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
})

Snippets in POSTMAN

Snippets in POSTMAN are nothing but small scripts that are helpful for testing APIs. Snippets are pre-built scripts that can be used directly and saves time to type codes. Snippets can be used as both pre-scripts as well as test scripts.. To create script snippets, at first we have to create a new request:

postman-new-request

Create a new request by naming it with a suitable name. I have named it as- Script_testing.

postman-request-script

Now, save the request inside the folder GET, which is created earlier inside Collection01.

postman-request-save-in-get

Now, click on the triple-dot beside the collection and select edit.

postman-collection-edit-option

Now, navigate to the pre-request Script tab in the collection level of Collection01 and on the left side a small arrow is visible. Click the arrow to see the snippets.

Now, click on the POSTMAN pre-request script tab:

postman-pre-request-script

Now, navigate to the pre-request Script tab in the collection level of Collection01 and on the left side a small arrow is visible. Click the arrow to see the snippets.

postman-snippet-list

As shown above, there are different snippets available under the snippet list, you can click on any of the snippets, and that will be added into the test script editor. This way the time to write scripts can be reduced, and also the possibility of error occurrence is reduced.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions