This post is continuation of our last articleIntroduction to Postman“.

Sending the first request

  • Enter “postman-echo.com/get” into the URL field
  • Hit the Send button to send your request, and you will see the server response at the bottom with some JSON data.
  • Notice that Postman has added postman-echo.com/get under the History tab of the sidebar

 

 

 

 

 

 

 

 

How does this work?

  • Enter your request details (URL: postman-echo.com/get) in Postman, and hit the Send button
  • The request is received by the API server (postman-echo.com), and it returns a response
  • The response is received by Postman, and the response is visualized in the interface

 Creating the First Collection

Every request that is sent in Postman appears under the History tab of the sidebar. On a small scale, reusing requests via the History section is convenient.

 

  • With the request created above in the request builder, click the Save button to open the SAVE REQUEST modal.
  • As an optional step, enter a new request name. Otherwise, the default name will be the request URL.

  • As an optional step, enter a request description in plain text or using Markdown.
  • Now, save this request to an existing collection, or create a new collection by entering a collection name, and then hit Save.

Automated Tests for APIs using Postman

One of Postman’s most powerful features is its ability to run automated tests on your requests. When you open Postman, you should see the ‘Tests’ section in the request editor area.

To the right of the test editor, there is a list of snippets. You can click on any snippet title and the corresponding code snippet will be appended to the test editor. This makes writing simple tests extremely fast.

 Writing a simple test

Demo API: http://echo.getpostman.com

Postman tests are written in Javascript. It expects the special tests object to be populated with a description and the result of the test. Postman renders the results in the “Tests” tab as well as in the Collection runner, depending on the Boolean values present in this dictionary.

For our first test, we’ll send a GET request to http://echo.getpostman.com/get to find out the status of the API. We’ll check if the status code is 200, and if the Content-Type header is “application/json”. Follow the steps below:

  • Set the URL to http://echo.getpostman.com/get
  • Scroll down in the snippets editor and click on the “Status code: Code is 200” snippet.

  • We do not need to make any changes in this case. Let’s send this request right away.
  • Switch to the “Tests” tab. We see that this test has passed as the correct status code is returned

Just to see how things look when the API does not behave as expected, let’s change the status code in the test to 201 and send the request again

  • In the tests tab, Postman tells us that the test has failed (expectedly).
  • Let’s add another test for the Content-Type header. We want to make sure that the Content-Type header exists and it is equal to “application/json”
  • responseHeaders is a dictionary available inside the Postman test sandbox which contains all the headers returned in the response. Select the “Response headers: Content-Type header check” snippet.

We’ll change this test a bit. We’ll assign the result of the responseHeaders.has()function to a variable contentTypeHeaderExists.

Remember that as Postman tests are just Javascript code, we can add conditionals like if to our code. We’ll first check if contentTypeHeaderExists is true. If yes, we’ll check if it’s equal to application/json.

Write the following snippet in the test editor:

var contentTypeHeaderExists = responseHeaders.hasOwnProperty(“Content-Type”);

tests[“Has Content-Type”] = contentTypeHeaderExists;

if (contentTypeHeaderExists) {

tests[“Content-Type is application/json”] =

responseHeaders[“Content-Type”].has(“application/json”);

}

This is how it should look like inside Postman:

  • Send the request

  • Everything seems to be fine with this API as all our test cases have passed. In case, something breaks down, these tests will fail.
  • And that’s all it takes. Usually API testing requires a huge amount of boilerplate code. By giving you an integrated environment, Postman aims to make this process painless

Create, Update & Delete Content

In this section, we’ll use the super-useful JSONBlob API. This API lets you create, update, and delete content on the server. This will be used to mimic a blog & it mirrors common API usage scenarios and will be helpful in demonstrating how Postman can make things easier for a user

Create an Environment

URL: https://jsonblob.com

  • Click on settings icon from top right corner of postman application
  • Select “Manage Environments”

  • Click on “Add” from the bottom right screen

  • Enter the environment name “jSONBlob”
  • Give Key as “url” &
  • Value as “https://jsonblob.com”
  • Click on Add

  • Select the environment in the environment list to activate it

POST request to create a new blog post

To create a new blog post, we’ll need to send a POST request to {{url}}/api/jsonBlob with the following request body:

{

“content”: “My first blog post :)”

}

Click “Send” and this endpoint will return a “Location” header, with the URL of the newly created blog post:

Use the test script feature to extract this header and store it. Write the following script in the “Test” Editor:

var bloglink = postman.getResponseHeader(“location”);

postman.setEnvironmentVariable(“bloglink”, bloglink);

Save this request to a collection.

Validate that the post was created

Now make a GET call to the saved URL to make sure the blog post was created. Set the URL to {{blogLink}}, and add a test script to validate the contents.

Update the Blog Post

We can update existing blog posts using the PUT method. Set the URL to {{blogLink}}, since we know the blog post we want to update. Let’s update the blog content to:

{

“content”: “My first blog post, updated :)”

}

Verify that the contents have been updated

We can use a modified version of the GET request we created earlier. All we need to do is change the test script to:

tests[“Has correct updated text”] = responseBody.has(“My first blog post, updated :)”);

Delete the blog post

Change the GET to DELETE to remove the blog post from the server

Verify that the blog post has been deleted

Use the same URL as the previous GET requests, and change the test script to:

tests[“Status code is 404”] = responseCode.code === 404;

A 404 response code indicates that the entity we requested was not found

 

Starting a collection run

Collections are groups of requests that can be run together as a series of requests, against a corresponding environment. Using scripts, you can build integration test suites, pass data between API requests, and build workflows that mirror the actual use case of APIs.

Collections can be run within the Postman app using the collection runner, from the command line using Postman’s Newman tool, or on scheduled intervals using Postman Monitors.

Running a collection is useful when user want to automate API testing. When user runs a collection, they are essentially sending all requests in their collection one after another.

 

Conclusion:

We have seen how to use Postman and what features are in it. Readers are always welcomed to suggest their views and add opinions.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>