Summary: Postman lets you create request collections for API calls. Each collection can have several folders and save multiple requests. But how can you create collections? Let’s find out.
API testing is important to ensure that your APIs work smoothly when it gets a lot of requests. Postman is one of the most popular tools that you can use for API building and testing. From sending API requests, receiving responses, to analyzing test results, every aspect of API testing can be managed with this software.
In this article, we will learn more about how you can leverage Postman to streamline API development cycle.
Postman is an API platform that helps testers and developers in building, testing, and managing APIs. It helps in making API testing effective with features like request builders, response visualizations, test automation, etc.
With its collaboration features, you can easily share test results and collection suites with other members in real-time to boost collaboration.
Postman platform offers a comprehensive set of tools to streamline API lifecycle right from API designing and testing to documenting APIs. Some common features of Postman Workspace include Collection Runner, Request History, Pre-Request Scripts, Test Execution, and so on.
Let’s learn in detail about its features and how can you execute APIs via requests in Postman.
GET Requests are used for retrieving data from a particular URL and no changes would be made to the URL. We will follow the given URL for all Postman examples in the guide.
https://jsonplaceholder.typicode.com/users
Step 1: In the workspace, set the HTTP request to GET request.
Step 2: In the request URL field, add link and click Send
Step 3: Lastly, you will see almost 200 OK Message. There will be 10 user results in the body that will highlight the successful running of the test.
POST Requests differ from GET requests as they involve data manipulation with the users inserting more data into the API endpoint. We are using the same data from GET requests to work on SET Requests.
Step 1: Choose a new tab and create a new request.
Step 2: In this new tab, configure the HTTP request to POST request and add the same link in the request URL and switch to body tab.
Step 3: In the body, open Raw and choose JSON file format.
Step 4: Paste a single user result from the previous GET request as shown below. Make sure the code is pasted correctly with braces and brackets. Next, change the ID to 11 and name it.
[
{
"id": 11,
"name": "Krishna Rungta",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
Step 5: Next, click on send and status 201 will be displayed. The posted data will be shown in the body.
Data parameterization is the procedure of converting test values into reusable parameters. It helps in avoiding same tests repetition and iterations. To create parameterize requests, follow the below steps:
Step 1: Set the HTTP request to GET request and add the same link as used above jsonplaceholder.typicode.com/users.
Step 2: Replace the initial part of the link with parameters like {{url}}. After that, your request URL will be shown like {{url}}/users and click send. There will be no response because the source of parameter has not been configured.
Step 3: To utilize the parameter, you must configure the environment. For that, click on the eye icon and open Edit to set variable in a global environment to be used in all test collections.
Step 4: In variable, name the URL which you have used, i.e. https://jsonplaceholder.typicode.com and click save.
Step 5: Select close in case you see the upcoming screen. Return to GET request and click send. After that, your request results will be displayed.
Postman Tests are JavaScript codes which are added to requests to verify results like successful or failed testing, comparing expected outcomes, etc. Here are the steps that you need to follow to perform basic API testing for parameterize requests.
Step 1: Go to the GET user request from the previous section and switch to tests tab. Next, on the right side, you will see code snippets.
Step 2: Within the snippet section, open “Status code: Code is 200”. The window will automatically pop up as shown below.
Step 3: Next click on Send and test results will be displayed. After that, go to the test tab and add one more test. This time, it will be a comparison between the expected and the actual outcomes.
Step 4: From the snippets section, select “Response body: JSON value check” to check if Leanne Graham has the user id 1.
Step 5: Replace “Your Test Name” with “Check if user with id1 is Leanne Graham” to specify what you need to check.
Step 6: Change jsonData.value with jsonData[0].name and to get the path, check the body early in GET results. Because Leanne Graham is userid 1, jsonData must start with O. To get the second result, utilize jsonData[1], and so on for upcoming outcomes. Lastly, in to eql, add “Leanne Graham” as shown below.
Step 7: Tap on send and two passed test results will be displayed for the request.
Collections are important to organize the API test suites and can be easily shared among the team members. Here are the steps you need to follow to create one:
Step 1: Press the New button from the top left corner of the page.
Step 2: Choose Collection and a new collection window will pop up.
Step 3: Add the collection name and description, a new collection will be created.
Step 4: Go to the previous GET request and click on save.
Step 5: Select Postman Test Collection and click on Save to Postman Test Collection.
Step 6: Postman test collection will now contain one request and follow steps 4-5 times for the previous Post request so the collection will have two requests.
Let’s learn the steps to run and test multiple API collections with Collection Runner:
Step 1: Click on the Runner button at the top of the page
Step 2: Collection Runner page will pop up along with description of several fields
Step 3: Run the Postman Test Collection by selecting collection and setting Iterations as 3. Next, you need to set the time 2500s to execute requests.tly, click on the Run Test option.
Step 4: Test Run Results page will be shown after you click the Run button. Here you will be able to see the test results as per the iteration done. It will show the pass status for GET requests. Since we did not have tests for post requests, you will get the message “the request did not have any tests.”
To run collections from Newman, first install it using http://nodejs.org/download/. Next, open the command line and add npm install -g newman.
Step 1: In the Collections box, go to three dots and select Export.
Step 2: Select Export Collection as Collection v2.1 (Recommended) and click Export. Next, select the location to save collection and click save.
Step 3: Next, to export the environment, click on the eye icon in the environment dropdown in Global and select Download as JSON. After that, select the location and save the environment.
Step 4: Next, return to the command line and change the directory to where you saved the collection and the environment.
cd C:\Users\Asus\Desktop\Postman Tutorial
Step 5: Run the collection by entering this command
newman run PostmanTestCollection.postman_collection.json -e Testing.postman_globals.json
After that the results will appear as shown below:
Conclusion: Postman is a powerful tool to create, manage, and execute API requests. To effectively use Postman for API testing, you must organize requests using collections, leverage variables for dynamic data, create and run automated test scripts, and utilize the comprehensive set of testing features such as assertions and environments.
With its versatility, Postman simplifies the process of testing APIs and contributes to the overall success of software development projects.
To automate API testing in Postman, you can create test suites where you can save your APIs requests. Once done, the series of requests will be automated for API testing.
To create API in Postman, you can use its API builder. With it, you can import API definitions and collections, add code repository, add a new name to the API, etc.
To test API endpoints in Postman, you need to choose the API for testing, then select test and automation, and select requests collections. After that, you can run the test and analyze test results.
In Postman, you can go to “New” button in the left sidebar and open the WebSocket Request tab. In this tab, you can add the WebSocket API URL, build a connection, and send and receive messages easily.
To do performance testing in Postman, choose Collections in the sidebar and choose the request collection you want to use for testing. After configuring the Collection, you can run the test.
Introducing Xoriant Corporation, leading player in the era of product development, engineering, and consulting… Read More
The dark web is a part of the internet that isn't indexed by standard search… Read More
A strong sales pipeline is indispensable for the expansion of every business organization. It's simply… Read More
In our earlier blogs, we have already discussed website cookies. Now, we will try to… Read More
Remote desktop software, which is also known as remote access software, allows users to interact… Read More
Human resource planning is the simplest way to describe strategy for ensuring that the… Read More