How to Create an Environment in Postman?

HOME

{
    "email": "eve.holt@reqres.in",
    "password": "pistol"
}

How to generate Newman HTML Report?

HOME

npm install -g newman-reporter-htmlextra

newman run API_Newman_Tests.json -r htmlextra

newman run API_Newman_Tests.json -r htmlextra --reporter-htmlextra-export ./results/report.html

How to import Collection into Postman?

HOME

How to run Postman requests from the command line

HOME

In this tutorial, we will run the Postman requests from the command line. Newman is a command line collection runner for Postman.

node -v

npm -v

npm install -g newman

On cmd go to that location of collection json file

cd C:\Users\Vibha\Desktop\Automation\Postman

newman run API_Testing.json

How to Export Postman Collections?

HOME

How to create Tests in Postman

HOME

In this tutorial, we will see how we can create Tests in the Postman.

Consider a sample JSON which has 4 sets of data. This data we will be passing to a POST request.

Create a request in Postman. Below is the URL of the request.

https://reqres.in/api/users

Paste the body of the request in the Body part of the Postman.

Create Tests for the request

Click on the “Tests”. Write the tests as shown below:

pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

pm.test("The endpoint does not return unexpected status code", () => {
 
  // change 404 to the response code you do not expect
  pm.response.to.not.have.status(404);
});

pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

pm.test("API response contians the expected header", () => {
  pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});

pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("Postman");
});

The output of the above test is

pm.test("Value of name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.name).to.eql("Postman");
});

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the name field is Postman.
  pm.expect(response).to.have.property("name", "Postman");

  // the line below checks value of the job field is Test (string).
  pm.expect(response).to.have.property("job", "Test");
});

The output of the above test is

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

The output of the above test is

const responseJson = pm.response.json();
pm.test("Test Data Type of the response", () => {
    
    pm.expect(responseJson.job).to.be.a("string");
    pm.expect(parseInt(responseJson.id)).to.be.a("number");
});

pm.test("The response has all properties", () => {
    //parse the response JSON and test three properties
    const responseJson = pm.response.json();
    pm.expect(responseJson.name).to.eql("Postman");
    pm.expect(responseJson.job).to.be.a('string')
    pm.expect(responseJson.id).to.have.lengthOf(3);
});

Sample Response

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the id field is 2 (number).
  pm.expect(response).to.have.property("id", 2);

  // the line below checks value of the name field is Morty Smith (string).
  pm.expect(response).to.have.property("name", "Morty Smith");

  // the line below checks value of the origin.name field is Earth (C-137) (string).
  pm.expect(response).to.have.nested.property("location.name", "Citadel of Ricks");
});

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  pm.expect(response).to.have.nested.property("episode.3", "https://rickandmortyapi.com/api/episode/4");
});

Data Driven Testing in Postman

HOME

In this tutorial, we will see how we can use data files to feed test data to the Postman requests.

Postman provides two ways to pass sets of values to use in a collection run.

By selecting a JSON or CSV data file in the Collection Runner, you can test your requests with multiple different values as part of a single run.

1. Passing data using JSON File

Consider a sample JSON which has 4 sets of data. This data we will be passing to a POST request.

[
   {
      "name":"Test1",
      "job":"Developer"
   },
   {
      "name":"Test2",
      "job":"Scrum Master"
   },
   {
      "name":"Test3",
      "job":"Quality Analyst"
   },
   {
      "name":"Test4",
      "job":"Business Analyst"
   }
]

Create a request in Postman. Below is the URL of the request.

https://reqres.in/api/users

Paste the body of the request in the Body part of the Postman.

Here, name and job are the variables that we will be passing to the POST request. So, we have parameterized these 2 variables.

Create Tests for the request

Click on the “Tests”. Write the tests as shown below:

If you need help in creating the test, you can refer to the snippets present on the right side of the console.

Here, data.json is the name of the json we are using in this example.

To read the JSON data we need to upload a JSON file from the collection runner.

Select the more actions icon More actions icon next to the collection name. Click on the “Run Collection” option.

This shows all the requests present in this collection. Here, I want to run only CreateUser Request. So, I will uncheck the “Dummy User” option.

On the Functional tab, select Run manually.

Click on the Select File and upload the data file. The Iterations field shows the number of data sets present in the data file. Here, we have 4 sets of data, so used Iterations as 4. The Delay field defines the time in ms between two iterations.

Click on the preview to see the data that is uploaded.

Click on the “RunDataDrivenTesting” button to run the tests available in the Collection.

We can see that our API has run four times. Each time, a different set of data is picked up.

2. Passing data using CSV File

All the steps remain the same here, except instead of a JSON, we will use a .csv file.

Click on the “Preview” button to view the data present in the .csv file.

Below are the sample tests.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(201);
});

var expectedName = pm.variables.get("name");
var expectedRole = pm.variables.get("role");


pm.test("Verify name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(expectedName);
});

pm.test("Verify role", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(expectedRole);
});


Run the collection again. Below is the image of the test execution in Postman.

What is Collection in Postman?

HOME

What is Collection?

In Postman, a collection is a group of API requests that are saved in Postman and can be organized into folders. Within a collection, any number of folders can be formed. All API requests can be saved and stored in a collection, which can be shared around the team in the Postman workspace.
Organizing comparable requests into folders and collections enables the customer to better organize and document their demands.

For example, if you are testing or validating a restful API that has 10 different endpoints. Then, it makes sense to organize them in a collection that would make things like applying collection variables, import/export easier and could be run as part of a single collection.

How to create a Collection?

Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2:  Provide a name to the collection – “Demo”.

How to create a request in Collection?

How to send DELETE Requests in Postman?

HOME

In this tutorial, we will create DELETE request in Postman and see how to execute it.

The HTTP DELETE method is used to delete a resource from the server. Unlike GET requests, the DELETE requests may change the server state.
The DELETE method is defined to be idempotent, which means that sending the same HTTP DELETE request multiple times will have the same effect on the server and will not additionally affect the state or cause additional side effects.

We will use the following URL for this Postman tutorial.

https://dummy.restapiexample.com/api/v1/delete/2

To create the first DELETE request in Postman, follow the following steps:

Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2:  Provide a name to the collection – “API Testing”.

Step 3: To create a new request, click on “Add a request” if it is a new Collection. Otherwise, click on the 3 dots and select “Add request”.

Step 4: Once you create a new request, then you will get the following window:

Step 5: Enter the “name” in the request. Here, the name is “DeleteUser”.

Step 5: Enter the “URL” in the address bar.

Step 6: Now, select the “DELETE” request from the list of request methods.

Step 7: Press the “Send” button.

Step 8: Once you press the send button, you will get the response from the server. Make sure you have a proper internet connection; otherwise, you will not get a response.

Status

You can check the status code. Here, we got the status code 200, which means we got a successful response to the request.

Body

In the Body tab of the response box, we have multiple options to see the response in a different format.

Format Type

Each request has a defined response to it as defined by the Content-Type header. That response can be in any format. Such as in the above example, we have JSON code file.

Below are the various format type present in Postman.

XML

HTML

Text

Headers

Headers are the extra information that is transferred to the server or the client. In Postman, headers will show like key-value pairs under the headers tab. Click on the Headers link as shown in the below image:

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!