2019 / SEPTEMBER / 4


API API TestingPostman

How to Use Postman for API Testing


How to Use Postman for API Testing

Postman is currently one of the most famous tools for API Testing. This tool is very popular if you need to dissect the RESTful API made by others or if you want to test an API you have made yourself.
But what do we exactly mean by an API? API means Application Programming Interface which helps software applications to interact with each other via API Calls.
Postman began as a side project to simplify the workflow of an API during testing and development.

After reading this short introduction you must be wondering why Postman is so popular among the crowd.
There are various reasons for the popularity. Let’s begin by discussing reasons first, then we will go to Step by Step installation guide of Postman.

  1. Using Postman just needs you to log-in to your account and here you go. You can access all your files anytime, anywhere. All you need is a postman desktop application.

  2. Postman helps to organize test suites by allowing collection creation for API calls. Each collection can create multiple requests and subfolders.

  3. It is possible to create, export, import collections. Thus, making it easier to share files. Collections can be shared via direct link.

  4. Having multiple environments aid in less repetition of tests as the same collection can be used but for a different environment.

  5. Test Checkpoints can be added to each API call, thus ensuring test coverage.

  6. Using Collection Runner or Newman helps in automating testing where tests can run in multiple iterations, saving time for repetitive tasks.

  7. Postman is able to support Continuous Integration.

After having such a detailed discussion, You must now a bit convinced that Postman is not a bad choice. In Fact, it deserves to be in the top 5 list.

Let’s quickly start discussing steps to install and download Postman. So, that we can start working on it.

Postman is an open-source tool.

Steps to Install Postman

  1. Follow the link https://www.getpostman.com/downloads/ to download the Postman software. Choose your desired platform from Windows, Mac or Linux. If you chose Windows, choose from 32 -bit or 64-bit.

Postman open-source tool

  1. Once downloaded, run the executable file.

Installing Postman

  1. There are 3 ways to use Postman:
         a. Signup and create your own account and start using Postman.
         b. Sign-in using Google Account and start using Postman.
         c. Skip Sign-up and sign-in procedure and directly start using the software.

    In the 3rd case, you won’t be able to access your collections. So, it is better to create an account or sign-in using Google credentials so that your collections are saved and can be accessed later.

Postman Sign Up

  1. Select the workspace tools you need, and click on Save My Preferences.

Postman Workspace

  1. You will see the startup screen.

Postman Startup screen

How to Use Postman

If you clearly observe the screenshot below, you will see that the workspace itself is a puzzle with so many pieces. In order to solve this, you need to have a basic knowledge of the basic pieces. Let’s start discussing a few of the important components of the workspace.

  1. New: This is used to create new building blocks like request, collection or an environment or alternatively you can also create advanced components like Documentation, Mock Servers, Monitor.
  2. Import: This helps to import a collection of an environment. It supports the following types of import options:
    a. Import from a File
    b. Import from a folder
    c. Import from a link
    d. Paste Raw Text.
  3. Runner: It is possible to run the automation test through the collection runner.
  4. Open New: This button helps you to open a new tab, Postman Window or Runner Window.
  5. My Workspace: There are 2 ways to create a Workspace: either a Personal workspace or as a Team. There is also an option to view the Workspace.
  6. Invite: You can collaborate on a workspace by simply inviting the team members.
  7. History: Past requests sent by you will be displayed in this section. There is also a toggle switch to save responses. This helps in tracking actions easily.
  8. Collections: Collections help in organizing test suites, where the collection can consist of multiple subfolders and multiple requests.
  9. Request Tab: This tab displays the title of the request you are working on. By default “Untitled Requests will be displayed for requests without any title.
  10. HTTP Request: This is a drop-down menu listing different types of HTTP Requests. Some of the requests that are available are: GET, POST, DELETE, PUT, HEAD, OPTIONS and many more. Most commonly that are used are GET and POST.
  11. Request URL: This is the text box asking for the URL with which API will communicate with. This is often referred to as endpoint.
  12. Save: In case of changes in a request, click on the Save button to commit the new changes and prevent them from being lost or overwritten.
  13. Params: This holds the parameters like key values for the requests.
  14. Authorization: Proper authorization is needed to access APIs. Authorization can be done using any credential set like username- password, bearer token, etc.
  15. Headers: You can customize headers as per the need of your organization. Headers like Content-Type JSON can be added.
  16. Body: This is the main section that can be customized in an HTTP Request. Most commonly POST Request requires customization inside the body of the request.
  17. Pre-request Script: pre-request scripts are used for setting the environment to ensure tests will be executed in the correct environment. So, these are the scripts that are executed before running an actual request.
  18. Tests: These are the scripts that are executed during a request to set up checkpoints like to verify if the retrieved data is as expected if the response status is ok and many more.

