Postman Tutorial for Beginners to perform API Testing: Part 2

Postman Tutorial for Beginners to perform API Testing: Part 2

2019 / 9 / 4

API, API Testing, Postman

    In the last blog post we have discussed introduction of Postman and how to install Postman and get it ready for the task.
In this blog post we will be discussing how to execute various HTTP requests in Postman.
I will be covering following topics in this blog post:

  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

Let’s get started.

  1. 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 Message. 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 end point in case of GET Requests. 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 are different from GET Request in the sense that Users here are involved in manipulating end point. Using data from the results of 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 a 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

  1. Parameterization of Requests: Parameterization of Request is one 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 repetition of the test.
    Let’s have a look at the example. Parameters are created using curly braces {{Parameters}}
        1. Set HTTP Request Type to GET Request.
        2. Input URL: {{url}}/users in the text box Enter the URL.
        3. 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 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 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 userid 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.
  3. Replace value 100 in the method “to.eql(100)” with “to.eql(“Ervin Howell”)”.
  4. Click the send button.
  5. There will 2 PASS Test Results for your test.

Postman Test Results

You might also like