Postman API Testing Tool
List of most frequently asked questions around Postman API Testing Tool. Include concepts of the postman tool, REST APIs, and Web Services.
What is scripting in postman?
Scripting provides us with the capability to make our collection and request dynamic. Using scripting we can write test suites, create requests with a dynamic parameter and can pass data among requests. Postman supports JavaScript using which we can write the script.
Where we can implement script inside the workflow of the API testing?
We can define scripting at two levels
- Before a request is sent to the server, as a pre-request script under the Pre-request Script tab.
- After a response is received, as a test script under the Tests tab.
As per the requirement, we can add pre-request and test scripts at the collection level, folder level and also at a request level. The execution order will be in the sequence
- Collection Level
- Folder Level
- Request Level
Also, we can verify the order of execution in the console-logs:
Describe postman scripting using JavaScript and chai BDD.
By default postman internally support JavaScript and chai BDD as a framework.
1- Pre-request Script
Adding a pre-request script is simple, anything you want to execute before sending a request you can add as pre-request script in the Pre-request Script tab. Consider the below example
REQUEST | URI |
GET | http://localhost:3000/users/1 |
Above URI gives the data of the user, whose ID is passed in the request, instead of hard coding the USER ID in the request we can write a pre-request script which will excuted before the request and on the runtime USER ID will be assigned to the request.
2- Test Script
We can add a number of test script under the Tests tab which will run after the execution of the request. Using test script we can do validation and various assertion on the response, based on which the test will pass or fail.
GET | http://localhost:3000/users/1 |
RESPONSE | { “id”: 1, “name”: “Jhon M”, “location”: “India”, “phone”: 9876543210, “courses”: [ “Java”, “Selenium” ] } |
For the above GET request, we are going to perform some assertion and validation.
Validating Response Code | pm.test(“Status code is 200”, function () { pm.response.to.have.status(200); }); |
Multiple assertions | pm.test (“Validate Multiple Fields in the Response”, function () { pm.response.json(); .to.eql(“Jhon M”); .to.eql(9876543210); .to.eql(“Java”); .to.eql(“Selenium”); |
Sometimes we get a response that cannot be parsed and we have to verify something from the response. In that case method “pm.response.text()” can be used to convert the entire repose in text.
Handling response which doesn’t parse | pm.test(“Body Contains the String”, function () { .to.include(“Jhon M”) |
We can specify an array of status codes to validate status codes from a set.
Varfying Status code from one of the set | pm.test(“Successful Status Code”, function() { (pm.response.code) .to.be.oneOf([200,201]) |
How to add a script to verify cookies, headers, and response time?
Testing Headers
Header Key | Value |
Content-Type | application/json; charset=utf-8 |
In order to verify the values of the headers, we can use the below script.
Script | pm.test(“Content-Type header Value”, function() { .get(“Content-Type”)) .to.eql(“application/json; charset=utf-8”) |
Testing Cookies
Cookie Key | Value |
BATPOD | d2ffc44f-dfd8-3026-be75-10e28bd81a97 |
In order to verify the values of the cookies, we can use the below script.
Script | pm.test(“Cookie Value Check”, function() { pm.expect(pm.cookies.get(“cookie name”)).to.eql(“cookie value”); }); |
Testing response time to be less than 20 ms
Script | pm.test(“Response time less than 10 ms”, function() { .to.be.below(20); |
File Upload and download using Rest API.
When we try to upload any file from the UI there will be some rest API which will execute and that will exactly upload the file similarly when we try to download a file from the UI corresponding API will execute which will download the file.
Uploading file
If you are uploading a file it means you are dealing with a POST request. You can perform a similar POST request validation and assertion after execution.
- Go to the body tab
- Select form-data
- Enter the Key as “File”
- Select the file from the “Select Files”
- Execute the request
Downloading file
If you are downloading a file it means you are dealing with a GET request. You can perform a similar GET request validation and assertion after execution.
CHECK API TESTING INTERVIEW QUESTIONS
About Postman