How to execute various HTTP requests in Postman

  1. Working with GET Request.

  2. Working with POST Requests.

  3. Parameterization of Requests.

  4. How to Create Postman Tests.

  5. How to Create Collections in Postman.

  6. How to run collections using Runner and Newman.

  7. Working with GET Requests: GET Requests are used to retrieve information from the specified URL.
    We will be using the following URL in our examples:
    https://jsonplaceholder.typicode.com/users
    Steps to execute GET Request:
        a. In the workspace, set HTTP Request to GET Request type.
         b. In the request URL field, input the URL -
    https://jsonplaceholder.typicode.com/users
         c. Click the Send button.
        d. On successful completion of the GET REquest, you will see 200 OK Messages. It might be possible that GET Request failed to execute. Reasons can be two- one the requested URL is invalid or authentication is needed.

NOTE: There is no data manipulation at the endpoint in the case of GET Requests. The user just queries a page. User is not allowed to send data to manipulate the page.

Postman GET Request

  1. Working with POST Requests: POST Request is different from GET Request in the sense that Users here are involved in manipulating endpoints. Using data from the results of the previous example. Let’s add a new user.

     1. Click on the + sign to create a new request.

Postman POST Request

    2. Select the HTTP request type to POST Request.
   3. Input the same URL from the previous example:
https://jsonplaceholder.typicode.com/users.
    4. Switch to the Body Tab. Here we will type the code with correct curly braces to add a new user.

Body tab in Postman
   5. Click raw and then select JSON.

Bady tab in Postman
   6. Copy and paste complete details of one user from the output of the previous example. NOTE: change the id of the user to 11. You can also change other details also . But changing id is mandatory.

  [
      {
          "id": 11,
          "name": "Rashi Garg",
          "username": "RashiG",
          "email": "[email protected]",
          "address": {
            "street": "APHB Colony, Andhra Pradesh Housing Board Phase-IV, ",
            "suite": "A-77",
            "city": "Hyderabad",
            "zipcode": "500032",
            "geo": {
                  "lat": "17.440081",
                  "lng": "78.348915"
              }
          },
          "phone": "9899787777",
          "website": "wwww.frugaltesting.com",
          "company": {
            "name": "FrugalTesting",
            "catchPhrase": "On Demand Performance Testing",
            "bs": "Cloud Based Load Testing Solution"
          }
      }
  ]

NOTE: The POST Request should have the correct format. Please check the format of the JSON request. You can use tools like
https://jsonformatter.curiousconcept.com/

JSON Request Format

    7. Click the send button.
   8. Status 201: Created shows the success of POST Request.
   9. The POST data is shown in the body.

Posted Data JSON Request

3. Parameterization of Requests: Parameterization of Request is one of the most important feature of Postman. Variables with parameters can be used instead of using the same request with different data. The data can be from a data file or an environment variable. Parameterization helps to the repetition of the test.
Let’s have a look at the example. Parameters are created using curly braces {{Parameters}}

     a. Set HTTP Request Type to GET Request.
    b. Input URL: {{url}}/users in the text box Enter the URL.
    c. Click the send button
NOTE: There will be no response as we have not set the source of the parameter.

Parametrization
   4. In order to use the parameter, you need to set the environment. Click on the eye icon.
   5. Click edit and set the variable to the global variable in order to make it accessible in all collections.

Variables in Postman

    6. In the variable set the name of the URL as
https://jsonplaceholder.typicode.com
   7. Click the Save button.

Postman

    8. Click Close if you see the next screen.

Postman

    9. Go back to the GET Request screen and click the Send button. This time you will be able to see the results.

Postman results
NOTE: Please ensure that parameters have a source like a destination file or an environment variable.

How to create Postman Test

Postman Tests are Javascript codes added to the requests to verify the results such as successful or failed status etc. /it can be compared to assert and verify command in other tools.

  1. Go to GET Request from the previous example.
  2. Switch to Test Results.

Postman Test Results

  1. Switch to Tests tab. On the right side are snippet codes.

Tests tab Postman

  1. From the snippets section, click on the “Status code: Code is 200”. You will observe that the pane is auto-populated.

Status Codes

  1. Now click the send button. You will see the test results.

Postman Test Results

  1. Let’s generate another test. Go to the test tab.

  2. From the snippets section. Click on “Response body: JSON value check”. We will be checking if Ervin Howell has user id 2.

JSON Value check in Postman

  1. Replace “Your Test Name” from the code with “Check if user id 2 is Ervin Howell” so that test name gives a clear description of what we intend to do.
  2. Replace “jsonData.value” with “jsonData[1].name”. This works like an array indexing where the first result starts at position 0, second from position 1 and so on.
    10.Replace value 100 in the method “to.eql(100)” with “to.eql(“Ervin Howell”)”.
  3. Click the send button.
  4. There will 2 PASS Test Results for your test.

Postman Test Results

Share:


Recommended Posts